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}
51
52MCAsmInfo::~MCAsmInfo() = default;
53
54void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
55 InitialFrameState.push_back(x: Inst);
56}
57
58const MCExpr *
59MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
60 unsigned Encoding,
61 MCStreamer &Streamer) const {
62 return getExprForFDESymbol(Sym, Encoding, Streamer);
63}
64
65const MCExpr *MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
66 unsigned Encoding,
67 MCStreamer &Streamer) const {
68 MCContext &Context = Streamer.getContext();
69 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: Context);
70
71 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
72 return Res;
73 if (DwarfFDERelSymbolSpec) {
74 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
75 return MCSpecifierExpr::create(Expr: Res, S: DwarfFDERelSymbolSpec, Ctx&: Context);
76 }
77
78 MCSymbol *PCSym = Context.createTempSymbol();
79 Streamer.emitLabel(Symbol: PCSym);
80 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context);
81 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context);
82}
83
84bool MCAsmInfo::isAcceptableChar(char C) const {
85 // For AIX assembler, symbols may consist of numeric digits, underscores,
86 // periods, uppercase or lowercase letters, orany combination of these.
87 // QualName is allowed for a MCSymbolXCOFF, and QualName contains '[' and ']'.
88 //
89 // Others also allow '$'. HLASM (SystemZ) also allows '#'.
90
91 if (isAlnum(C) || C == '_' || C == '.')
92 return true;
93 if (C == '[' || C == ']')
94 return isAIX();
95 if (C == '@')
96 return doesAllowAtInName();
97 if (C == '$')
98 return !isAIX();
99 if (C == '#')
100 return isHLASM();
101 return false;
102}
103
104bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
105 if (Name.empty())
106 return false;
107
108 // If any of the characters in the string is an unacceptable character, force
109 // quotes.
110 for (char C : Name) {
111 if (!isAcceptableChar(C))
112 return false;
113 }
114
115 return !getReservedIdentifiers().contains(V: CachedHashStringRef(Name.lower()));
116}
117
118bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
119 // FIXME: Does .section .bss/.data/.text work everywhere??
120 return SectionName == ".text" || SectionName == ".data" ||
121 (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
122}
123
124void MCAsmInfo::initializeAtSpecifiers(EnumStrings<AtSpecifierKind, 1> Descs) {
125 assert(AtSpecifierToName.empty() && "cannot initialize twice");
126 UseAtForSpecifier = true;
127 for (const auto &Desc : Descs) {
128 [[maybe_unused]] auto It =
129 AtSpecifierToName.try_emplace(Key: Desc.value(), Args: Desc.name());
130 assert(It.second && "duplicate Kind");
131 [[maybe_unused]] auto It2 =
132 NameToAtSpecifier.try_emplace(Key: Desc.name().lower(), Args: Desc.value());
133 assert(It2.second);
134 }
135}
136
137StringRef MCAsmInfo::getSpecifierName(uint32_t S) const {
138 auto It = AtSpecifierToName.find(Val: S);
139 assert(It != AtSpecifierToName.end() &&
140 "ensure the specifier is set in initializeVariantKinds");
141 return It->second;
142}
143
144std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
145 auto It = NameToAtSpecifier.find(Key: Name.lower());
146 if (It != NameToAtSpecifier.end())
147 return It->second;
148 return {};
149}
150
151void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
152 if (auto *SE = dyn_cast<MCSpecifierExpr>(Val: &Expr))
153 printSpecifierExpr(OS, *SE);
154 else
155 Expr.print(OS, MAI: this);
156}
157
158bool MCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &E,
159 MCValue &Res,
160 const MCAssembler *Asm) const {
161 if (!E.getSubExpr()->evaluateAsRelocatable(Res, Asm))
162 return false;
163
164 Res.setSpecifier(E.getSpecifier());
165 return !Res.getSubSym();
166}
167