LLVM 24.0.0git
MCAsmInfo.cpp
Go to the documentation of this file.
1//===- MCAsmInfo.cpp - Asm Info -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines target asm properties related what form asm statements
10// should take.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/MCAsmInfo.h"
15#include "MCCLOptions.h"
16#include "llvm/ADT/Enum.h"
19#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCValue.h"
25
26using namespace llvm;
27
29 const MCCLOptions &CLOpts = getMCCLOptions(Options);
31 valueOr(CLOpts.dwarf_extended_loc, SupportsExtendedDwarfLocDirective);
33 valueOr(CLOpts.use_leb128_directives, HasLEB128Directives);
34 if (Options.BinutilsVersion.first > 0)
35 BinutilsVersion = Options.BinutilsVersion;
36}
37
38MCAsmInfo::~MCAsmInfo() = default;
39
43 return false;
44}
45
47 return getMCCLOptions(TargetOptions).asm_macro_max_nesting_depth;
48}
49
51 InitialFrameState.push_back(Inst);
52}
53
54const MCExpr *
56 unsigned Encoding,
57 MCStreamer &Streamer) const {
59 MCContext &Context = Streamer.getContext();
60 const MCExpr *Res =
63 const MCExpr *Off =
66 }
67
68 MCSymbol *PCSym = Context.createTempSymbol();
69 Streamer.emitLabel(PCSym);
70 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
71 return MCBinaryExpr::createSub(Res, PC, Context);
72 }
73 return getExprForFDESymbol(Sym, Encoding, Streamer);
74}
75
77 unsigned Encoding,
78 MCStreamer &Streamer) const {
79 MCContext &Context = Streamer.getContext();
80 const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
81
82 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
83 return Res;
85 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
87 }
88
89 MCSymbol *PCSym = Context.createTempSymbol();
90 Streamer.emitLabel(PCSym);
91 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
92 return MCBinaryExpr::createSub(Res, PC, Context);
93}
94
96 uint64_t Offset,
97 MCContext &Ctx) const {
98 const MCExpr *Expr;
101 else
102 Expr = MCSymbolRefExpr::create(Sym, Ctx);
103 if (Offset)
104 Expr =
106 return Expr;
107}
108
110 // For AIX assembler, symbols may consist of numeric digits, underscores,
111 // periods, uppercase or lowercase letters, orany combination of these.
112 // QualName is allowed for a MCSymbolXCOFF, and QualName contains '[' and ']'.
113 //
114 // Others also allow '$'. HLASM (SystemZ) also allows '#'.
115
116 if (isAlnum(C) || C == '_' || C == '.')
117 return true;
118 if (C == '[' || C == ']')
119 return isAIX();
120 if (C == '@')
121 return doesAllowAtInName();
122 if (C == '$')
123 return !isAIX();
124 if (C == '#')
125 return isHLASM();
126 return false;
127}
128
130 if (Name.empty())
131 return false;
132
133 // If any of the characters in the string is an unacceptable character, force
134 // quotes.
135 for (char C : Name) {
136 if (!isAcceptableChar(C))
137 return false;
138 }
139
140 return !getReservedIdentifiers().contains(CachedHashStringRef(Name.lower()));
141}
142
144 assert(AtSpecifierToName.empty() && "cannot initialize twice");
145 UseAtForSpecifier = true;
146 for (const auto &Desc : Descs) {
147 [[maybe_unused]] auto It =
148 AtSpecifierToName.try_emplace(Desc.value(), Desc.name());
149 assert(It.second && "duplicate Kind");
150 [[maybe_unused]] auto It2 =
151 NameToAtSpecifier.try_emplace(Desc.name().lower(), Desc.value());
152 assert(It2.second);
153 }
154}
155
157 auto It = AtSpecifierToName.find(S);
158 assert(It != AtSpecifierToName.end() &&
159 "ensure the specifier is set in initializeVariantKinds");
160 return It->second;
161}
162
163std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
164 auto It = NameToAtSpecifier.find(Name.lower());
165 if (It != NameToAtSpecifier.end())
166 return It->second;
167 return {};
168}
169
170void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
171 if (auto *SE = dyn_cast<MCSpecifierExpr>(&Expr)) {
173 "need to implement hook if target uses MCSpecifierExpr");
174 SpecifierExprPrintFn(*this, OS, *SE);
175 } else {
176 Expr.print(OS, this);
177 }
178}
179
181 MCValue &Res,
182 const MCAssembler *Asm) const {
184 return SpecifierExprEvaluateFn(E, Res, Asm);
185
186 if (!E.getSubExpr()->evaluateAsRelocatable(Res, Asm))
187 return false;
188
189 Res.setSpecifier(E.getSpecifier());
190 return !Res.getSubSym();
191}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains constants used for implementing Dwarf debug support.
static LVOptions Options
Definition LVOptions.cpp:25
This file contains some functions that are useful when dealing with strings.
std::unique_ptr< MCStreamer > && Streamer
A container which contains a StringRef plus a precomputed hash.
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition MCAsmInfo.cpp:50
bool(* SpecifierExprEvaluateFn)(const MCSpecifierExpr &E, MCValue &Res, const MCAssembler *Asm)
Definition MCAsmInfo.h:231
unsigned DwarfSectionOffSpecifierExpr
If non-zero, MCSpecifierExpr kind used as base for DWARF-relative exprs.
Definition MCAsmInfo.h:423
bool isHLASM() const
Definition MCAsmInfo.h:572
bool UseAtForSpecifier
True if target uses @ (expr@specifier) for relocation specifiers.
Definition MCAsmInfo.h:411
std::optional< unsigned > CompInstByteOrderFeature
Feature that indicates mixed-endian byte order.
Definition MCAsmInfo.h:99
std::vector< MCCFIInstruction > InitialFrameState
Definition MCAsmInfo.h:427
llvm::DenseSet< llvm::CachedHashStringRef > & getReservedIdentifiers()
Definition MCAsmInfo.h:543
bool evaluateAsRelocatableImpl(const MCSpecifierExpr &, MCValue &Res, const MCAssembler *Asm) const
bool doesAllowAtInName() const
Definition MCAsmInfo.h:614
const MCTargetOptions & TargetOptions
Definition MCAsmInfo.h:477
StringRef getSpecifierName(uint32_t S) const
MCAsmInfo(const MCTargetOptions &Options)
Definition MCAsmInfo.cpp:28
bool isAIX() const
Definition MCAsmInfo.h:571
std::optional< uint32_t > getSpecifierForName(StringRef Name) const
void printExpr(raw_ostream &, const MCExpr &) const
uint16_t DwarfFDERelSymbolSpec
The optional specifier to use for the relative FDE symbol references.
Definition MCAsmInfo.h:400
int DwarfPersonalitySymbolsAbsOff
Non-zero if DWARF personality symbol reference relocations should have a constant addend instead of b...
Definition MCAsmInfo.h:393
unsigned getAsmMacroMaxNestingDepth() const
Definition MCAsmInfo.cpp:46
llvm::DenseMap< AtSpecifierKind, StringRef > AtSpecifierToName
Definition MCAsmInfo.h:462
void(* SpecifierExprPrintFn)(const MCAsmInfo &, raw_ostream &, const MCSpecifierExpr &)
Definition MCAsmInfo.h:228
const MCExpr * getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition MCAsmInfo.cpp:76
const MCExpr * getDwarfSectionOffsetExpr(const MCSymbol *Sym, uint64_t Offset, MCContext &Ctx) const
Return the expression used to emit the offset of Sym + Offset from the start of its DWARF section.
Definition MCAsmInfo.cpp:95
bool isAcceptableChar(char C) const
Return true if C is an acceptable character inside a symbol name.
llvm::StringMap< AtSpecifierKind > NameToAtSpecifier
Definition MCAsmInfo.h:463
bool SupportsExtendedDwarfLocDirective
True if the target supports flags in ".loc" directive, false if only location is allowed.
Definition MCAsmInfo.h:419
void initializeAtSpecifiers(EnumStrings< AtSpecifierKind, 1 >)
uint16_t DwarfPersonalitySymbolSpec
The optional specifier to use for the personality symbol references.
Definition MCAsmInfo.h:388
const MCExpr * getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition MCAsmInfo.cpp:55
bool usesCompInstByteOrder(const MCSubtargetInfo &STI) const
True if the instruction is stored in multiple halfwords, with the high-order half appearing first in ...
Definition MCAsmInfo.cpp:40
std::pair< int, int > BinutilsVersion
Definition MCAsmInfo.h:435
bool HasLEB128Directives
True if the target supports LEB128 directives.
Definition MCAsmInfo.h:220
bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:339
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:424
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:84
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:491
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:221
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
void setSpecifier(uint32_t S)
Definition MCValue.h:47
const MCSymbol * getSubSym() const
Definition MCValue.h:51
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:183
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ DW_EH_PE_pcrel
Definition Dwarf.h:982
@ DW_EH_PE_sdata4
Definition Dwarf.h:979
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const MCCLOptions & getMCCLOptions(const MCTargetOptions &Options)
Definition MCCLOptions.h:19
Op::Description Desc
@ Off
Use window algorithm after SMS algorithm fails.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
constexpr bool valueOr(BoolOrDefault X, bool Default)