xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCAsmInfo.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- MCAsmInfo.cpp - Asm Info -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines target asm properties related what form asm statements
100b57cec5SDimitry Andric // should take.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
150b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
190b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric enum DefaultOnOff { Default, Enable, Disable };
240b57cec5SDimitry Andric static cl::opt<DefaultOnOff> DwarfExtendedLoc(
250b57cec5SDimitry Andric     "dwarf-extended-loc", cl::Hidden,
260b57cec5SDimitry Andric     cl::desc("Disable emission of the extended flags in .loc directives."),
270b57cec5SDimitry Andric     cl::values(clEnumVal(Default, "Default for platform"),
280b57cec5SDimitry Andric                clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
290b57cec5SDimitry Andric     cl::init(Default));
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric MCAsmInfo::MCAsmInfo() {
320b57cec5SDimitry Andric   SeparatorString = ";";
330b57cec5SDimitry Andric   CommentString = "#";
340b57cec5SDimitry Andric   LabelSuffix = ":";
350b57cec5SDimitry Andric   PrivateGlobalPrefix = "L";
360b57cec5SDimitry Andric   PrivateLabelPrefix = PrivateGlobalPrefix;
370b57cec5SDimitry Andric   LinkerPrivateGlobalPrefix = "";
380b57cec5SDimitry Andric   InlineAsmStart = "APP";
390b57cec5SDimitry Andric   InlineAsmEnd = "NO_APP";
400b57cec5SDimitry Andric   Code16Directive = ".code16";
410b57cec5SDimitry Andric   Code32Directive = ".code32";
420b57cec5SDimitry Andric   Code64Directive = ".code64";
430b57cec5SDimitry Andric   ZeroDirective = "\t.zero\t";
440b57cec5SDimitry Andric   AsciiDirective = "\t.ascii\t";
450b57cec5SDimitry Andric   AscizDirective = "\t.asciz\t";
460b57cec5SDimitry Andric   Data8bitsDirective = "\t.byte\t";
470b57cec5SDimitry Andric   Data16bitsDirective = "\t.short\t";
480b57cec5SDimitry Andric   Data32bitsDirective = "\t.long\t";
490b57cec5SDimitry Andric   Data64bitsDirective = "\t.quad\t";
500b57cec5SDimitry Andric   GlobalDirective = "\t.globl\t";
510b57cec5SDimitry Andric   WeakDirective = "\t.weak\t";
520b57cec5SDimitry Andric   if (DwarfExtendedLoc != Default)
530b57cec5SDimitry Andric     SupportsExtendedDwarfLocDirective = DwarfExtendedLoc == Enable;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // FIXME: Clang's logic should be synced with the logic used to initialize
560b57cec5SDimitry Andric   //        this member and the two implementations should be merged.
570b57cec5SDimitry Andric   // For reference:
580b57cec5SDimitry Andric   // - Solaris always enables the integrated assembler by default
590b57cec5SDimitry Andric   //   - SparcELFMCAsmInfo and X86ELFMCAsmInfo are handling this case
600b57cec5SDimitry Andric   // - Windows always enables the integrated assembler by default
610b57cec5SDimitry Andric   //   - MCAsmInfoCOFF is handling this case, should it be MCAsmInfoMicrosoft?
620b57cec5SDimitry Andric   // - MachO targets always enables the integrated assembler by default
630b57cec5SDimitry Andric   //   - MCAsmInfoDarwin is handling this case
640b57cec5SDimitry Andric   // - Generic_GCC toolchains enable the integrated assembler on a per
650b57cec5SDimitry Andric   //   architecture basis.
660b57cec5SDimitry Andric   //   - The target subclasses for AArch64, ARM, and X86 handle these cases
67*5ffd83dbSDimitry Andric   UseIntegratedAssembler = true;
680b57cec5SDimitry Andric   PreserveAsmComments = true;
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric MCAsmInfo::~MCAsmInfo() = default;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
740b57cec5SDimitry Andric   InitialFrameState.push_back(Inst);
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
780b57cec5SDimitry Andric   return false;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric const MCExpr *
820b57cec5SDimitry Andric MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
830b57cec5SDimitry Andric                                        unsigned Encoding,
840b57cec5SDimitry Andric                                        MCStreamer &Streamer) const {
850b57cec5SDimitry Andric   return getExprForFDESymbol(Sym, Encoding, Streamer);
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric const MCExpr *
890b57cec5SDimitry Andric MCAsmInfo::getExprForFDESymbol(const MCSymbol *Sym,
900b57cec5SDimitry Andric                                unsigned Encoding,
910b57cec5SDimitry Andric                                MCStreamer &Streamer) const {
920b57cec5SDimitry Andric   if (!(Encoding & dwarf::DW_EH_PE_pcrel))
930b57cec5SDimitry Andric     return MCSymbolRefExpr::create(Sym, Streamer.getContext());
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   MCContext &Context = Streamer.getContext();
960b57cec5SDimitry Andric   const MCExpr *Res = MCSymbolRefExpr::create(Sym, Context);
970b57cec5SDimitry Andric   MCSymbol *PCSym = Context.createTempSymbol();
98*5ffd83dbSDimitry Andric   Streamer.emitLabel(PCSym);
990b57cec5SDimitry Andric   const MCExpr *PC = MCSymbolRefExpr::create(PCSym, Context);
1000b57cec5SDimitry Andric   return MCBinaryExpr::createSub(Res, PC, Context);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
103480093f4SDimitry Andric bool MCAsmInfo::isAcceptableChar(char C) const {
1040b57cec5SDimitry Andric   return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
1050b57cec5SDimitry Andric          (C >= '0' && C <= '9') || C == '_' || C == '$' || C == '.' || C == '@';
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
1090b57cec5SDimitry Andric   if (Name.empty())
1100b57cec5SDimitry Andric     return false;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // If any of the characters in the string is an unacceptable character, force
1130b57cec5SDimitry Andric   // quotes.
1140b57cec5SDimitry Andric   for (char C : Name) {
1150b57cec5SDimitry Andric     if (!isAcceptableChar(C))
1160b57cec5SDimitry Andric       return false;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   return true;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
1230b57cec5SDimitry Andric   // FIXME: Does .section .bss/.data/.text work everywhere??
1240b57cec5SDimitry Andric   return SectionName == ".text" || SectionName == ".data" ||
1250b57cec5SDimitry Andric         (SectionName == ".bss" && !usesELFSectionDirectiveForBSS());
1260b57cec5SDimitry Andric }
127