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 = MCCLOptions::Global;
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
39void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
40 InitialFrameState.push_back(x: Inst);
41}
42
43const MCExpr *
44MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
45 unsigned Encoding,
46 MCStreamer &Streamer) const {
47 return getExprForFDESymbol(Sym, Encoding, Streamer);
48}
49
50const MCExpr *MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
51 unsigned Encoding,
52 MCStreamer &Streamer) const {
53 MCContext &Context = Streamer.getContext();
54 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: Context);
55
56 if (!(Encoding & dwarf::DW_EH_PE_pcrel))
57 return Res;
58 if (DwarfFDERelSymbolSpec) {
59 assert(Encoding & dwarf::DW_EH_PE_sdata4 && "Unexpected encoding");
60 return MCSpecifierExpr::create(Expr: Res, S: DwarfFDERelSymbolSpec, Ctx&: Context);
61 }
62
63 MCSymbol *PCSym = Context.createTempSymbol();
64 Streamer.emitLabel(Symbol: PCSym);
65 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context);
66 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context);
67}
68
69bool MCAsmInfo::isAcceptableChar(char C) const {
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
89bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
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(V: CachedHashStringRef(Name.lower()));
101}
102
103bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
104 // FIXME: Does .section .bss/.data/.text work everywhere??
105 return SectionName == ".text" || SectionName == ".data" ||
106 (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
107}
108
109void MCAsmInfo::initializeAtSpecifiers(EnumStrings<AtSpecifierKind, 1> Descs) {
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(Key: Desc.value(), Args: Desc.name());
115 assert(It.second && "duplicate Kind");
116 [[maybe_unused]] auto It2 =
117 NameToAtSpecifier.try_emplace(Key: Desc.name().lower(), Args: Desc.value());
118 assert(It2.second);
119 }
120}
121
122StringRef MCAsmInfo::getSpecifierName(uint32_t S) const {
123 auto It = AtSpecifierToName.find(Val: 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(Key: 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>(Val: &Expr))
138 printSpecifierExpr(OS, *SE);
139 else
140 Expr.print(OS, MAI: this);
141}
142
143bool MCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &E,
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}
152