1 //==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=//
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 /// \file
11 /// This file defines WebAssembly-specific target streamer classes.
12 /// These are for implementing support for target-specific assembly directives.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "WebAssemblyTargetStreamer.h"
17 #include "InstPrinter/WebAssemblyInstPrinter.h"
18 #include "WebAssemblyMCTargetDesc.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSectionWasm.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbolWasm.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm;
27
WebAssemblyTargetStreamer(MCStreamer & S)28 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
29 : MCTargetStreamer(S) {}
30
emitValueType(wasm::ValType Type)31 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
32 Streamer.EmitIntValue(uint8_t(Type), 1);
33 }
34
WebAssemblyTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)35 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
36 MCStreamer &S, formatted_raw_ostream &OS)
37 : WebAssemblyTargetStreamer(S), OS(OS) {}
38
WebAssemblyTargetWasmStreamer(MCStreamer & S)39 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
40 : WebAssemblyTargetStreamer(S) {}
41
printTypes(formatted_raw_ostream & OS,ArrayRef<wasm::ValType> Types)42 static void printTypes(formatted_raw_ostream &OS,
43 ArrayRef<wasm::ValType> Types) {
44 bool First = true;
45 for (auto Type : Types) {
46 if (First)
47 First = false;
48 else
49 OS << ", ";
50 OS << WebAssembly::typeToString(Type);
51 }
52 OS << '\n';
53 }
54
emitLocal(ArrayRef<wasm::ValType> Types)55 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
56 if (!Types.empty()) {
57 OS << "\t.local \t";
58 printTypes(OS, Types);
59 }
60 }
61
emitEndFunc()62 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
63
emitSignature(const wasm::WasmSignature * Sig)64 void WebAssemblyTargetAsmStreamer::emitSignature(
65 const wasm::WasmSignature *Sig) {
66 OS << "(";
67 emitParamList(Sig);
68 OS << ") -> (";
69 emitReturnList(Sig);
70 OS << ")";
71 }
72
emitParamList(const wasm::WasmSignature * Sig)73 void WebAssemblyTargetAsmStreamer::emitParamList(
74 const wasm::WasmSignature *Sig) {
75 auto &Params = Sig->Params;
76 for (auto &Ty : Params) {
77 if (&Ty != &Params[0])
78 OS << ", ";
79 OS << WebAssembly::typeToString(Ty);
80 }
81 }
82
emitReturnList(const wasm::WasmSignature * Sig)83 void WebAssemblyTargetAsmStreamer::emitReturnList(
84 const wasm::WasmSignature *Sig) {
85 auto &Returns = Sig->Returns;
86 for (auto &Ty : Returns) {
87 if (&Ty != &Returns[0])
88 OS << ", ";
89 OS << WebAssembly::typeToString(Ty);
90 }
91 }
92
emitFunctionType(const MCSymbolWasm * Sym)93 void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm *Sym) {
94 assert(Sym->isFunction());
95 OS << "\t.functype\t" << Sym->getName() << " ";
96 emitSignature(Sym->getSignature());
97 OS << "\n";
98 }
99
emitGlobalType(const MCSymbolWasm * Sym)100 void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm *Sym) {
101 assert(Sym->isGlobal());
102 OS << "\t.globaltype\t" << Sym->getName() << ", "
103 << WebAssembly::typeToString(
104 static_cast<wasm::ValType>(Sym->getGlobalType().Type))
105 << '\n';
106 }
107
emitEventType(const MCSymbolWasm * Sym)108 void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm *Sym) {
109 assert(Sym->isEvent());
110 OS << "\t.eventtype\t" << Sym->getName() << " ";
111 emitParamList(Sym->getSignature());
112 OS << "\n";
113 }
114
emitImportModule(const MCSymbolWasm * Sym,StringRef ImportModule)115 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym,
116 StringRef ImportModule) {
117 OS << "\t.import_module\t" << Sym->getName() << ", "
118 << ImportModule << '\n';
119 }
120
emitImportName(const MCSymbolWasm * Sym,StringRef ImportName)121 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm *Sym,
122 StringRef ImportName) {
123 OS << "\t.import_name\t" << Sym->getName() << ", "
124 << ImportName << '\n';
125 }
126
emitIndIdx(const MCExpr * Value)127 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
128 OS << "\t.indidx \t" << *Value << '\n';
129 }
130
emitLocal(ArrayRef<wasm::ValType> Types)131 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
132 SmallVector<std::pair<wasm::ValType, uint32_t>, 4> Grouped;
133 for (auto Type : Types) {
134 if (Grouped.empty() || Grouped.back().first != Type)
135 Grouped.push_back(std::make_pair(Type, 1));
136 else
137 ++Grouped.back().second;
138 }
139
140 Streamer.EmitULEB128IntValue(Grouped.size());
141 for (auto Pair : Grouped) {
142 Streamer.EmitULEB128IntValue(Pair.second);
143 emitValueType(Pair.first);
144 }
145 }
146
emitEndFunc()147 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
148 llvm_unreachable(".end_func is not needed for direct wasm output");
149 }
150
emitIndIdx(const MCExpr * Value)151 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
152 llvm_unreachable(".indidx encoding not yet implemented");
153 }
154