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/WebAssemblyInstPrinter.h"
17 #include "MCTargetDesc/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"
25 using namespace llvm;
26 
WebAssemblyTargetStreamer(MCStreamer & S)27 WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S)
28     : MCTargetStreamer(S) {}
29 
emitValueType(wasm::ValType Type)30 void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) {
31   Streamer.emitIntValue(uint8_t(Type), 1);
32 }
33 
WebAssemblyTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)34 WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer(
35     MCStreamer &S, formatted_raw_ostream &OS)
36     : WebAssemblyTargetStreamer(S), OS(OS) {}
37 
WebAssemblyTargetWasmStreamer(MCStreamer & S)38 WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S)
39     : WebAssemblyTargetStreamer(S) {}
40 
printTypes(formatted_raw_ostream & OS,ArrayRef<wasm::ValType> Types)41 static 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 
emitLocal(ArrayRef<wasm::ValType> Types)54 void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
55   if (!Types.empty()) {
56     OS << "\t.local  \t";
57     printTypes(OS, Types);
58   }
59 }
60 
emitEndFunc()61 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
62 
emitFunctionType(const MCSymbolWasm * Sym)63 void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm *Sym) {
64   assert(Sym->isFunction());
65   OS << "\t.functype\t" << Sym->getName() << " ";
66   OS << WebAssembly::signatureToString(Sym->getSignature());
67   OS << "\n";
68 }
69 
emitGlobalType(const MCSymbolWasm * Sym)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      << '\n';
76 }
77 
emitEventType(const MCSymbolWasm * Sym)78 void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm *Sym) {
79   assert(Sym->isEvent());
80   OS << "\t.eventtype\t" << Sym->getName() << " ";
81   OS << WebAssembly::typeListToString(Sym->getSignature()->Params);
82   OS << "\n";
83 }
84 
emitImportModule(const MCSymbolWasm * Sym,StringRef ImportModule)85 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym,
86                                                     StringRef ImportModule) {
87   OS << "\t.import_module\t" << Sym->getName() << ", "
88                              << ImportModule << '\n';
89 }
90 
emitImportName(const MCSymbolWasm * Sym,StringRef ImportName)91 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm *Sym,
92                                                   StringRef ImportName) {
93   OS << "\t.import_name\t" << Sym->getName() << ", "
94                            << ImportName << '\n';
95 }
96 
emitExportName(const MCSymbolWasm * Sym,StringRef ExportName)97 void WebAssemblyTargetAsmStreamer::emitExportName(const MCSymbolWasm *Sym,
98                                                   StringRef ExportName) {
99   OS << "\t.export_name\t" << Sym->getName() << ", "
100                            << ExportName << '\n';
101 }
102 
emitIndIdx(const MCExpr * Value)103 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
104   OS << "\t.indidx  \t" << *Value << '\n';
105 }
106 
emitLocal(ArrayRef<wasm::ValType> Types)107 void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) {
108   SmallVector<std::pair<wasm::ValType, uint32_t>, 4> Grouped;
109   for (auto Type : Types) {
110     if (Grouped.empty() || Grouped.back().first != Type)
111       Grouped.push_back(std::make_pair(Type, 1));
112     else
113       ++Grouped.back().second;
114   }
115 
116   Streamer.emitULEB128IntValue(Grouped.size());
117   for (auto Pair : Grouped) {
118     Streamer.emitULEB128IntValue(Pair.second);
119     emitValueType(Pair.first);
120   }
121 }
122 
emitEndFunc()123 void WebAssemblyTargetWasmStreamer::emitEndFunc() {
124   llvm_unreachable(".end_func is not needed for direct wasm output");
125 }
126 
emitIndIdx(const MCExpr * Value)127 void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) {
128   llvm_unreachable(".indidx encoding not yet implemented");
129 }
130