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