LLVM 22.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"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCValue.h"
23
24using namespace llvm;
25
26namespace {
28}
30 "dwarf-extended-loc", cl::Hidden,
31 cl::desc("Disable emission of the extended flags in .loc directives."),
32 cl::values(clEnumVal(Default, "Default for platform"),
33 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
35
36namespace llvm {
38 "use-leb128-directives", cl::Hidden,
40 "Disable the usage of LEB128 directives, and generate .byte instead."),
42}
43
45 SeparatorString = ";";
46 CommentString = "#";
47 LabelSuffix = ":";
51 InlineAsmStart = "APP";
52 InlineAsmEnd = "NO_APP";
53 ZeroDirective = "\t.zero\t";
54 AsciiDirective = "\t.ascii\t";
55 AscizDirective = "\t.asciz\t";
56 Data8bitsDirective = "\t.byte\t";
57 Data16bitsDirective = "\t.short\t";
58 Data32bitsDirective = "\t.long\t";
59 Data64bitsDirective = "\t.quad\t";
60 GlobalDirective = "\t.globl\t";
61 WeakDirective = "\t.weak\t";
70}
71
72MCAsmInfo::~MCAsmInfo() = default;
73
75 InitialFrameState.push_back(Inst);
76}
77
78const MCExpr *
80 unsigned Encoding,
81 MCStreamer &Streamer) const {
82 return getExprForFDESymbol(Sym, Encoding, Streamer);
83}
84
85const MCExpr *
87 unsigned Encoding,
88 MCStreamer &Streamer) const {
89 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
90 return MCSymbolRefExpr::create(Sym, Streamer.getContext());
91
92 MCContext &Context = Streamer.getContext();
94 MCSymbol *PCSym = Context.createTempSymbol();
95 Streamer.emitLabel(PCSym);
96 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
97 return MCBinaryExpr::createSub(Res, PC, Context);
98}
99
101 if (C == '@')
102 return doesAllowAtInName();
103
104 return isAlnum(C) || C == '_' || C == '$' || C == '.';
105}
106
108 if (Name.empty())
109 return false;
110
111 // If any of the characters in the string is an unacceptable character, force
112 // quotes.
113 for (char C : Name) {
114 if (!isAcceptableChar(C))
115 return false;
116 }
117
118 return true;
119}
120
122 // FIXME: Does .section .bss/.data/.text work everywhere??
123 return SectionName == ".text" || SectionName == ".data" ||
125}
126
128 assert(AtSpecifierToName.empty() && "cannot initialize twice");
129 for (auto Desc : Descs) {
130 [[maybe_unused]] auto It =
131 AtSpecifierToName.try_emplace(Desc.Kind, Desc.Name);
132 assert(It.second && "duplicate Kind");
133 [[maybe_unused]] auto It2 =
134 NameToAtSpecifier.try_emplace(Desc.Name.lower(), Desc.Kind);
135 assert(It2.second);
136 }
137}
138
140 auto It = AtSpecifierToName.find(S);
141 assert(It != AtSpecifierToName.end() &&
142 "ensure the specifier is set in initializeVariantKinds");
143 return It->second;
144}
145
146std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
147 auto It = NameToAtSpecifier.find(Name.lower());
148 if (It != NameToAtSpecifier.end())
149 return It->second;
150 return {};
151}
152
153void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
154 if (auto *SE = dyn_cast<MCSpecifierExpr>(&Expr))
156 else
157 Expr.print(OS, this);
158}
159
161 MCValue &Res,
162 const MCAssembler *Asm) const {
163 if (!E.getSubExpr()->evaluateAsRelocatable(Res, Asm))
164 return false;
165
166 Res.setSpecifier(E.getSpecifier());
167 return !Res.getSubSym();
168}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define clEnumVal(ENUMVAL, DESC)
Definition: CommandLine.h:685
DefaultOnOff
Definition: DwarfDebug.cpp:86
@ Default
Definition: DwarfDebug.cpp:86
@ Enable
Definition: DwarfDebug.cpp:86
@ Disable
Definition: DwarfDebug.cpp:86
This file contains constants used for implementing Dwarf debug support.
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
static cl::opt< DefaultOnOff > DwarfExtendedLoc("dwarf-extended-loc", cl::Hidden, cl::desc("Disable emission of the extended flags in .loc directives."), cl::values(clEnumVal(Default, "Default for platform"), clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), cl::init(Default))
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const char * Data16bitsDirective
Definition: MCAsmInfo.h:248
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.cpp:74
virtual void printSpecifierExpr(raw_ostream &, const MCSpecifierExpr &) const
Definition: MCAsmInfo.h:725
const char * AsciiDirective
This directive allows emission of an ascii string with the standard C escape characters embedded into...
Definition: MCAsmInfo.h:231
std::vector< MCCFIInstruction > InitialFrameState
Definition: MCAsmInfo.h:397
llvm::StringMap< uint32_t > NameToAtSpecifier
Definition: MCAsmInfo.h:431
void initializeAtSpecifiers(ArrayRef< AtSpecifier >)
Definition: MCAsmInfo.cpp:127
const char * Data8bitsDirective
These directives are used to output some unit of integer data to the current section.
Definition: MCAsmInfo.h:247
const char * Data64bitsDirective
Definition: MCAsmInfo.h:250
llvm::DenseMap< uint32_t, StringRef > AtSpecifierToName
Definition: MCAsmInfo.h:430
virtual ~MCAsmInfo()
virtual bool evaluateAsRelocatableImpl(const MCSpecifierExpr &, MCValue &Res, const MCAssembler *Asm) const
Definition: MCAsmInfo.cpp:160
bool doesAllowAtInName() const
Definition: MCAsmInfo.h:561
bool PreserveAsmComments
Preserve Comments in assembly.
Definition: MCAsmInfo.h:418
const char * LabelSuffix
This is appended to emitted labels. Defaults to ":".
Definition: MCAsmInfo.h:146
StringRef PrivateGlobalPrefix
This prefix is used for globals like constant pool entries that are completely private to the ....
Definition: MCAsmInfo.h:160
StringRef getSpecifierName(uint32_t S) const
Definition: MCAsmInfo.cpp:139
const char * Data32bitsDirective
Definition: MCAsmInfo.h:249
bool UseIntegratedAssembler
Should we use the integrated assembler? The integrated assembler should be enabled by default (by the...
Definition: MCAsmInfo.h:412
const char * GlobalDirective
This is the directive used to declare a global entity.
Definition: MCAsmInfo.h:283
std::optional< uint32_t > getSpecifierForName(StringRef Name) const
Definition: MCAsmInfo.cpp:146
StringRef PrivateLabelPrefix
This prefix is used for labels for basic blocks.
Definition: MCAsmInfo.h:164
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
const char * InlineAsmStart
If these are nonempty, they contain a directive to emit before and after an inline assembly statement...
Definition: MCAsmInfo.h:174
bool ParseInlineAsmUsingAsmParser
Use AsmParser to parse inlineAsm when UseIntegratedAssembler is not set.
Definition: MCAsmInfo.h:415
StringRef LinkerPrivateGlobalPrefix
This prefix is used for symbols that should be passed through the assembler but be removed by the lin...
Definition: MCAsmInfo.h:170
virtual const MCExpr * getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:86
bool PPCUseFullRegisterNames
True if full register names are printed.
Definition: MCAsmInfo.h:218
const char * InlineAsmEnd
Definition: MCAsmInfo.h:175
virtual bool isAcceptableChar(char C) const
Return true if C is an acceptable character inside a symbol name.
Definition: MCAsmInfo.cpp:100
const char * SeparatorString
This string, if specified, is used to separate instructions from each other when on the same line.
Definition: MCAsmInfo.h:131
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
Definition: MCAsmInfo.cpp:121
bool usesELFSectionDirectiveForBSS() const
Definition: MCAsmInfo.h:509
bool SupportsExtendedDwarfLocDirective
True if the target supports flags in ".loc" directive, false if only location is allowed.
Definition: MCAsmInfo.h:393
const char * ZeroDirective
This should be set to the directive used to get some number of zero (and non-zero if supported by the...
Definition: MCAsmInfo.h:226
const char * AscizDirective
If not null, this allows for special handling of zero terminated strings on this target.
Definition: MCAsmInfo.h:236
virtual const MCExpr * getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:79
bool HasLEB128Directives
True if the target supports LEB128 directives.
Definition: MCAsmInfo.h:215
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:107
StringRef CommentString
This indicates the comment string used by the assembler.
Definition: MCAsmInfo.h:135
const char * WeakDirective
Used to declare a global as being a weak symbol. Defaults to ".weak".
Definition: MCAsmInfo.h:321
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:428
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:450
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition: MCExpr.h:495
const MCExpr * getSubExpr() const
Definition: MCExpr.h:509
Spec getSpecifier() const
Definition: MCExpr.h:508
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCContext & getContext() const
Definition: MCStreamer.h:314
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:395
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:214
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
iterator end()
Definition: StringMap.h:224
iterator find(StringRef Key)
Definition: StringMap.h:237
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:372
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:712
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
@ DW_EH_PE_pcrel
Definition: Dwarf.h:865
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< cl::boolOrDefault > UseLEB128Directives("use-leb128-directives", cl::Hidden, cl::desc("Disable the usage of LEB128 directives, and generate .byte instead."), cl::init(cl::BOU_UNSET))
@ Default
The result values are uniform if and only if all operands are uniform.
@ Enable
Enable colors.
Description of the encoding of one expression Op.