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<MVT> Types)42 static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) {
43 bool First = true;
44 for (MVT Type : Types) {
45 if (First)
46 First = false;
47 else
48 OS << ", ";
49 OS << WebAssembly::TypeToString(Type);
50 }
51 OS << '\n';
52 }
53
emitParam(MCSymbol * Symbol,ArrayRef<MVT> Types)54 void WebAssemblyTargetAsmStreamer::emitParam(MCSymbol *Symbol,
55 ArrayRef<MVT> Types) {
56 if (!Types.empty()) {
57 OS << "\t.param \t";
58
59 // FIXME: Currently this applies to the "current" function; it may
60 // be cleaner to specify an explicit symbol as part of the directive.
61
62 PrintTypes(OS, Types);
63 }
64 }
65
emitResult(MCSymbol * Symbol,ArrayRef<MVT> Types)66 void WebAssemblyTargetAsmStreamer::emitResult(MCSymbol *Symbol,
67 ArrayRef<MVT> Types) {
68 if (!Types.empty()) {
69 OS << "\t.result \t";
70
71 // FIXME: Currently this applies to the "current" function; it may
72 // be cleaner to specify an explicit symbol as part of the directive.
73
74 PrintTypes(OS, Types);
75 }
76 }
77
emitLocal(ArrayRef<MVT> Types)78 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) {
79 if (!Types.empty()) {
80 OS << "\t.local \t";
81 PrintTypes(OS, Types);
82 }
83 }
84
emitEndFunc()85 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
86
emitIndirectFunctionType(MCSymbol * Symbol,SmallVectorImpl<MVT> & Params,SmallVectorImpl<MVT> & Results)87 void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
88 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
89 OS << "\t.functype\t" << Symbol->getName();
90 if (Results.empty())
91 OS << ", void";
92 else {
93 assert(Results.size() == 1);
94 OS << ", " << WebAssembly::TypeToString(Results.front());
95 }
96 for (auto Ty : Params)
97 OS << ", " << WebAssembly::TypeToString(Ty);
98 OS << '\n';
99 }
100
emitGlobalImport(StringRef name)101 void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
102 OS << "\t.import_global\t" << name << '\n';
103 }
104
emitImportModule(MCSymbolWasm * Sym,StringRef ModuleName)105 void WebAssemblyTargetAsmStreamer::emitImportModule(MCSymbolWasm *Sym,
106 StringRef ModuleName) {
107 OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
108 }
109
emitIndIdx(const MCExpr * Value)110 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
111 OS << "\t.indidx \t" << *Value << '\n';
112 }
113
emitParam(MCSymbol * Symbol,ArrayRef<MVT> Types)114 void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol,
115 ArrayRef<MVT> Types) {
116 SmallVector<wasm::ValType, 4> Params;
117 for (MVT Ty : Types)
118 Params.push_back(WebAssembly::toValType(Ty));
119
120 cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
121 }
122
emitResult(MCSymbol * Symbol,ArrayRef<MVT> Types)123 void WebAssemblyTargetWasmStreamer::emitResult(MCSymbol *Symbol,
124 ArrayRef<MVT> Types) {
125 SmallVector<wasm::ValType, 4> Returns;
126 for (MVT Ty : Types)
127 Returns.push_back(WebAssembly::toValType(Ty));
128
129 cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
130 }
131
emitLocal(ArrayRef<MVT> Types)132 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<MVT> Types) {
133 SmallVector<std::pair<MVT, uint32_t>, 4> Grouped;
134 for (MVT Type : Types) {
135 if (Grouped.empty() || Grouped.back().first != Type)
136 Grouped.push_back(std::make_pair(Type, 1));
137 else
138 ++Grouped.back().second;
139 }
140
141 Streamer.EmitULEB128IntValue(Grouped.size());
142 for (auto Pair : Grouped) {
143 Streamer.EmitULEB128IntValue(Pair.second);
144 emitValueType(WebAssembly::toValType(Pair.first));
145 }
146 }
147
emitEndFunc()148 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
149 llvm_unreachable(".end_func is not needed for direct wasm output");
150 }
151
emitIndIdx(const MCExpr * Value)152 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
153 llvm_unreachable(".indidx encoding not yet implemented");
154 }
155
emitIndirectFunctionType(MCSymbol * Symbol,SmallVectorImpl<MVT> & Params,SmallVectorImpl<MVT> & Results)156 void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
157 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params,
158 SmallVectorImpl<MVT> &Results) {
159 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
160 if (WasmSym->isFunction()) {
161 // Symbol already has its arguments and result set.
162 return;
163 }
164
165 SmallVector<wasm::ValType, 4> ValParams;
166 for (MVT Ty : Params)
167 ValParams.push_back(WebAssembly::toValType(Ty));
168
169 SmallVector<wasm::ValType, 1> ValResults;
170 for (MVT Ty : Results)
171 ValResults.push_back(WebAssembly::toValType(Ty));
172
173 WasmSym->setParams(std::move(ValParams));
174 WasmSym->setReturns(std::move(ValResults));
175 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
176 }
177
emitGlobalImport(StringRef name)178 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
179 llvm_unreachable(".global_import is not needed for direct wasm output");
180 }
181
emitImportModule(MCSymbolWasm * Sym,StringRef ModuleName)182 void WebAssemblyTargetWasmStreamer::emitImportModule(MCSymbolWasm *Sym,
183 StringRef ModuleName) {
184 Sym->setModuleName(ModuleName);
185 }
186