[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / llvm / lib / Target / WebAssembly / MCTargetDesc / WebAssemblyTargetStreamer.cpp
blob1a9aa1e14f716cd49bb73a9164da9815b3edf75d
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 //===----------------------------------------------------------------------===//
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"
22 using namespace llvm;
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) {
40 bool First = true;
41 for (auto Type : Types) {
42 if (First)
43 First = false;
44 else
45 OS << ", ";
46 OS << WebAssembly::typeToString(Type);
48 OS << '\n';
51 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
52 if (!Types.empty()) {
53 OS << "\t.local \t";
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());
62 OS << "\n";
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)
71 OS << ", immutable";
72 OS << '\n';
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;
83 if (HasMaximum)
84 OS << ", " << Type.Limits.Maximum;
86 OS << '\n';
89 void WebAssemblyTargetAsmStreamer::emitTagType(const MCSymbolWasm *Sym) {
90 assert(Sym->isTag());
91 OS << "\t.tagtype\t" << Sym->getName() << " ";
92 OS << WebAssembly::typeListToString(Sym->getSignature()->Params);
93 OS << "\n";
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));
123 else
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");