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"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/BinaryFormat/Dwarf.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"
22#include "llvm/Support/Casting.h"
23#include "llvm/Support/CommandLine.h"
24
25using namespace llvm;
26
27namespace {
28enum DefaultOnOff { Default, Enable, Disable };
29}
30static cl::opt<DefaultOnOff> DwarfExtendedLoc(
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")),
35 cl::init(Val: Default));
36
37namespace llvm {
38cl::opt<cl::boolOrDefault> UseLEB128Directives(
39 "use-leb128-directives", cl::Hidden,
40 cl::desc(
41 "Disable the usage of LEB128 directives, and generate .byte instead."),
42 cl::init(Val: cl::boolOrDefault::BOU_UNSET));
43}
44
45MCAsmInfo::MCAsmInfo(const MCTargetOptions &Options) : TargetOptions(Options) {
46 if (DwarfExtendedLoc != Default)
47 SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable;
48 if (UseLEB128Directives != cl::boolOrDefault::BOU_UNSET)
49 HasLEB128Directives = UseLEB128Directives == cl::boolOrDefault::BOU_TRUE;
50 if (Options.BinutilsVersion.first > 0)
51 BinutilsVersion = Options.BinutilsVersion;
52}
53
54MCAsmInfo::~MCAsmInfo() = default;
55
56void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
57 InitialFrameState.push_back(x: Inst);
58}
59
60const MCExpr *
61MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
62 unsigned Encoding,
63 MCStreamer &Streamer) const {
64 return getExprForFDESymbol(Sym, Encoding, Streamer);
65}
66
67const MCExpr *MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
68 unsigned Encoding,
69 MCStreamer &Streamer) const {
70 MCContext &Context = Streamer.getContext();
71 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: Context);
72
73 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
74 return Res;
75 if (DwarfFDERelSymbolSpec) {
76 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
77 return MCSpecifierExpr::create(Expr: Res, S: DwarfFDERelSymbolSpec, Ctx&: Context);
78 }
79
80 MCSymbol *PCSym = Context.createTempSymbol();
81 Streamer.emitLabel(Symbol: PCSym);
82 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context);
83 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context);
84}
85
86bool MCAsmInfo::isAcceptableChar(char C) const {
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
106bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
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(V: CachedHashStringRef(Name.lower()));
118}
119
120bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
121 // FIXME: Does .section .bss/.data/.text work everywhere??
122 return SectionName == ".text" || SectionName == ".data" ||
123 (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
124}
125
126void MCAsmInfo::initializeAtSpecifiers(EnumStrings<AtSpecifierKind, 1> Descs) {
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(Key: Desc.value(), Args: Desc.name());
132 assert(It.second && "duplicate Kind");
133 [[maybe_unused]] auto It2 =
134 NameToAtSpecifier.try_emplace(Key: Desc.name().lower(), Args: Desc.value());
135 assert(It2.second);
136 }
137}
138
139StringRef MCAsmInfo::getSpecifierName(uint32_t S) const {
140 auto It = AtSpecifierToName.find(Val: 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(Key: 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>(Val: &Expr))
155 printSpecifierExpr(OS, *SE);
156 else
157 Expr.print(OS, MAI: this);
158}
159
160bool MCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &E,
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}
169