MCSectionWasm.cpp 2.7 KB

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