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 /// \brief 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/MCSectionELF.h"
21 #include "llvm/MC/MCSectionWasm.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/MCSymbolWasm.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Support/Wasm.h"
29 using namespace llvm;
30 
31 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
32     : MCTargetStreamer(S) {}
33 
34 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
35   Streamer.EmitSLEB128IntValue(int32_t(Type));
36 }
37 
38 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
39     MCStreamer &S, formatted_raw_ostream &OS)
40     : WebAssemblyTargetStreamer(S), OS(OS) {}
41 
42 WebAssemblyTargetELFStreamer::WebAssemblyTargetELFStreamer(MCStreamer &S)
43     : WebAssemblyTargetStreamer(S) {}
44 
45 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
46     : WebAssemblyTargetStreamer(S) {}
47 
48 static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) {
49   bool First = true;
50   for (MVT Type : Types) {
51     if (First)
52       First = false;
53     else
54       OS << ", ";
55     OS << WebAssembly::TypeToString(Type);
56   }
57   OS << '\n';
58 }
59 
60 void WebAssemblyTargetAsmStreamer::emitParam(MCSymbol *Symbol,
61                                              ArrayRef<MVT> Types) {
62   if (!Types.empty()) {
63     OS << "\t.param  \t";
64 
65     // FIXME: Currently this applies to the "current" function; it may
66     // be cleaner to specify an explicit symbol as part of the directive.
67 
68     PrintTypes(OS, Types);
69   }
70 }
71 
72 void WebAssemblyTargetAsmStreamer::emitResult(MCSymbol *Symbol,
73                                               ArrayRef<MVT> Types) {
74   if (!Types.empty()) {
75     OS << "\t.result \t";
76 
77     // FIXME: Currently this applies to the "current" function; it may
78     // be cleaner to specify an explicit symbol as part of the directive.
79 
80     PrintTypes(OS, Types);
81   }
82 }
83 
84 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) {
85   if (!Types.empty()) {
86     OS << "\t.local  \t";
87     PrintTypes(OS, Types);
88   }
89 }
90 
91 void WebAssemblyTargetAsmStreamer::emitGlobal(ArrayRef<MVT> Types) {
92   if (!Types.empty()) {
93     OS << "\t.globalvar  \t";
94     PrintTypes(OS, Types);
95   }
96 }
97 
98 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
99 
100 void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
101     StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
102   OS << "\t.functype\t" << name;
103   if (Results.empty())
104     OS << ", void";
105   else {
106     assert(Results.size() == 1);
107     OS << ", " << WebAssembly::TypeToString(Results.front());
108   }
109   for (auto Ty : Params)
110     OS << ", " << WebAssembly::TypeToString(Ty);
111   OS << '\n';
112 }
113 
114 void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
115   OS << "\t.import_global\t" << name << '\n';
116 }
117 
118 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
119   OS << "\t.indidx  \t" << *Value << '\n';
120 }
121 
122 void WebAssemblyTargetELFStreamer::emitParam(MCSymbol *Symbol,
123                                              ArrayRef<MVT> Types) {
124   // Nothing to emit; params are declared as part of the function signature.
125 }
126 
127 void WebAssemblyTargetELFStreamer::emitResult(MCSymbol *Symbol,
128                                               ArrayRef<MVT> Types) {
129   // Nothing to emit; results are declared as part of the function signature.
130 }
131 
132 void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) {
133   Streamer.EmitULEB128IntValue(Types.size());
134   for (MVT Type : Types)
135     emitValueType(WebAssembly::toValType(Type));
136 }
137 
138 void WebAssemblyTargetELFStreamer::emitGlobal(ArrayRef<MVT> Types) {
139   llvm_unreachable(".globalvar encoding not yet implemented");
140 }
141 
142 void WebAssemblyTargetELFStreamer::emitEndFunc() {
143   Streamer.EmitIntValue(WebAssembly::End, 1);
144 }
145 
146 void WebAssemblyTargetELFStreamer::emitIndIdx(const MCExpr *Value) {
147   llvm_unreachable(".indidx encoding not yet implemented");
148 }
149 
150 void WebAssemblyTargetELFStreamer::emitIndirectFunctionType(
151     StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
152   // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
153   // whether it's necessary for .o files to declare indirect function types.
154 }
155 
156 void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) {
157 }
158 
159 void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol,
160                                               ArrayRef<MVT> Types) {
161   SmallVector<wasm::ValType, 4> Params;
162   for (MVT Ty : Types)
163     Params.push_back(WebAssembly::toValType(Ty));
164 
165   cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
166 }
167 
168 void WebAssemblyTargetWasmStreamer::emitResult(MCSymbol *Symbol,
169                                                ArrayRef<MVT> Types) {
170   SmallVector<wasm::ValType, 4> Returns;
171   for (MVT Ty : Types)
172     Returns.push_back(WebAssembly::toValType(Ty));
173 
174   cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
175 }
176 
177 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<MVT> Types) {
178   SmallVector<std::pair<MVT, uint32_t>, 4> Grouped;
179   for (MVT Type : Types) {
180     if (Grouped.empty() || Grouped.back().first != Type)
181       Grouped.push_back(std::make_pair(Type, 1));
182     else
183       ++Grouped.back().second;
184   }
185 
186   Streamer.EmitULEB128IntValue(Grouped.size());
187   for (auto Pair : Grouped) {
188     Streamer.EmitULEB128IntValue(Pair.second);
189     emitValueType(WebAssembly::toValType(Pair.first));
190   }
191 }
192 
193 void WebAssemblyTargetWasmStreamer::emitGlobal(ArrayRef<MVT> Types) {
194   // Encode the globals use by the funciton into the special .global_variables
195   // section. This will later be decoded and turned into contents for the
196   // Globals Section.
197   Streamer.PushSection();
198   Streamer.SwitchSection(Streamer.getContext()
199                                  .getWasmSection(".global_variables", 0, 0));
200   for (MVT Ty : Types)
201     Streamer.EmitIntValue(int64_t(WebAssembly::toValType(Ty)), 1);
202   Streamer.PopSection();
203 }
204 
205 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
206   llvm_unreachable(".end_func is not needed for direct wasm output");
207 }
208 
209 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
210   llvm_unreachable(".indidx encoding not yet implemented");
211 }
212 
213 void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
214     StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
215   // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
216   // whether it's necessary for .o files to declare indirect function types.
217 }
218 
219 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
220 }
221