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 
MCAsmInfo()31 MCAsmInfo::MCAsmInfo() {
32   SeparatorString = ";";
33   CommentString = "#";
34   LabelSuffix = ":";
35   PrivateGlobalPrefix = "L";
36   PrivateLabelPrefix = PrivateGlobalPrefix;
37   LinkerPrivateGlobalPrefix = "";
38   InlineAsmStart = "APP";
39   InlineAsmEnd = "NO_APP";
40   Code16Directive = ".code16";
41   Code32Directive = ".code32";
42   Code64Directive = ".code64";
43   ZeroDirective = "\t.zero\t";
44   AsciiDirective = "\t.ascii\t";
45   AscizDirective = "\t.asciz\t";
46   Data8bitsDirective = "\t.byte\t";
47   Data16bitsDirective = "\t.short\t";
48   Data32bitsDirective = "\t.long\t";
49   Data64bitsDirective = "\t.quad\t";
50   GlobalDirective = "\t.globl\t";
51   WeakDirective = "\t.weak\t";
52   if (DwarfExtendedLoc != Default)
53     SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable;
54 
55   // FIXME: Clang's logic should be synced with the logic used to initialize
56   //        this member and the two implementations should be merged.
57   // For reference:
58   // - Solaris always enables the integrated assembler by default
59   //   - SparcELFMCAsmInfo and X86ELFMCAsmInfo are handling this case
60   // - Windows always enables the integrated assembler by default
61   //   - MCAsmInfoCOFF is handling this case, should it be MCAsmInfoMicrosoft?
62   // - MachO targets always enables the integrated assembler by default
63   //   - MCAsmInfoDarwin is handling this case
64   // - Generic_GCC toolchains enable the integrated assembler on a per
65   //   architecture basis.
66   //   - The target subclasses for AArch64, ARM, and X86 handle these cases
67   UseIntegratedAssembler = false;
68   PreserveAsmComments = true;
69 }
70 
71 MCAsmInfo::~MCAsmInfo() = default;
72 
addInitialFrameState(const MCCFIInstruction & Inst)73 void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
74   InitialFrameState.push_back(Inst);
75 }
76 
isSectionAtomizableBySymbols(const MCSection & Section) const77 bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
78   return false;
79 }
80 
81 const MCExpr *
getExprForPersonalitySymbol(const MCSymbol * Sym,unsigned Encoding,MCStreamer & Streamer) const82 MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
83                                        unsigned Encoding,
84                                        MCStreamer &Streamer) const {
85   return getExprForFDESymbol(Sym, Encoding, Streamer);
86 }
87 
88 const MCExpr *
getExprForFDESymbol(const MCSymbol * Sym,unsigned Encoding,MCStreamer & Streamer) const89 MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
90                                unsigned Encoding,
91                                MCStreamer &Streamer) const {
92   if (!(Encoding & dwarf::DW_EH_PE_pcrel))
93     return MCSymbolRefExpr::create(Sym, Streamer.getContext());
94 
95   MCContext &Context = Streamer.getContext();
96   const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
97   MCSymbol *PCSym = Context.createTempSymbol();
98   Streamer.EmitLabel(PCSym);
99   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
100   return MCBinaryExpr::createSub(Res, PC, Context);
101 }
102 
isAcceptableChar(char C) const103 bool MCAsmInfo::isAcceptableChar(char C) const {
104   return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
105          (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
106 }
107 
isValidUnquotedName(StringRef Name) const108 bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
109   if (Name.empty())
110     return false;
111 
112   // If any of the characters in the string is an unacceptable character, force
113   // quotes.
114   for (char C : Name) {
115     if (!isAcceptableChar(C))
116       return false;
117   }
118 
119   return true;
120 }
121 
shouldOmitSectionDirective(StringRef SectionName) const122 bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
123   // FIXME: Does .section .bss/.data/.text work everywhere??
124   return SectionName == ".text" || SectionName == ".data" ||
125         (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
126 }
127