MCSectionWasm.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===- lib/MC/MCSectionWasm.cpp - Wasm Code Section Representation --------===//
  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. #include "llvm/MC/MCSectionWasm.h"
  9. #include "llvm/MC/MCAsmInfo.h"
  10. #include "llvm/MC/MCExpr.h"
  11. #include "llvm/MC/MCSymbol.h"
  12. #include "llvm/MC/MCSymbolWasm.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace llvm;
  15. // Decides whether a '.section' directive
  16. // should be printed before the section name.
  17. bool MCSectionWasm::shouldOmitSectionDirective(StringRef Name,
  18. const MCAsmInfo &MAI) const {
  19. return MAI.shouldOmitSectionDirective(Name);
  20. }
  21. static void printName(raw_ostream &OS, StringRef Name) {
  22. if (Name.find_first_not_of("0123456789_."
  23. "abcdefghijklmnopqrstuvwxyz"
  24. "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
  25. OS << Name;
  26. return;
  27. }
  28. OS << '"';
  29. for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
  30. if (*B == '"') // Unquoted "
  31. OS << "\\\"";
  32. else if (*B != '\\') // Neither " or backslash
  33. OS << *B;
  34. else if (B + 1 == E) // Trailing backslash
  35. OS << "\\\\";
  36. else {
  37. OS << B[0] << B[1]; // Quoted character
  38. ++B;
  39. }
  40. }
  41. OS << '"';
  42. }
  43. void MCSectionWasm::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
  44. raw_ostream &OS,
  45. const MCExpr *Subsection) const {
  46. if (shouldOmitSectionDirective(getName(), MAI)) {
  47. OS << '\t' << getName();
  48. if (Subsection) {
  49. OS << '\t';
  50. Subsection->print(OS, &MAI);
  51. }
  52. OS << '\n';
  53. return;
  54. }
  55. OS << "\t.section\t";
  56. printName(OS, getName());
  57. OS << ",\"";
  58. if (IsPassive)
  59. OS << 'p';
  60. if (Group)
  61. OS << 'G';
  62. if (SegmentFlags & wasm::WASM_SEG_FLAG_STRINGS)
  63. OS << 'S';
  64. if (SegmentFlags & wasm::WASM_SEG_FLAG_TLS)
  65. OS << 'T';
  66. OS << '"';
  67. OS << ',';
  68. // If comment string is '@', e.g. as on ARM - use '%' instead
  69. if (MAI.getCommentString()[0] == '@')
  70. OS << '%';
  71. else
  72. OS << '@';
  73. // TODO: Print section type.
  74. if (Group) {
  75. OS << ",";
  76. printName(OS, Group->getName());
  77. OS << ",comdat";
  78. }
  79. if (isUnique())
  80. OS << ",unique," << UniqueID;
  81. OS << '\n';
  82. if (Subsection) {
  83. OS << "\t.subsection\t";
  84. Subsection->print(OS, &MAI);
  85. OS << '\n';
  86. }
  87. }
  88. bool MCSectionWasm::UseCodeAlign() const { return false; }
  89. bool MCSectionWasm::isVirtualSection() const { return false; }