10b57cec5SDimitry Andric //===- lib/MC/MCSectionWasm.cpp - Wasm Code Section Representation --------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h"
100b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
110b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
125ffd83dbSDimitry Andric #include "llvm/MC/MCSymbolWasm.h"
130b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric // Decides whether a '.section' directive
180b57cec5SDimitry Andric // should be printed before the section name.
shouldOmitSectionDirective(StringRef Name,const MCAsmInfo & MAI) const190b57cec5SDimitry Andric bool MCSectionWasm::shouldOmitSectionDirective(StringRef Name,
200b57cec5SDimitry Andric const MCAsmInfo &MAI) const {
210b57cec5SDimitry Andric return MAI.shouldOmitSectionDirective(Name);
220b57cec5SDimitry Andric }
230b57cec5SDimitry Andric
printName(raw_ostream & OS,StringRef Name)240b57cec5SDimitry Andric static void printName(raw_ostream &OS, StringRef Name) {
250b57cec5SDimitry Andric if (Name.find_first_not_of("0123456789_."
260b57cec5SDimitry Andric "abcdefghijklmnopqrstuvwxyz"
270b57cec5SDimitry Andric "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
280b57cec5SDimitry Andric OS << Name;
290b57cec5SDimitry Andric return;
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric OS << '"';
320b57cec5SDimitry Andric for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
330b57cec5SDimitry Andric if (*B == '"') // Unquoted "
340b57cec5SDimitry Andric OS << "\\\"";
350b57cec5SDimitry Andric else if (*B != '\\') // Neither " or backslash
360b57cec5SDimitry Andric OS << *B;
370b57cec5SDimitry Andric else if (B + 1 == E) // Trailing backslash
380b57cec5SDimitry Andric OS << "\\\\";
390b57cec5SDimitry Andric else {
400b57cec5SDimitry Andric OS << B[0] << B[1]; // Quoted character
410b57cec5SDimitry Andric ++B;
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric OS << '"';
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric
printSwitchToSection(const MCAsmInfo & MAI,const Triple & T,raw_ostream & OS,uint32_t Subsection) const4781ad6265SDimitry Andric void MCSectionWasm::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
480b57cec5SDimitry Andric raw_ostream &OS,
49*0fca6ea1SDimitry Andric uint32_t Subsection) const {
500b57cec5SDimitry Andric
515ffd83dbSDimitry Andric if (shouldOmitSectionDirective(getName(), MAI)) {
525ffd83dbSDimitry Andric OS << '\t' << getName();
53*0fca6ea1SDimitry Andric if (Subsection)
54*0fca6ea1SDimitry Andric OS << '\t' << Subsection;
550b57cec5SDimitry Andric OS << '\n';
560b57cec5SDimitry Andric return;
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric OS << "\t.section\t";
605ffd83dbSDimitry Andric printName(OS, getName());
610b57cec5SDimitry Andric OS << ",\"";
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric if (IsPassive)
64fe6060f1SDimitry Andric OS << 'p';
65e8d8bef9SDimitry Andric if (Group)
66fe6060f1SDimitry Andric OS << 'G';
67fe6060f1SDimitry Andric if (SegmentFlags & wasm::WASM_SEG_FLAG_STRINGS)
68fe6060f1SDimitry Andric OS << 'S';
69fe6060f1SDimitry Andric if (SegmentFlags & wasm::WASM_SEG_FLAG_TLS)
70fe6060f1SDimitry Andric OS << 'T';
71*0fca6ea1SDimitry Andric if (SegmentFlags & wasm::WASM_SEG_FLAG_RETAIN)
72*0fca6ea1SDimitry Andric OS << 'R';
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric OS << '"';
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric OS << ',';
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric // If comment string is '@', e.g. as on ARM - use '%' instead
790b57cec5SDimitry Andric if (MAI.getCommentString()[0] == '@')
800b57cec5SDimitry Andric OS << '%';
810b57cec5SDimitry Andric else
820b57cec5SDimitry Andric OS << '@';
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // TODO: Print section type.
850b57cec5SDimitry Andric
86e8d8bef9SDimitry Andric if (Group) {
87e8d8bef9SDimitry Andric OS << ",";
88e8d8bef9SDimitry Andric printName(OS, Group->getName());
89e8d8bef9SDimitry Andric OS << ",comdat";
90e8d8bef9SDimitry Andric }
91e8d8bef9SDimitry Andric
920b57cec5SDimitry Andric if (isUnique())
930b57cec5SDimitry Andric OS << ",unique," << UniqueID;
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric OS << '\n';
960b57cec5SDimitry Andric
97*0fca6ea1SDimitry Andric if (Subsection)
98*0fca6ea1SDimitry Andric OS << "\t.subsection\t" << Subsection << '\n';
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric
useCodeAlign() const10181ad6265SDimitry Andric bool MCSectionWasm::useCodeAlign() const { return false; }
102