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