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"
22#include "llvm/MC/MCValue.h"
24
25using namespace llvm;
26
28 const MCCLOptions &CLOpts = MCCLOptions::Global;
30 valueOr(CLOpts.dwarf_extended_loc, SupportsExtendedDwarfLocDirective);
32 valueOr(CLOpts.use_leb128_directives, HasLEB128Directives);
33 if (Options.BinutilsVersion.first > 0)
34 BinutilsVersion = Options.BinutilsVersion;
35}
36
37MCAsmInfo::~MCAsmInfo() = default;
38
40 InitialFrameState.push_back(Inst);
41}
42
43const MCExpr *
45 unsigned Encoding,
46 MCStreamer &Streamer) const {
47 return getExprForFDESymbol(Sym, Encoding, Streamer);
48}
49
51 unsigned Encoding,
52 MCStreamer &Streamer) const {
53 MCContext &Context = Streamer.getContext();
54 const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
55
56 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
57 return Res;
59 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
61 }
62
63 MCSymbol *PCSym = Context.createTempSymbol();
64 Streamer.emitLabel(PCSym);
65 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
66 return MCBinaryExpr::createSub(Res, PC, Context);
67}
68
70 // For AIX assembler, symbols may consist of numeric digits, underscores,
71 // periods, uppercase or lowercase letters, orany combination of these.
72 // QualName is allowed for a MCSymbolXCOFF, and QualName contains '[' and ']'.
73 //
74 // Others also allow '$'. HLASM (SystemZ) also allows '#'.
75
76 if (isAlnum(C) || C == '_' || C == '.')
77 return true;
78 if (C == '[' || C == ']')
79 return isAIX();
80 if (C == '@')
81 return doesAllowAtInName();
82 if (C == '$')
83 return !isAIX();
84 if (C == '#')
85 return isHLASM();
86 return false;
87}
88
90 if (Name.empty())
91 return false;
92
93 // If any of the characters in the string is an unacceptable character, force
94 // quotes.
95 for (char C : Name) {
96 if (!isAcceptableChar(C))
97 return false;
98 }
99
100 return !getReservedIdentifiers().contains(CachedHashStringRef(Name.lower()));
101}
102
104 // FIXME: Does .section .bss/.data/.text work everywhere??
105 return SectionName == ".text" || SectionName == ".data" ||
107}
108
110 assert(AtSpecifierToName.empty() && "cannot initialize twice");
111 UseAtForSpecifier = true;
112 for (const auto &Desc : Descs) {
113 [[maybe_unused]] auto It =
114 AtSpecifierToName.try_emplace(Desc.value(), Desc.name());
115 assert(It.second && "duplicate Kind");
116 [[maybe_unused]] auto It2 =
117 NameToAtSpecifier.try_emplace(Desc.name().lower(), Desc.value());
118 assert(It2.second);
119 }
120}
121
123 auto It = AtSpecifierToName.find(S);
124 assert(It != AtSpecifierToName.end() &&
125 "ensure the specifier is set in initializeVariantKinds");
126 return It->second;
127}
128
129std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
130 auto It = NameToAtSpecifier.find(Name.lower());
131 if (It != NameToAtSpecifier.end())
132 return It->second;
133 return {};
134}
135
136void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
137 if (auto *SE = dyn_cast<MCSpecifierExpr>(&Expr))
138 printSpecifierExpr(OS, *SE);
139 else
140 Expr.print(OS, this);
141}
142
144 MCValue &Res,
145 const MCAssembler *Asm) const {
146 if (!E.getSubExpr()->evaluateAsRelocatable(Res, Asm))
147 return false;
148
149 Res.setSpecifier(E.getSpecifier());
150 return !Res.getSubSym();
151}
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:39
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:758
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:27
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:50
bool isAcceptableChar(char C) const
Return true if C is an acceptable character inside a symbol name.
Definition MCAsmInfo.cpp:69
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:44
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.
Definition MCAsmInfo.cpp:89
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
@ DW_EH_PE_pcrel
Definition Dwarf.h:977
@ DW_EH_PE_sdata4
Definition Dwarf.h:974
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
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)