blob: 128394f263281172de5cfa9bf794ad62824fca10 [file] [log] [blame]
Dan Gohman3469ee12016-01-12 20:30:51 +00001//==-- 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file defines WebAssembly-specific target streamer classes.
Dan Gohman3469ee12016-01-12 20:30:51 +000012/// 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"
Dan Gohman3469ee12016-01-12 20:30:51 +000019#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCSectionELF.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000021#include "llvm/MC/MCSectionWasm.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000022#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/MC/MCSymbolELF.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000024#include "llvm/MC/MCSymbolWasm.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000025#include "llvm/Support/Casting.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/FormattedStream.h"
28using namespace llvm;
29
30WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
31 : MCTargetStreamer(S) {}
32
Derek Schuffb8795392017-03-16 20:49:48 +000033void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
Sam Clegg03e101f2018-03-01 18:06:21 +000034 Streamer.EmitIntValue(uint8_t(Type), 1);
Derek Schuffb8795392017-03-16 20:49:48 +000035}
36
Dan Gohman3469ee12016-01-12 20:30:51 +000037WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
38 MCStreamer &S, formatted_raw_ostream &OS)
39 : WebAssemblyTargetStreamer(S), OS(OS) {}
40
41WebAssemblyTargetELFStreamer::WebAssemblyTargetELFStreamer(MCStreamer &S)
42 : WebAssemblyTargetStreamer(S) {}
43
Dan Gohman18eafb62017-02-22 01:23:18 +000044WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
45 : WebAssemblyTargetStreamer(S) {}
46
Dan Gohman3469ee12016-01-12 20:30:51 +000047static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) {
48 bool First = true;
49 for (MVT Type : Types) {
50 if (First)
51 First = false;
52 else
53 OS << ", ";
54 OS << WebAssembly::TypeToString(Type);
55 }
56 OS << '\n';
57}
58
Dan Gohmand934cb82017-02-24 23:18:00 +000059void WebAssemblyTargetAsmStreamer::emitParam(MCSymbol *Symbol,
60 ArrayRef<MVT> Types) {
61 if (!Types.empty()) {
62 OS << "\t.param \t";
63
64 // FIXME: Currently this applies to the "current" function; it may
65 // be cleaner to specify an explicit symbol as part of the directive.
66
67 PrintTypes(OS, Types);
68 }
Dan Gohman3469ee12016-01-12 20:30:51 +000069}
70
Dan Gohmand934cb82017-02-24 23:18:00 +000071void WebAssemblyTargetAsmStreamer::emitResult(MCSymbol *Symbol,
72 ArrayRef<MVT> Types) {
73 if (!Types.empty()) {
74 OS << "\t.result \t";
75
76 // FIXME: Currently this applies to the "current" function; it may
77 // be cleaner to specify an explicit symbol as part of the directive.
78
79 PrintTypes(OS, Types);
80 }
Dan Gohman3469ee12016-01-12 20:30:51 +000081}
82
83void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +000084 if (!Types.empty()) {
85 OS << "\t.local \t";
86 PrintTypes(OS, Types);
87 }
Dan Gohman3469ee12016-01-12 20:30:51 +000088}
89
90void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
91
Derek Schuff5859a9ed2016-06-03 18:34:36 +000092void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
Sam Clegg9bf73c02017-07-05 20:25:08 +000093 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
94 OS << "\t.functype\t" << Symbol->getName();
Dan Gohman2726b882016-10-06 22:29:32 +000095 if (Results.empty())
Derek Schuffc64d7652016-08-01 22:25:02 +000096 OS << ", void";
Dan Gohman2726b882016-10-06 22:29:32 +000097 else {
98 assert(Results.size() == 1);
99 OS << ", " << WebAssembly::TypeToString(Results.front());
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000100 }
Dan Gohman2726b882016-10-06 22:29:32 +0000101 for (auto Ty : Params)
102 OS << ", " << WebAssembly::TypeToString(Ty);
103 OS << '\n';
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000104}
105
Derek Schuff7747d703e2016-12-01 00:11:15 +0000106void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
107 OS << "\t.import_global\t" << name << '\n';
108}
109
Dan Gohmandb1916a2018-02-09 23:13:22 +0000110void WebAssemblyTargetAsmStreamer::emitImportModule(MCSymbolWasm *Sym,
111 StringRef ModuleName) {
112 OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
113}
114
Derek Schuffc64d7652016-08-01 22:25:02 +0000115void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
116 OS << "\t.indidx \t" << *Value << '\n';
117}
118
Dan Gohmand934cb82017-02-24 23:18:00 +0000119void WebAssemblyTargetELFStreamer::emitParam(MCSymbol *Symbol,
120 ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000121 // Nothing to emit; params are declared as part of the function signature.
Dan Gohman3469ee12016-01-12 20:30:51 +0000122}
123
Dan Gohmand934cb82017-02-24 23:18:00 +0000124void WebAssemblyTargetELFStreamer::emitResult(MCSymbol *Symbol,
125 ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000126 // Nothing to emit; results are declared as part of the function signature.
Dan Gohman3469ee12016-01-12 20:30:51 +0000127}
128
129void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000130 Streamer.EmitULEB128IntValue(Types.size());
131 for (MVT Type : Types)
Derek Schuffb8795392017-03-16 20:49:48 +0000132 emitValueType(WebAssembly::toValType(Type));
Dan Gohman3469ee12016-01-12 20:30:51 +0000133}
134
135void WebAssemblyTargetELFStreamer::emitEndFunc() {
Dan Gohman3acb1872016-10-24 23:27:49 +0000136 Streamer.EmitIntValue(WebAssembly::End, 1);
Dan Gohman3469ee12016-01-12 20:30:51 +0000137}
Derek Schuffc64d7652016-08-01 22:25:02 +0000138
139void WebAssemblyTargetELFStreamer::emitIndIdx(const MCExpr *Value) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000140 llvm_unreachable(".indidx encoding not yet implemented");
141}
142
143void WebAssemblyTargetELFStreamer::emitIndirectFunctionType(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000144 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000145 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
146 // whether it's necessary for .o files to declare indirect function types.
Derek Schuffc64d7652016-08-01 22:25:02 +0000147}
Derek Schuff7747d703e2016-12-01 00:11:15 +0000148
149void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000150}
151
Dan Gohmandb1916a2018-02-09 23:13:22 +0000152void WebAssemblyTargetELFStreamer::emitImportModule(MCSymbolWasm *Sym,
153 StringRef ModuleName) {
154 llvm_unreachable(".import_module encoding not yet implemented");
155}
156
Dan Gohmand934cb82017-02-24 23:18:00 +0000157void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol,
158 ArrayRef<MVT> Types) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000159 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000160 for (MVT Ty : Types)
Derek Schuffe2688c42017-03-14 20:23:22 +0000161 Params.push_back(WebAssembly::toValType(Ty));
Dan Gohmand934cb82017-02-24 23:18:00 +0000162
163 cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
164}
165
166void WebAssemblyTargetWasmStreamer::emitResult(MCSymbol *Symbol,
167 ArrayRef<MVT> Types) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000168 SmallVector<wasm::ValType, 4> Returns;
Dan Gohmand934cb82017-02-24 23:18:00 +0000169 for (MVT Ty : Types)
Derek Schuffe2688c42017-03-14 20:23:22 +0000170 Returns.push_back(WebAssembly::toValType(Ty));
Dan Gohmand934cb82017-02-24 23:18:00 +0000171
172 cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
Dan Gohman18eafb62017-02-22 01:23:18 +0000173}
174
175void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000176 SmallVector<std::pair<MVT, uint32_t>, 4> Grouped;
177 for (MVT Type : Types) {
178 if (Grouped.empty() || Grouped.back().first != Type)
179 Grouped.push_back(std::make_pair(Type, 1));
180 else
181 ++Grouped.back().second;
182 }
183
184 Streamer.EmitULEB128IntValue(Grouped.size());
185 for (auto Pair : Grouped) {
186 Streamer.EmitULEB128IntValue(Pair.second);
Derek Schuffb8795392017-03-16 20:49:48 +0000187 emitValueType(WebAssembly::toValType(Pair.first));
Dan Gohmand934cb82017-02-24 23:18:00 +0000188 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000189}
190
191void WebAssemblyTargetWasmStreamer::emitEndFunc() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000192 llvm_unreachable(".end_func is not needed for direct wasm output");
Dan Gohman18eafb62017-02-22 01:23:18 +0000193}
194
195void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
196 llvm_unreachable(".indidx encoding not yet implemented");
197}
198
199void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
Sam Clegg9bf73c02017-07-05 20:25:08 +0000200 MCSymbol *Symbol, SmallVectorImpl<MVT> &Params,
201 SmallVectorImpl<MVT> &Results) {
202 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
203 if (WasmSym->isFunction()) {
204 // Symbol already has its arguments and result set.
205 return;
206 }
207
208 SmallVector<wasm::ValType, 4> ValParams;
209 for (MVT Ty : Params)
210 ValParams.push_back(WebAssembly::toValType(Ty));
211
212 SmallVector<wasm::ValType, 1> ValResults;
213 for (MVT Ty : Results)
214 ValResults.push_back(WebAssembly::toValType(Ty));
215
216 WasmSym->setParams(std::move(ValParams));
217 WasmSym->setReturns(std::move(ValResults));
Sam Clegg6c899ba2018-02-23 05:08:34 +0000218 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohman18eafb62017-02-22 01:23:18 +0000219}
220
221void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
Dan Gohmanc2c99772017-12-05 17:23:43 +0000222 llvm_unreachable(".global_import is not needed for direct wasm output");
Dan Gohman18eafb62017-02-22 01:23:18 +0000223}
Dan Gohmandb1916a2018-02-09 23:13:22 +0000224
225void WebAssemblyTargetWasmStreamer::emitImportModule(MCSymbolWasm *Sym,
226 StringRef ModuleName) {
227 Sym->setModuleName(ModuleName);
228}