MCAsmInfo.cpp 4.8 KB

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