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