blob: a499cc23a6bfc91dda000744c470f1d9310030ca [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
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"
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"
Dan Gohmand934cb82017-02-24 23:18:00 +000028#include "llvm/Support/Wasm.h"
Dan Gohman3469ee12016-01-12 20:30:51 +000029using namespace llvm;
30
31WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
32 : MCTargetStreamer(S) {}
33
34WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
35 MCStreamer &S, formatted_raw_ostream &OS)
36 : WebAssemblyTargetStreamer(S), OS(OS) {}
37
38WebAssemblyTargetELFStreamer::WebAssemblyTargetELFStreamer(MCStreamer &S)
39 : WebAssemblyTargetStreamer(S) {}
40
Dan Gohman18eafb62017-02-22 01:23:18 +000041WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
42 : WebAssemblyTargetStreamer(S) {}
43
Dan Gohman3469ee12016-01-12 20:30:51 +000044static void PrintTypes(formatted_raw_ostream &OS, ArrayRef<MVT> Types) {
45 bool First = true;
46 for (MVT Type : Types) {
47 if (First)
48 First = false;
49 else
50 OS << ", ";
51 OS << WebAssembly::TypeToString(Type);
52 }
53 OS << '\n';
54}
55
Dan Gohmand934cb82017-02-24 23:18:00 +000056void WebAssemblyTargetAsmStreamer::emitParam(MCSymbol *Symbol,
57 ArrayRef<MVT> Types) {
58 if (!Types.empty()) {
59 OS << "\t.param \t";
60
61 // FIXME: Currently this applies to the "current" function; it may
62 // be cleaner to specify an explicit symbol as part of the directive.
63
64 PrintTypes(OS, Types);
65 }
Dan Gohman3469ee12016-01-12 20:30:51 +000066}
67
Dan Gohmand934cb82017-02-24 23:18:00 +000068void WebAssemblyTargetAsmStreamer::emitResult(MCSymbol *Symbol,
69 ArrayRef<MVT> Types) {
70 if (!Types.empty()) {
71 OS << "\t.result \t";
72
73 // FIXME: Currently this applies to the "current" function; it may
74 // be cleaner to specify an explicit symbol as part of the directive.
75
76 PrintTypes(OS, Types);
77 }
Dan Gohman3469ee12016-01-12 20:30:51 +000078}
79
80void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +000081 if (!Types.empty()) {
82 OS << "\t.local \t";
83 PrintTypes(OS, Types);
84 }
Dan Gohman3469ee12016-01-12 20:30:51 +000085}
86
Dan Gohman82607f52017-02-24 23:46:05 +000087void WebAssemblyTargetAsmStreamer::emitGlobal(ArrayRef<MVT> Types) {
88 if (!Types.empty()) {
89 OS << "\t.globalvar \t";
90 PrintTypes(OS, Types);
91 }
92}
93
Dan Gohman3469ee12016-01-12 20:30:51 +000094void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
95
Derek Schuff5859a9ed2016-06-03 18:34:36 +000096void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
Dan Gohman2726b882016-10-06 22:29:32 +000097 StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
Derek Schuff5859a9ed2016-06-03 18:34:36 +000098 OS << "\t.functype\t" << name;
Dan Gohman2726b882016-10-06 22:29:32 +000099 if (Results.empty())
Derek Schuffc64d7652016-08-01 22:25:02 +0000100 OS << ", void";
Dan Gohman2726b882016-10-06 22:29:32 +0000101 else {
102 assert(Results.size() == 1);
103 OS << ", " << WebAssembly::TypeToString(Results.front());
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000104 }
Dan Gohman2726b882016-10-06 22:29:32 +0000105 for (auto Ty : Params)
106 OS << ", " << WebAssembly::TypeToString(Ty);
107 OS << '\n';
Derek Schuff5859a9ed2016-06-03 18:34:36 +0000108}
109
Derek Schuff7747d703e2016-12-01 00:11:15 +0000110void WebAssemblyTargetAsmStreamer::emitGlobalImport(StringRef name) {
111 OS << "\t.import_global\t" << name << '\n';
112}
113
Derek Schuffc64d7652016-08-01 22:25:02 +0000114void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
115 OS << "\t.indidx \t" << *Value << '\n';
116}
117
Dan Gohmand934cb82017-02-24 23:18:00 +0000118void WebAssemblyTargetELFStreamer::emitParam(MCSymbol *Symbol,
119 ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000120 // Nothing to emit; params are declared as part of the function signature.
Dan Gohman3469ee12016-01-12 20:30:51 +0000121}
122
Dan Gohmand934cb82017-02-24 23:18:00 +0000123void WebAssemblyTargetELFStreamer::emitResult(MCSymbol *Symbol,
124 ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000125 // Nothing to emit; results are declared as part of the function signature.
Dan Gohman3469ee12016-01-12 20:30:51 +0000126}
127
128void WebAssemblyTargetELFStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000129 Streamer.EmitULEB128IntValue(Types.size());
130 for (MVT Type : Types)
131 Streamer.EmitIntValue(int64_t(WebAssembly::toValType(Type)), 1);
Dan Gohman3469ee12016-01-12 20:30:51 +0000132}
133
Dan Gohman82607f52017-02-24 23:46:05 +0000134void WebAssemblyTargetELFStreamer::emitGlobal(ArrayRef<MVT> Types) {
135 llvm_unreachable(".globalvar encoding not yet implemented");
136}
137
Dan Gohman3469ee12016-01-12 20:30:51 +0000138void WebAssemblyTargetELFStreamer::emitEndFunc() {
Dan Gohman3acb1872016-10-24 23:27:49 +0000139 Streamer.EmitIntValue(WebAssembly::End, 1);
Dan Gohman3469ee12016-01-12 20:30:51 +0000140}
Derek Schuffc64d7652016-08-01 22:25:02 +0000141
142void WebAssemblyTargetELFStreamer::emitIndIdx(const MCExpr *Value) {
Dan Gohman3acb1872016-10-24 23:27:49 +0000143 llvm_unreachable(".indidx encoding not yet implemented");
144}
145
146void WebAssemblyTargetELFStreamer::emitIndirectFunctionType(
147 StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
148 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
149 // whether it's necessary for .o files to declare indirect function types.
Derek Schuffc64d7652016-08-01 22:25:02 +0000150}
Derek Schuff7747d703e2016-12-01 00:11:15 +0000151
152void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000153}
154
Dan Gohmand934cb82017-02-24 23:18:00 +0000155void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol,
156 ArrayRef<MVT> Types) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000157 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000158 for (MVT Ty : Types)
Derek Schuffe2688c42017-03-14 20:23:22 +0000159 Params.push_back(WebAssembly::toValType(Ty));
Dan Gohmand934cb82017-02-24 23:18:00 +0000160
161 cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
162}
163
164void WebAssemblyTargetWasmStreamer::emitResult(MCSymbol *Symbol,
165 ArrayRef<MVT> Types) {
Derek Schuffe2688c42017-03-14 20:23:22 +0000166 SmallVector<wasm::ValType, 4> Returns;
Dan Gohmand934cb82017-02-24 23:18:00 +0000167 for (MVT Ty : Types)
Derek Schuffe2688c42017-03-14 20:23:22 +0000168 Returns.push_back(WebAssembly::toValType(Ty));
Dan Gohmand934cb82017-02-24 23:18:00 +0000169
170 cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
Dan Gohman18eafb62017-02-22 01:23:18 +0000171}
172
173void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<MVT> Types) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000174 SmallVector<std::pair<MVT, uint32_t>, 4> Grouped;
175 for (MVT Type : Types) {
176 if (Grouped.empty() || Grouped.back().first != Type)
177 Grouped.push_back(std::make_pair(Type, 1));
178 else
179 ++Grouped.back().second;
180 }
181
182 Streamer.EmitULEB128IntValue(Grouped.size());
183 for (auto Pair : Grouped) {
184 Streamer.EmitULEB128IntValue(Pair.second);
185 Streamer.EmitULEB128IntValue(uint64_t(WebAssembly::toValType(Pair.first)));
186 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000187}
188
Dan Gohman82607f52017-02-24 23:46:05 +0000189void WebAssemblyTargetWasmStreamer::emitGlobal(ArrayRef<MVT> Types) {
190 // Encode the globals use by the funciton into the special .global_variables
191 // section. This will later be decoded and turned into contents for the
192 // Globals Section.
193 Streamer.PushSection();
194 Streamer.SwitchSection(Streamer.getContext()
195 .getWasmSection(".global_variables", 0, 0));
196 for (MVT Ty : Types)
197 Streamer.EmitIntValue(uint64_t(WebAssembly::toValType(Ty)), 1);
198 Streamer.PopSection();
199}
200
Dan Gohman18eafb62017-02-22 01:23:18 +0000201void WebAssemblyTargetWasmStreamer::emitEndFunc() {
Dan Gohmand934cb82017-02-24 23:18:00 +0000202 llvm_unreachable(".end_func is not needed for direct wasm output");
Dan Gohman18eafb62017-02-22 01:23:18 +0000203}
204
205void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
206 llvm_unreachable(".indidx encoding not yet implemented");
207}
208
209void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
210 StringRef name, SmallVectorImpl<MVT> &Params, SmallVectorImpl<MVT> &Results) {
211 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
212 // whether it's necessary for .o files to declare indirect function types.
213}
214
215void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
216}