| 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/StringExtras.h" |
| 16 | #include "llvm/BinaryFormat/Dwarf.h" |
| 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/MC/MCExpr.h" |
| 19 | #include "llvm/MC/MCStreamer.h" |
| 20 | #include "llvm/MC/MCValue.h" |
| 21 | #include "llvm/Support/Casting.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | enum DefaultOnOff { Default, Enable, Disable }; |
| 28 | } |
| 29 | static cl::opt<DefaultOnOff> DwarfExtendedLoc( |
| 30 | "dwarf-extended-loc" , cl::Hidden, |
| 31 | cl::desc("Disable emission of the extended flags in .loc directives." ), |
| 32 | cl::values(clEnumVal(Default, "Default for platform" ), |
| 33 | clEnumVal(Enable, "Enabled" ), clEnumVal(Disable, "Disabled" )), |
| 34 | cl::init(Val: Default)); |
| 35 | |
| 36 | namespace llvm { |
| 37 | cl::opt<cl::boolOrDefault> UseLEB128Directives( |
| 38 | "use-leb128-directives" , cl::Hidden, |
| 39 | cl::desc( |
| 40 | "Disable the usage of LEB128 directives, and generate .byte instead." ), |
| 41 | cl::init(Val: cl::BOU_UNSET)); |
| 42 | } |
| 43 | |
| 44 | MCAsmInfo::MCAsmInfo() { |
| 45 | if (DwarfExtendedLoc != Default) |
| 46 | SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable; |
| 47 | if (UseLEB128Directives != cl::BOU_UNSET) |
| 48 | HasLEB128Directives = UseLEB128Directives == cl::BOU_TRUE; |
| 49 | } |
| 50 | |
| 51 | MCAsmInfo::~MCAsmInfo() = default; |
| 52 | |
| 53 | void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) { |
| 54 | InitialFrameState.push_back(x: Inst); |
| 55 | } |
| 56 | |
| 57 | const MCExpr * |
| 58 | MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym, |
| 59 | unsigned Encoding, |
| 60 | MCStreamer &Streamer) const { |
| 61 | return getExprForFDESymbol(Sym, Encoding, Streamer); |
| 62 | } |
| 63 | |
| 64 | const MCExpr * |
| 65 | MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym, |
| 66 | unsigned Encoding, |
| 67 | MCStreamer &Streamer) const { |
| 68 | if (!(Encoding & dwarf::DW_EH_PE_pcrel)) |
| 69 | return MCSymbolRefExpr::create(Symbol: Sym, Ctx&: Streamer.getContext()); |
| 70 | |
| 71 | MCContext &Context = Streamer.getContext(); |
| 72 | const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: Context); |
| 73 | MCSymbol *PCSym = Context.createTempSymbol(); |
| 74 | Streamer.emitLabel(Symbol: PCSym); |
| 75 | const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context); |
| 76 | return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context); |
| 77 | } |
| 78 | |
| 79 | bool MCAsmInfo::isAcceptableChar(char C) const { |
| 80 | if (C == '@') |
| 81 | return doesAllowAtInName(); |
| 82 | |
| 83 | return isAlnum(C) || C == '_' || C == '$' || C == '.'; |
| 84 | } |
| 85 | |
| 86 | bool MCAsmInfo::isValidUnquotedName(StringRef Name) const { |
| 87 | if (Name.empty()) |
| 88 | return false; |
| 89 | |
| 90 | // If any of the characters in the string is an unacceptable character, force |
| 91 | // quotes. |
| 92 | for (char C : Name) { |
| 93 | if (!isAcceptableChar(C)) |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const { |
| 101 | // FIXME: Does .section .bss/.data/.text work everywhere?? |
| 102 | return SectionName == ".text" || SectionName == ".data" || |
| 103 | (SectionName == ".bss" && !usesELFSectionDirectiveForBSS()); |
| 104 | } |
| 105 | |
| 106 | void MCAsmInfo::initializeAtSpecifiers(ArrayRef<AtSpecifier> Descs) { |
| 107 | assert(AtSpecifierToName.empty() && "cannot initialize twice" ); |
| 108 | UseAtForSpecifier = true; |
| 109 | for (auto Desc : Descs) { |
| 110 | [[maybe_unused]] auto It = |
| 111 | AtSpecifierToName.try_emplace(Key: Desc.Kind, Args&: Desc.Name); |
| 112 | assert(It.second && "duplicate Kind" ); |
| 113 | [[maybe_unused]] auto It2 = |
| 114 | NameToAtSpecifier.try_emplace(Key: Desc.Name.lower(), Args&: Desc.Kind); |
| 115 | assert(It2.second); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | StringRef MCAsmInfo::getSpecifierName(uint32_t S) const { |
| 120 | auto It = AtSpecifierToName.find(Val: S); |
| 121 | assert(It != AtSpecifierToName.end() && |
| 122 | "ensure the specifier is set in initializeVariantKinds" ); |
| 123 | return It->second; |
| 124 | } |
| 125 | |
| 126 | std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const { |
| 127 | auto It = NameToAtSpecifier.find(Key: Name.lower()); |
| 128 | if (It != NameToAtSpecifier.end()) |
| 129 | return It->second; |
| 130 | return {}; |
| 131 | } |
| 132 | |
| 133 | void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const { |
| 134 | if (auto *SE = dyn_cast<MCSpecifierExpr>(Val: &Expr)) |
| 135 | printSpecifierExpr(OS, *SE); |
| 136 | else |
| 137 | Expr.print(OS, MAI: this); |
| 138 | } |
| 139 | |
| 140 | bool MCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &E, |
| 141 | MCValue &Res, |
| 142 | const MCAssembler *Asm) const { |
| 143 | if (!E.getSubExpr()->evaluateAsRelocatable(Res, Asm)) |
| 144 | return false; |
| 145 | |
| 146 | Res.setSpecifier(E.getSpecifier()); |
| 147 | return !Res.getSubSym(); |
| 148 | } |
| 149 | |