1//==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines WebAssembly-specific target streamer classes.
11/// These are for implementing support for target-specific assembly directives.
12///
13//===----------------------------------------------------------------------===//
14
15#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17#include "MCTargetDesc/WebAssemblyMCTypeUtilities.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"
25using namespace llvm;
26
27WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
28 : MCTargetStreamer(S) {}
29
30void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
31 Streamer.emitIntValue(Value: uint8_t(Type), Size: 1);
32}
33
34WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
35 MCStreamer &S, formatted_raw_ostream &OS)
36 : WebAssemblyTargetStreamer(S), OS(OS) {}
37
38WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
39 : WebAssemblyTargetStreamer(S) {}
40
41static void printTypes(formatted_raw_ostream &OS,
42 ArrayRef<wasm::ValType> Types) {
43 bool First = true;
44 for (auto Type : Types) {
45 if (First)
46 First = false;
47 else
48 OS << ", ";
49 OS << WebAssembly::typeToString(Type);
50 }
51 OS << '\n';
52}
53
54void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
55 if (!Types.empty()) {
56 OS << "\t.local \t";
57 printTypes(OS, Types);
58 }
59}
60
61void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm *Sym) {
62 assert(Sym->isFunction());
63 OS << "\t.functype\t" << Sym->getName() << " ";
64 OS << WebAssembly::signatureToString(Sig: Sym->getSignature());
65 OS << "\n";
66}
67
68void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm *Sym) {
69 assert(Sym->isGlobal());
70 OS << "\t.globaltype\t" << Sym->getName() << ", "
71 << WebAssembly::typeToString(
72 Type: static_cast<wasm::ValType>(Sym->getGlobalType().Type));
73 if (!Sym->getGlobalType().Mutable)
74 OS << ", immutable";
75 OS << '\n';
76}
77
78void WebAssemblyTargetAsmStreamer::emitTableType(const MCSymbolWasm *Sym) {
79 assert(Sym->isTable());
80 const wasm::WasmTableType &Type = Sym->getTableType();
81 OS << "\t.tabletype\t" << Sym->getName() << ", "
82 << WebAssembly::typeToString(Type: static_cast<wasm::ValType>(Type.ElemType));
83 bool HasMaximum = Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX;
84 if (Type.Limits.Minimum != 0 || HasMaximum) {
85 OS << ", " << Type.Limits.Minimum;
86 if (HasMaximum)
87 OS << ", " << Type.Limits.Maximum;
88 }
89 OS << '\n';
90}
91
92void WebAssemblyTargetAsmStreamer::emitTagType(const MCSymbolWasm *Sym) {
93 assert(Sym->isTag());
94 OS << "\t.tagtype\t" << Sym->getName() << " ";
95 OS << WebAssembly::typeListToString(List: Sym->getSignature()->Params);
96 OS << "\n";
97}
98
99void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym,
100 StringRef ImportModule) {
101 OS << "\t.import_module\t" << Sym->getName() << ", "
102 << ImportModule << '\n';
103}
104
105void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm *Sym,
106 StringRef ImportName) {
107 OS << "\t.import_name\t" << Sym->getName() << ", "
108 << ImportName << '\n';
109}
110
111void WebAssemblyTargetAsmStreamer::emitExportName(const MCSymbolWasm *Sym,
112 StringRef ExportName) {
113 OS << "\t.export_name\t" << Sym->getName() << ", "
114 << ExportName << '\n';
115}
116
117void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
118 OS << "\t.indidx \t" << *Value << '\n';
119}
120
121void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
122 SmallVector<std::pair<wasm::ValType, uint32_t>, 4> Grouped;
123 for (auto Type : Types) {
124 if (Grouped.empty() || Grouped.back().first != Type)
125 Grouped.push_back(Elt: std::make_pair(x&: Type, y: 1));
126 else
127 ++Grouped.back().second;
128 }
129
130 Streamer.emitULEB128IntValue(Value: Grouped.size());
131 for (auto Pair : Grouped) {
132 Streamer.emitULEB128IntValue(Value: Pair.second);
133 emitValueType(Type: Pair.first);
134 }
135}
136
137void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
138 llvm_unreachable(".indidx encoding not yet implemented");
139}
140