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