xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSectionWasm.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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 
9 #include "llvm/MC/MCSectionWasm.h"
10 #include "llvm/MC/MCAsmInfo.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/MC/MCSymbolWasm.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 using namespace llvm;
17 
18 // Decides whether a '.section' directive
19 // should be printed before the section name.
20 bool MCSectionWasm::shouldOmitSectionDirective(StringRef Name,
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 
52   if (shouldOmitSectionDirective(getName(), MAI)) {
53     OS << '\t' << getName();
54     if (Subsection) {
55       OS << '\t';
56       Subsection->print(OS, &MAI);
57     }
58     OS << '\n';
59     return;
60   }
61 
62   OS << "\t.section\t";
63   printName(OS, getName());
64   OS << ",\"";
65 
66   if (IsPassive)
67     OS << "p";
68   if (Group)
69     OS << "G";
70 
71   OS << '"';
72 
73   OS << ',';
74 
75   // If comment string is '@', e.g. as on ARM - use '%' instead
76   if (MAI.getCommentString()[0] == '@')
77     OS << '%';
78   else
79     OS << '@';
80 
81   // TODO: Print section type.
82 
83   if (Group) {
84     OS << ",";
85     printName(OS, Group->getName());
86     OS << ",comdat";
87   }
88 
89   if (isUnique())
90     OS << ",unique," << UniqueID;
91 
92   OS << '\n';
93 
94   if (Subsection) {
95     OS << "\t.subsection\t";
96     Subsection->print(OS, &MAI);
97     OS << '\n';
98   }
99 }
100 
101 bool MCSectionWasm::UseCodeAlign() const { return false; }
102 
103 bool MCSectionWasm::isVirtualSection() const { return false; }
104