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