xref: /openbsd/gnu/llvm/llvm/lib/MC/MCSectionWasm.cpp (revision d415bd75)
109467b48Spatrick //===- lib/MC/MCSectionWasm.cpp - Wasm Code Section Representation --------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick 
909467b48Spatrick #include "llvm/MC/MCSectionWasm.h"
1009467b48Spatrick #include "llvm/MC/MCAsmInfo.h"
1109467b48Spatrick #include "llvm/MC/MCExpr.h"
12097a140dSpatrick #include "llvm/MC/MCSymbolWasm.h"
1309467b48Spatrick #include "llvm/Support/raw_ostream.h"
1409467b48Spatrick 
1509467b48Spatrick using namespace llvm;
1609467b48Spatrick 
1709467b48Spatrick // Decides whether a '.section' directive
1809467b48Spatrick // should be printed before the section name.
shouldOmitSectionDirective(StringRef Name,const MCAsmInfo & MAI) const1909467b48Spatrick bool MCSectionWasm::shouldOmitSectionDirective(StringRef Name,
2009467b48Spatrick                                                const MCAsmInfo &MAI) const {
2109467b48Spatrick   return MAI.shouldOmitSectionDirective(Name);
2209467b48Spatrick }
2309467b48Spatrick 
printName(raw_ostream & OS,StringRef Name)2409467b48Spatrick static void printName(raw_ostream &OS, StringRef Name) {
2509467b48Spatrick   if (Name.find_first_not_of("0123456789_."
2609467b48Spatrick                              "abcdefghijklmnopqrstuvwxyz"
2709467b48Spatrick                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
2809467b48Spatrick     OS << Name;
2909467b48Spatrick     return;
3009467b48Spatrick   }
3109467b48Spatrick   OS << '"';
3209467b48Spatrick   for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
3309467b48Spatrick     if (*B == '"') // Unquoted "
3409467b48Spatrick       OS << "\\\"";
3509467b48Spatrick     else if (*B != '\\') // Neither " or backslash
3609467b48Spatrick       OS << *B;
3709467b48Spatrick     else if (B + 1 == E) // Trailing backslash
3809467b48Spatrick       OS << "\\\\";
3909467b48Spatrick     else {
4009467b48Spatrick       OS << B[0] << B[1]; // Quoted character
4109467b48Spatrick       ++B;
4209467b48Spatrick     }
4309467b48Spatrick   }
4409467b48Spatrick   OS << '"';
4509467b48Spatrick }
4609467b48Spatrick 
printSwitchToSection(const MCAsmInfo & MAI,const Triple & T,raw_ostream & OS,const MCExpr * Subsection) const47*d415bd75Srobert void MCSectionWasm::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
4809467b48Spatrick                                          raw_ostream &OS,
4909467b48Spatrick                                          const MCExpr *Subsection) const {
5009467b48Spatrick 
51097a140dSpatrick   if (shouldOmitSectionDirective(getName(), MAI)) {
52097a140dSpatrick     OS << '\t' << getName();
5309467b48Spatrick     if (Subsection) {
5409467b48Spatrick       OS << '\t';
5509467b48Spatrick       Subsection->print(OS, &MAI);
5609467b48Spatrick     }
5709467b48Spatrick     OS << '\n';
5809467b48Spatrick     return;
5909467b48Spatrick   }
6009467b48Spatrick 
6109467b48Spatrick   OS << "\t.section\t";
62097a140dSpatrick   printName(OS, getName());
6309467b48Spatrick   OS << ",\"";
6409467b48Spatrick 
6509467b48Spatrick   if (IsPassive)
6673471bf0Spatrick     OS << 'p';
6773471bf0Spatrick   if (Group)
6873471bf0Spatrick     OS << 'G';
6973471bf0Spatrick   if (SegmentFlags & wasm::WASM_SEG_FLAG_STRINGS)
7073471bf0Spatrick     OS << 'S';
7173471bf0Spatrick   if (SegmentFlags & wasm::WASM_SEG_FLAG_TLS)
7273471bf0Spatrick     OS << 'T';
7309467b48Spatrick 
7409467b48Spatrick   OS << '"';
7509467b48Spatrick 
7609467b48Spatrick   OS << ',';
7709467b48Spatrick 
7809467b48Spatrick   // If comment string is '@', e.g. as on ARM - use '%' instead
7909467b48Spatrick   if (MAI.getCommentString()[0] == '@')
8009467b48Spatrick     OS << '%';
8109467b48Spatrick   else
8209467b48Spatrick     OS << '@';
8309467b48Spatrick 
8409467b48Spatrick   // TODO: Print section type.
8509467b48Spatrick 
8673471bf0Spatrick   if (Group) {
8773471bf0Spatrick     OS << ",";
8873471bf0Spatrick     printName(OS, Group->getName());
8973471bf0Spatrick     OS << ",comdat";
9073471bf0Spatrick   }
9173471bf0Spatrick 
9209467b48Spatrick   if (isUnique())
9309467b48Spatrick     OS << ",unique," << UniqueID;
9409467b48Spatrick 
9509467b48Spatrick   OS << '\n';
9609467b48Spatrick 
9709467b48Spatrick   if (Subsection) {
9809467b48Spatrick     OS << "\t.subsection\t";
9909467b48Spatrick     Subsection->print(OS, &MAI);
10009467b48Spatrick     OS << '\n';
10109467b48Spatrick   }
10209467b48Spatrick }
10309467b48Spatrick 
useCodeAlign() const104*d415bd75Srobert bool MCSectionWasm::useCodeAlign() const { return false; }
10509467b48Spatrick 
isVirtualSection() const10609467b48Spatrick bool MCSectionWasm::isVirtualSection() const { return false; }
107