1//===-- MCAsmInfoWasm.cpp - Wasm asm properties -----------------*- C++ -*-===//
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// This file defines target asm properties related what form asm statements
10// should take in general on Wasm-based targets
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/MCAsmInfoWasm.h"
15#include "llvm/MC/MCSectionWasm.h"
16#include "llvm/MC/MCSymbolWasm.h"
17#include "llvm/Support/raw_ostream.h"
18
19using namespace llvm;
20
21MCAsmInfoWasm::MCAsmInfoWasm() {
22 HasIdentDirective = true;
23 HasNoDeadStrip = true;
24 WeakRefDirective = "\t.weak\t";
25 PrivateGlobalPrefix = ".L";
26 PrivateLabelPrefix = ".L";
27}
28
29static void printName(raw_ostream &OS, StringRef Name) {
30 if (Name.find_first_not_of(Chars: "0123456789_."
31 "abcdefghijklmnopqrstuvwxyz"
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
33 OS << Name;
34 return;
35 }
36 OS << '"';
37 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
38 if (*B == '"') // Unquoted "
39 OS << "\\\"";
40 else if (*B != '\\') // Neither " or backslash
41 OS << *B;
42 else if (B + 1 == E) // Trailing backslash
43 OS << "\\\\";
44 else {
45 OS << B[0] << B[1]; // Quoted character
46 ++B;
47 }
48 }
49 OS << '"';
50}
51
52void MCAsmInfoWasm::printSwitchToSection(const MCSection &Section,
53 uint32_t Subsection, const Triple &T,
54 raw_ostream &OS) const {
55 auto &Sec = static_cast<const MCSectionWasm &>(Section);
56 if (shouldOmitSectionDirective(SectionName: Sec.getName())) {
57 OS << '\t' << Sec.getName();
58 if (Subsection)
59 OS << '\t' << Subsection;
60 OS << '\n';
61 return;
62 }
63
64 OS << "\t.section\t";
65 printName(OS, Name: Sec.getName());
66 OS << ",\"";
67
68 if (Sec.IsPassive)
69 OS << 'p';
70 if (Sec.Group)
71 OS << 'G';
72 if (Sec.SegmentFlags & wasm::WASM_SEG_FLAG_STRINGS)
73 OS << 'S';
74 if (Sec.SegmentFlags & wasm::WASM_SEG_FLAG_TLS)
75 OS << 'T';
76 if (Sec.SegmentFlags & wasm::WASM_SEG_FLAG_RETAIN)
77 OS << 'R';
78
79 OS << '"';
80
81 OS << ',';
82
83 // If comment string is '@', e.g. as on ARM - use '%' instead
84 if (getCommentString()[0] == '@')
85 OS << '%';
86 else
87 OS << '@';
88
89 // TODO: Print section type.
90
91 if (Sec.Group) {
92 OS << ",";
93 printName(OS, Name: Sec.Group->getName());
94 OS << ",comdat";
95 }
96
97 if (Sec.isUnique())
98 OS << ",unique," << Sec.UniqueID;
99
100 OS << '\n';
101
102 if (Subsection)
103 OS << "\t.subsection\t" << Subsection << '\n';
104}
105