xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSectionWasm.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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"
120b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
13*5ffd83dbSDimitry Andric #include "llvm/MC/MCSymbolWasm.h"
140b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric // Decides whether a '.section' directive
190b57cec5SDimitry Andric // should be printed before the section name.
200b57cec5SDimitry Andric bool MCSectionWasm::shouldOmitSectionDirective(StringRef Name,
210b57cec5SDimitry Andric                                                const MCAsmInfo &MAI) const {
220b57cec5SDimitry Andric   return MAI.shouldOmitSectionDirective(Name);
230b57cec5SDimitry Andric }
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric static void printName(raw_ostream &OS, StringRef Name) {
260b57cec5SDimitry Andric   if (Name.find_first_not_of("0123456789_."
270b57cec5SDimitry Andric                              "abcdefghijklmnopqrstuvwxyz"
280b57cec5SDimitry Andric                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
290b57cec5SDimitry Andric     OS << Name;
300b57cec5SDimitry Andric     return;
310b57cec5SDimitry Andric   }
320b57cec5SDimitry Andric   OS << '"';
330b57cec5SDimitry Andric   for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
340b57cec5SDimitry Andric     if (*B == '"') // Unquoted "
350b57cec5SDimitry Andric       OS << "\\\"";
360b57cec5SDimitry Andric     else if (*B != '\\') // Neither " or backslash
370b57cec5SDimitry Andric       OS << *B;
380b57cec5SDimitry Andric     else if (B + 1 == E) // Trailing backslash
390b57cec5SDimitry Andric       OS << "\\\\";
400b57cec5SDimitry Andric     else {
410b57cec5SDimitry Andric       OS << B[0] << B[1]; // Quoted character
420b57cec5SDimitry Andric       ++B;
430b57cec5SDimitry Andric     }
440b57cec5SDimitry Andric   }
450b57cec5SDimitry Andric   OS << '"';
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric void MCSectionWasm::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
490b57cec5SDimitry Andric                                          raw_ostream &OS,
500b57cec5SDimitry Andric                                          const MCExpr *Subsection) const {
510b57cec5SDimitry Andric 
52*5ffd83dbSDimitry Andric   if (shouldOmitSectionDirective(getName(), MAI)) {
53*5ffd83dbSDimitry Andric     OS << '\t' << getName();
540b57cec5SDimitry Andric     if (Subsection) {
550b57cec5SDimitry Andric       OS << '\t';
560b57cec5SDimitry Andric       Subsection->print(OS, &MAI);
570b57cec5SDimitry Andric     }
580b57cec5SDimitry Andric     OS << '\n';
590b57cec5SDimitry Andric     return;
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   OS << "\t.section\t";
63*5ffd83dbSDimitry Andric   printName(OS, getName());
640b57cec5SDimitry Andric   OS << ",\"";
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   if (IsPassive)
670b57cec5SDimitry Andric     OS << "passive";
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   OS << '"';
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   OS << ',';
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // If comment string is '@', e.g. as on ARM - use '%' instead
740b57cec5SDimitry Andric   if (MAI.getCommentString()[0] == '@')
750b57cec5SDimitry Andric     OS << '%';
760b57cec5SDimitry Andric   else
770b57cec5SDimitry Andric     OS << '@';
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // TODO: Print section type.
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   if (isUnique())
820b57cec5SDimitry Andric     OS << ",unique," << UniqueID;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   OS << '\n';
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   if (Subsection) {
870b57cec5SDimitry Andric     OS << "\t.subsection\t";
880b57cec5SDimitry Andric     Subsection->print(OS, &MAI);
890b57cec5SDimitry Andric     OS << '\n';
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric bool MCSectionWasm::UseCodeAlign() const { return false; }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric bool MCSectionWasm::isVirtualSection() const { return false; }
96