1 //===- lib/MC/MCSectionWasm.cpp - Wasm Code Section Representation --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/MC/MCSectionWasm.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 using namespace llvm;
18 
19 MCSectionWasm::~MCSectionWasm() {} // anchor.
20 
21 // Decides whether a '.section' directive
22 // should be printed before the section name.
23 bool MCSectionWasm::ShouldOmitSectionDirective(StringRef Name,
24                                                const MCAsmInfo &MAI) const {
25   return MAI.shouldOmitSectionDirective(Name);
26 }
27 
28 static void printName(raw_ostream &OS, StringRef Name) {
29   if (Name.find_first_not_of("0123456789_."
30                              "abcdefghijklmnopqrstuvwxyz"
31                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
32     OS << Name;
33     return;
34   }
35   OS << '"';
36   for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
37     if (*B == '"') // Unquoted "
38       OS << "\\\"";
39     else if (*B != '\\') // Neither " or backslash
40       OS << *B;
41     else if (B + 1 == E) // Trailing backslash
42       OS << "\\\\";
43     else {
44       OS << B[0] << B[1]; // Quoted character
45       ++B;
46     }
47   }
48   OS << '"';
49 }
50 
51 void MCSectionWasm::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
52                                          raw_ostream &OS,
53                                          const MCExpr *Subsection) const {
54 
55   if (ShouldOmitSectionDirective(SectionName, MAI)) {
56     OS << '\t' << getSectionName();
57     if (Subsection) {
58       OS << '\t';
59       Subsection->print(OS, &MAI);
60     }
61     OS << '\n';
62     return;
63   }
64 
65   OS << "\t.section\t";
66   printName(OS, getSectionName());
67   OS << ",\"";
68 
69   // TODO: Print section flags.
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 (isUnique())
84     OS << ",unique," << UniqueID;
85 
86   OS << '\n';
87 
88   if (Subsection) {
89     OS << "\t.subsection\t";
90     Subsection->print(OS, &MAI);
91     OS << '\n';
92   }
93 }
94 
95 bool MCSectionWasm::UseCodeAlign() const { return false; }
96 
97 bool MCSectionWasm::isVirtualSection() const { return false; }
98