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 "llvm/ADT/Enum.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCValue.h"
24
25using namespace llvm;
26
27namespace {
29}
31 "dwarf-extended-loc", cl::Hidden,
32 cl::desc("Disable emission of the extended flags in .loc directives."),
33 cl::values(clEnumVal(Default, "Default for platform"),
34 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
36
37namespace llvm {
39 "use-leb128-directives", cl::Hidden,
41 "Disable the usage of LEB128 directives, and generate .byte instead."),
43}
44
53
54MCAsmInfo::~MCAsmInfo() = default;
55
57 InitialFrameState.push_back(Inst);
58}
59
60const MCExpr *
62 unsigned Encoding,
63 MCStreamer &Streamer) const {
64 return getExprForFDESymbol(Sym, Encoding, Streamer);
65}
66
68 unsigned Encoding,
69 MCStreamer &Streamer) const {
70 MCContext &Context = Streamer.getContext();
71 const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
72
73 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
74 return Res;
76 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
78 }
79
80 MCSymbol *PCSym = Context.createTempSymbol();
81 Streamer.emitLabel(PCSym);
82 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
83 return MCBinaryExpr::createSub(Res, PC, Context);
84}
85
87 // For AIX assembler, symbols may consist of numeric digits, underscores,
88 // periods, uppercase or lowercase letters, orany combination of these.
89 // QualName is allowed for a MCSymbolXCOFF, and QualName contains '[' and ']'.
90 //
91 // Others also allow '$'. HLASM (SystemZ) also allows '#'.
92
93 if (isAlnum(C) || C == '_' || C == '.')
94 return true;
95 if (C == '[' || C == ']')
96 return isAIX();
97 if (C == '@')
98 return doesAllowAtInName();
99 if (C == '$')
100 return !isAIX();
101 if (C == '#')
102 return isHLASM();
103 return false;
104}
105
107 if (Name.empty())
108 return false;
109
110 // If any of the characters in the string is an unacceptable character, force
111 // quotes.
112 for (char C : Name) {
113 if (!isAcceptableChar(C))
114 return false;
115 }
116
117 return !getReservedIdentifiers().contains(CachedHashStringRef(Name.lower()));
118}
119
121 // FIXME: Does .section .bss/.data/.text work everywhere??
122 return SectionName == ".text" || SectionName == ".data" ||
124}
125
127 assert(AtSpecifierToName.empty() && "cannot initialize twice");
128 UseAtForSpecifier = true;
129 for (const auto &Desc : Descs) {
130 [[maybe_unused]] auto It =
131 AtSpecifierToName.try_emplace(Desc.value(), Desc.name());
132 assert(It.second && "duplicate Kind");
133 [[maybe_unused]] auto It2 =
134 NameToAtSpecifier.try_emplace(Desc.name().lower(), Desc.value());
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))
155 printSpecifierExpr(OS, *SE);
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!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define clEnumVal(ENUMVAL, DESC)
DefaultOnOff
@ Default
@ Enable
@ Disable
This file contains constants used for implementing Dwarf debug support.
static LVOptions Options
Definition LVOptions.cpp:25
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))
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:56
bool isHLASM() const
Definition MCAsmInfo.h:538
bool UseAtForSpecifier
True if target uses @ (expr@specifier) for relocation specifiers.
Definition MCAsmInfo.h:386
virtual void printSpecifierExpr(raw_ostream &, const MCSpecifierExpr &) const
Definition MCAsmInfo.h:743
std::vector< MCCFIInstruction > InitialFrameState
Definition MCAsmInfo.h:398
llvm::DenseSet< llvm::CachedHashStringRef > & getReservedIdentifiers()
Definition MCAsmInfo.h:499
virtual ~MCAsmInfo()
virtual bool evaluateAsRelocatableImpl(const MCSpecifierExpr &, MCValue &Res, const MCAssembler *Asm) const
bool doesAllowAtInName() const
Definition MCAsmInfo.h:583
const MCTargetOptions & TargetOptions
Definition MCAsmInfo.h:441
StringRef getSpecifierName(uint32_t S) const
MCAsmInfo(const MCTargetOptions &Options)
Definition MCAsmInfo.cpp:45
bool isAIX() const
Definition MCAsmInfo.h:537
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:375
llvm::DenseMap< AtSpecifierKind, StringRef > AtSpecifierToName
Definition MCAsmInfo.h:433
const MCExpr * getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition MCAsmInfo.cpp:67
bool isAcceptableChar(char C) const
Return true if C is an acceptable character inside a symbol name.
Definition MCAsmInfo.cpp:86
virtual bool shouldOmitSectionDirective(StringRef SectionName) const
Return true if the .section directive should be omitted when emitting SectionName.
llvm::StringMap< AtSpecifierKind > NameToAtSpecifier
Definition MCAsmInfo.h:434
bool usesELFSectionDirectiveForBSS() const
Definition MCAsmInfo.h:527
bool SupportsExtendedDwarfLocDirective
True if the target supports flags in ".loc" directive, false if only location is allowed.
Definition MCAsmInfo.h:394
void initializeAtSpecifiers(EnumStrings< AtSpecifierKind, 1 >)
virtual const MCExpr * getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition MCAsmInfo.cpp:61
std::pair< int, int > BinutilsVersion
Definition MCAsmInfo.h:406
bool HasLEB128Directives
True if the target supports LEB128 directives.
Definition MCAsmInfo.h:210
bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:427
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
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:494
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:222
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
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:182
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
@ DW_EH_PE_pcrel
Definition Dwarf.h:962
@ DW_EH_PE_sdata4
Definition Dwarf.h:959
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Op::Description Desc
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::boolOrDefault::BOU_UNSET))
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
@ Default
The result value is uniform if and only if all operands are uniform.
Definition Uniformity.h:20
@ Enable
Enable colors.
Definition WithColor.h:47