1 //==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file defines WebAssembly-specific target streamer classes.
11 /// These are for implementing support for target-specific assembly directives.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssemblyTargetStreamer.h"
16 #include "InstPrinter/WebAssemblyInstPrinter.h"
17 #include "WebAssemblyMCTargetDesc.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionWasm.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbolWasm.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FormattedStream.h"
27 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer
&S
)
28 : MCTargetStreamer(S
) {}
30 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type
) {
31 Streamer
.EmitIntValue(uint8_t(Type
), 1);
34 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
35 MCStreamer
&S
, formatted_raw_ostream
&OS
)
36 : WebAssemblyTargetStreamer(S
), OS(OS
) {}
38 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer
&S
)
39 : WebAssemblyTargetStreamer(S
) {}
41 static void printTypes(formatted_raw_ostream
&OS
,
42 ArrayRef
<wasm::ValType
> Types
) {
44 for (auto Type
: Types
) {
49 OS
<< WebAssembly::typeToString(Type
);
54 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef
<wasm::ValType
> Types
) {
57 printTypes(OS
, Types
);
61 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS
<< "\t.endfunc\n"; }
63 void WebAssemblyTargetAsmStreamer::emitSignature(
64 const wasm::WasmSignature
*Sig
) {
72 void WebAssemblyTargetAsmStreamer::emitParamList(
73 const wasm::WasmSignature
*Sig
) {
74 auto &Params
= Sig
->Params
;
75 for (auto &Ty
: Params
) {
76 if (&Ty
!= &Params
[0])
78 OS
<< WebAssembly::typeToString(Ty
);
82 void WebAssemblyTargetAsmStreamer::emitReturnList(
83 const wasm::WasmSignature
*Sig
) {
84 auto &Returns
= Sig
->Returns
;
85 for (auto &Ty
: Returns
) {
86 if (&Ty
!= &Returns
[0])
88 OS
<< WebAssembly::typeToString(Ty
);
92 void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm
*Sym
) {
93 assert(Sym
->isFunction());
94 OS
<< "\t.functype\t" << Sym
->getName() << " ";
95 emitSignature(Sym
->getSignature());
99 void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm
*Sym
) {
100 assert(Sym
->isGlobal());
101 OS
<< "\t.globaltype\t" << Sym
->getName() << ", "
102 << WebAssembly::typeToString(
103 static_cast<wasm::ValType
>(Sym
->getGlobalType().Type
))
107 void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm
*Sym
) {
108 assert(Sym
->isEvent());
109 OS
<< "\t.eventtype\t" << Sym
->getName() << " ";
110 emitParamList(Sym
->getSignature());
114 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm
*Sym
,
115 StringRef ImportModule
) {
116 OS
<< "\t.import_module\t" << Sym
->getName() << ", "
117 << ImportModule
<< '\n';
120 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm
*Sym
,
121 StringRef ImportName
) {
122 OS
<< "\t.import_name\t" << Sym
->getName() << ", "
123 << ImportName
<< '\n';
126 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr
*Value
) {
127 OS
<< "\t.indidx \t" << *Value
<< '\n';
130 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef
<wasm::ValType
> Types
) {
131 SmallVector
<std::pair
<wasm::ValType
, uint32_t>, 4> Grouped
;
132 for (auto Type
: Types
) {
133 if (Grouped
.empty() || Grouped
.back().first
!= Type
)
134 Grouped
.push_back(std::make_pair(Type
, 1));
136 ++Grouped
.back().second
;
139 Streamer
.EmitULEB128IntValue(Grouped
.size());
140 for (auto Pair
: Grouped
) {
141 Streamer
.EmitULEB128IntValue(Pair
.second
);
142 emitValueType(Pair
.first
);
146 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
147 llvm_unreachable(".end_func is not needed for direct wasm output");
150 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr
*Value
) {
151 llvm_unreachable(".indidx encoding not yet implemented");