MCAsmInfo.cpp 4.7 KB

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