Home | History | Annotate | Line # | Download | only in MC
      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   PreserveAsmComments = true;
     81 }
     82 
     83 MCAsmInfo::~MCAsmInfo() = default;
     84 
     85 void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
     86   InitialFrameState.push_back(Inst);
     87 }
     88 
     89 bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
     90   return false;
     91 }
     92 
     93 const MCExpr *
     94 MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
     95                                        unsigned Encoding,
     96                                        MCStreamer &Streamer) const {
     97   return getExprForFDESymbol(Sym, Encoding, Streamer);
     98 }
     99 
    100 const MCExpr *
    101 MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
    102                                unsigned Encoding,
    103                                MCStreamer &Streamer) const {
    104   if (!(Encoding & dwarf::DW_EH_PE_pcrel))
    105     return MCSymbolRefExpr::create(Sym, Streamer.getContext());
    106 
    107   MCContext &Context = Streamer.getContext();
    108   const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
    109   MCSymbol *PCSym = Context.createTempSymbol();
    110   Streamer.emitLabel(PCSym);
    111   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
    112   return MCBinaryExpr::createSub(Res, PC, Context);
    113 }
    114 
    115 bool MCAsmInfo::isAcceptableChar(char C) const {
    116   return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '@';
    117 }
    118 
    119 bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
    120   if (Name.empty())
    121     return false;
    122 
    123   // If any of the characters in the string is an unacceptable character, force
    124   // quotes.
    125   for (char C : Name) {
    126     if (!isAcceptableChar(C))
    127       return false;
    128   }
    129 
    130   return true;
    131 }
    132 
    133 bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
    134   // FIXME: Does .section .bss/.data/.text work everywhere??
    135   return SectionName == ".text" || SectionName == ".data" ||
    136         (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
    137 }
    138