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 "MCTargetDesc/WebAssemblyTargetStreamer.h"
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "Utils/WebAssemblyTypeUtilities.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::emitFunctionType(const MCSymbolWasm
*Sym
) {
64 assert(Sym
->isFunction());
65 OS
<< "\t.functype\t" << Sym
->getName() << " ";
66 OS
<< WebAssembly::signatureToString(Sym
->getSignature());
70 void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm
*Sym
) {
71 assert(Sym
->isGlobal());
72 OS
<< "\t.globaltype\t" << Sym
->getName() << ", "
73 << WebAssembly::typeToString(
74 static_cast<wasm::ValType
>(Sym
->getGlobalType().Type
));
75 if (!Sym
->getGlobalType().Mutable
)
80 void WebAssemblyTargetAsmStreamer::emitTableType(const MCSymbolWasm
*Sym
) {
81 assert(Sym
->isTable());
82 const wasm::WasmTableType
&Type
= Sym
->getTableType();
83 OS
<< "\t.tabletype\t" << Sym
->getName() << ", "
84 << WebAssembly::typeToString(static_cast<wasm::ValType
>(Type
.ElemType
));
85 bool HasMaximum
= Type
.Limits
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
;
86 if (Type
.Limits
.Minimum
!= 0 || HasMaximum
) {
87 OS
<< ", " << Type
.Limits
.Minimum
;
89 OS
<< ", " << Type
.Limits
.Maximum
;
94 void WebAssemblyTargetAsmStreamer::emitTagType(const MCSymbolWasm
*Sym
) {
96 OS
<< "\t.tagtype\t" << Sym
->getName() << " ";
97 OS
<< WebAssembly::typeListToString(Sym
->getSignature()->Params
);
101 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm
*Sym
,
102 StringRef ImportModule
) {
103 OS
<< "\t.import_module\t" << Sym
->getName() << ", "
104 << ImportModule
<< '\n';
107 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm
*Sym
,
108 StringRef ImportName
) {
109 OS
<< "\t.import_name\t" << Sym
->getName() << ", "
110 << ImportName
<< '\n';
113 void WebAssemblyTargetAsmStreamer::emitExportName(const MCSymbolWasm
*Sym
,
114 StringRef ExportName
) {
115 OS
<< "\t.export_name\t" << Sym
->getName() << ", "
116 << ExportName
<< '\n';
119 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr
*Value
) {
120 OS
<< "\t.indidx \t" << *Value
<< '\n';
123 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef
<wasm::ValType
> Types
) {
124 SmallVector
<std::pair
<wasm::ValType
, uint32_t>, 4> Grouped
;
125 for (auto Type
: Types
) {
126 if (Grouped
.empty() || Grouped
.back().first
!= Type
)
127 Grouped
.push_back(std::make_pair(Type
, 1));
129 ++Grouped
.back().second
;
132 Streamer
.emitULEB128IntValue(Grouped
.size());
133 for (auto Pair
: Grouped
) {
134 Streamer
.emitULEB128IntValue(Pair
.second
);
135 emitValueType(Pair
.first
);
139 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
140 llvm_unreachable(".end_func is not needed for direct wasm output");
143 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr
*Value
) {
144 llvm_unreachable(".indidx encoding not yet implemented");