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/BinaryFormat/Dwarf.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Support/CommandLine.h"
20 
21 using namespace llvm;
22 
23 enum DefaultOnOff { Default, Enable, Disable };
24 static cl::opt<DefaultOnOff> DwarfExtendedLoc(
25     "dwarf-extended-loc", cl::Hidden,
26     cl::desc("Disable emission of the extended flags in .loc directives."),
27     cl::values(clEnumVal(Default, "Default for platform"),
28                clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
29     cl::init(Default));
30 
31 cl::opt<cl::boolOrDefault> UseLEB128Directives(
32     "use-leb128-directives", cl::Hidden,
33     cl::desc(
34         "Disable the usage of LEB128 directives, and generate .byte instead."),
35     cl::init(cl::BOU_UNSET));
36 
MCAsmInfo()37 MCAsmInfo::MCAsmInfo() {
38   SeparatorString = ";";
39   CommentString = "#";
40   LabelSuffix = ":";
41   PrivateGlobalPrefix = "L";
42   PrivateLabelPrefix = PrivateGlobalPrefix;
43   LinkerPrivateGlobalPrefix = "";
44   InlineAsmStart = "APP";
45   InlineAsmEnd = "NO_APP";
46   Code16Directive = ".code16";
47   Code32Directive = ".code32";
48   Code64Directive = ".code64";
49   ZeroDirective = "\t.zero\t";
50   AsciiDirective = "\t.ascii\t";
51   AscizDirective = "\t.asciz\t";
52   Data8bitsDirective = "\t.byte\t";
53   Data16bitsDirective = "\t.short\t";
54   Data32bitsDirective = "\t.long\t";
55   Data64bitsDirective = "\t.quad\t";
56   GlobalDirective = "\t.globl\t";
57   WeakDirective = "\t.weak\t";
58   if (DwarfExtendedLoc != Default)
59     SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable;
60   if (UseLEB128Directives != cl::BOU_UNSET)
61     HasLEB128Directives = UseLEB128Directives == cl::BOU_TRUE;
62 
63   // FIXME: Clang's logic should be synced with the logic used to initialize
64   //        this member and the two implementations should be merged.
65   // For reference:
66   // - Solaris always enables the integrated assembler by default
67   //   - SparcELFMCAsmInfo and X86ELFMCAsmInfo are handling this case
68   // - Windows always enables the integrated assembler by default
69   //   - MCAsmInfoCOFF is handling this case, should it be MCAsmInfoMicrosoft?
70   // - MachO targets always enables the integrated assembler by default
71   //   - MCAsmInfoDarwin is handling this case
72   // - Generic_GCC toolchains enable the integrated assembler on a per
73   //   architecture basis.
74   //   - The target subclasses for AArch64, ARM, and X86 handle these cases
75   UseIntegratedAssembler = true;
76   PreserveAsmComments = true;
77 }
78 
79 MCAsmInfo::~MCAsmInfo() = default;
80 
addInitialFrameState(const MCCFIInstruction & Inst)81 void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
82   InitialFrameState.push_back(Inst);
83 }
84 
isSectionAtomizableBySymbols(const MCSection & Section) const85 bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
86   return false;
87 }
88 
89 const MCExpr *
getExprForPersonalitySymbol(const MCSymbol * Sym,unsigned Encoding,MCStreamer & Streamer) const90 MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
91                                        unsigned Encoding,
92                                        MCStreamer &Streamer) const {
93   return getExprForFDESymbol(Sym, Encoding, Streamer);
94 }
95 
96 const MCExpr *
getExprForFDESymbol(const MCSymbol * Sym,unsigned Encoding,MCStreamer & Streamer) const97 MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
98                                unsigned Encoding,
99                                MCStreamer &Streamer) const {
100   if (!(Encoding & dwarf::DW_EH_PE_pcrel))
101     return MCSymbolRefExpr::create(Sym, Streamer.getContext());
102 
103   MCContext &Context = Streamer.getContext();
104   const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
105   MCSymbol *PCSym = Context.createTempSymbol();
106   Streamer.emitLabel(PCSym);
107   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
108   return MCBinaryExpr::createSub(Res, PC, Context);
109 }
110 
isAcceptableChar(char C) const111 bool MCAsmInfo::isAcceptableChar(char C) const {
112   return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
113          (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
114 }
115 
isValidUnquotedName(StringRef Name) const116 bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
117   if (Name.empty())
118     return false;
119 
120   // If any of the characters in the string is an unacceptable character, force
121   // quotes.
122   for (char C : Name) {
123     if (!isAcceptableChar(C))
124       return false;
125   }
126 
127   return true;
128 }
129 
shouldOmitSectionDirective(StringRef SectionName) const130 bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
131   // FIXME: Does .section .bss/.data/.text work everywhere??
132   return SectionName == ".text" || SectionName == ".data" ||
133         (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
134 }
135