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/WebAssemblyMCTypeUtilities.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCSectionWasm.h"
19 #include "llvm/MC/MCSymbolWasm.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FormattedStream.h"
24 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer
&S
)
25 : MCTargetStreamer(S
) {}
27 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type
) {
28 Streamer
.emitIntValue(uint8_t(Type
), 1);
31 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
32 MCStreamer
&S
, formatted_raw_ostream
&OS
)
33 : WebAssemblyTargetStreamer(S
), OS(OS
) {}
35 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer
&S
)
36 : WebAssemblyTargetStreamer(S
) {}
38 static void printTypes(formatted_raw_ostream
&OS
,
39 ArrayRef
<wasm::ValType
> Types
) {
41 for (auto Type
: Types
) {
46 OS
<< WebAssembly::typeToString(Type
);
51 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef
<wasm::ValType
> Types
) {
54 printTypes(OS
, Types
);
58 void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm
*Sym
) {
59 assert(Sym
->isFunction());
60 OS
<< "\t.functype\t" << Sym
->getName() << " ";
61 OS
<< WebAssembly::signatureToString(Sym
->getSignature());
65 void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm
*Sym
) {
66 assert(Sym
->isGlobal());
67 OS
<< "\t.globaltype\t" << Sym
->getName() << ", "
68 << WebAssembly::typeToString(
69 static_cast<wasm::ValType
>(Sym
->getGlobalType().Type
));
70 if (!Sym
->getGlobalType().Mutable
)
75 void WebAssemblyTargetAsmStreamer::emitTableType(const MCSymbolWasm
*Sym
) {
76 assert(Sym
->isTable());
77 const wasm::WasmTableType
&Type
= Sym
->getTableType();
78 OS
<< "\t.tabletype\t" << Sym
->getName() << ", "
79 << WebAssembly::typeToString(static_cast<wasm::ValType
>(Type
.ElemType
));
80 bool HasMaximum
= Type
.Limits
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
;
81 if (Type
.Limits
.Minimum
!= 0 || HasMaximum
) {
82 OS
<< ", " << Type
.Limits
.Minimum
;
84 OS
<< ", " << Type
.Limits
.Maximum
;
89 void WebAssemblyTargetAsmStreamer::emitTagType(const MCSymbolWasm
*Sym
) {
91 OS
<< "\t.tagtype\t" << Sym
->getName() << " ";
92 OS
<< WebAssembly::typeListToString(Sym
->getSignature()->Params
);
96 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm
*Sym
,
97 StringRef ImportModule
) {
98 OS
<< "\t.import_module\t" << Sym
->getName() << ", "
99 << ImportModule
<< '\n';
102 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm
*Sym
,
103 StringRef ImportName
) {
104 OS
<< "\t.import_name\t" << Sym
->getName() << ", "
105 << ImportName
<< '\n';
108 void WebAssemblyTargetAsmStreamer::emitExportName(const MCSymbolWasm
*Sym
,
109 StringRef ExportName
) {
110 OS
<< "\t.export_name\t" << Sym
->getName() << ", "
111 << ExportName
<< '\n';
114 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr
*Value
) {
115 OS
<< "\t.indidx \t" << *Value
<< '\n';
118 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef
<wasm::ValType
> Types
) {
119 SmallVector
<std::pair
<wasm::ValType
, uint32_t>, 4> Grouped
;
120 for (auto Type
: Types
) {
121 if (Grouped
.empty() || Grouped
.back().first
!= Type
)
122 Grouped
.push_back(std::make_pair(Type
, 1));
124 ++Grouped
.back().second
;
127 Streamer
.emitULEB128IntValue(Grouped
.size());
128 for (auto Pair
: Grouped
) {
129 Streamer
.emitULEB128IntValue(Pair
.second
);
130 emitValueType(Pair
.first
);
134 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr
*Value
) {
135 llvm_unreachable(".indidx encoding not yet implemented");