/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
27 | | WebAssemblyTargetStreamer::WebAssemblyTargetStreamer(MCStreamer &S) |
28 | 444 | : MCTargetStreamer(S) {} |
29 | | |
30 | 19 | void WebAssemblyTargetStreamer::emitValueType(wasm::ValType Type) { |
31 | 19 | Streamer.EmitIntValue(uint8_t(Type), 1); |
32 | 19 | } |
33 | | |
34 | | WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer( |
35 | | MCStreamer &S, formatted_raw_ostream &OS) |
36 | 234 | : WebAssemblyTargetStreamer(S), OS(OS) {} |
37 | | |
38 | | WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer(MCStreamer &S) |
39 | 209 | : WebAssemblyTargetStreamer(S) {} |
40 | | |
41 | | static void printTypes(formatted_raw_ostream &OS, |
42 | 383 | ArrayRef<wasm::ValType> Types) { |
43 | 383 | bool First = true; |
44 | 922 | for (auto Type : Types) { |
45 | 922 | if (First) |
46 | 383 | First = false; |
47 | 539 | else |
48 | 539 | OS << ", "; |
49 | 922 | OS << WebAssembly::typeToString(Type); |
50 | 922 | } |
51 | 383 | OS << '\n'; |
52 | 383 | } |
53 | | |
54 | 4.04k | void WebAssemblyTargetAsmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) { |
55 | 4.04k | if (!Types.empty()) { |
56 | 383 | OS << "\t.local \t"; |
57 | 383 | printTypes(OS, Types); |
58 | 383 | } |
59 | 4.04k | } |
60 | | |
61 | 0 | void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; } |
62 | | |
63 | | void WebAssemblyTargetAsmStreamer::emitSignature( |
64 | 4.33k | const wasm::WasmSignature *Sig) { |
65 | 4.33k | OS << "("; |
66 | 4.33k | emitParamList(Sig); |
67 | 4.33k | OS << ") -> ("; |
68 | 4.33k | emitReturnList(Sig); |
69 | 4.33k | OS << ")"; |
70 | 4.33k | } |
71 | | |
72 | | void WebAssemblyTargetAsmStreamer::emitParamList( |
73 | 4.34k | const wasm::WasmSignature *Sig) { |
74 | 4.34k | auto &Params = Sig->Params; |
75 | 11.6k | for (auto &Ty : Params) { |
76 | 11.6k | if (&Ty != &Params[0]) |
77 | 7.81k | OS << ", "; |
78 | 11.6k | OS << WebAssembly::typeToString(Ty); |
79 | 11.6k | } |
80 | 4.34k | } |
81 | | |
82 | | void WebAssemblyTargetAsmStreamer::emitReturnList( |
83 | 4.33k | const wasm::WasmSignature *Sig) { |
84 | 4.33k | auto &Returns = Sig->Returns; |
85 | 4.33k | for (auto &Ty : Returns) { |
86 | 2.73k | if (&Ty != &Returns[0]) |
87 | 0 | OS << ", "; |
88 | 2.73k | OS << WebAssembly::typeToString(Ty); |
89 | 2.73k | } |
90 | 4.33k | } |
91 | | |
92 | 4.33k | void WebAssemblyTargetAsmStreamer::emitFunctionType(const MCSymbolWasm *Sym) { |
93 | 4.33k | assert(Sym->isFunction()); |
94 | 4.33k | OS << "\t.functype\t" << Sym->getName() << " "; |
95 | 4.33k | emitSignature(Sym->getSignature()); |
96 | 4.33k | OS << "\n"; |
97 | 4.33k | } |
98 | | |
99 | 76 | void WebAssemblyTargetAsmStreamer::emitGlobalType(const MCSymbolWasm *Sym) { |
100 | 76 | assert(Sym->isGlobal()); |
101 | 76 | OS << "\t.globaltype\t" << Sym->getName() << ", " |
102 | 76 | << WebAssembly::typeToString( |
103 | 76 | static_cast<wasm::ValType>(Sym->getGlobalType().Type)) |
104 | 76 | << '\n'; |
105 | 76 | } |
106 | | |
107 | 9 | void WebAssemblyTargetAsmStreamer::emitEventType(const MCSymbolWasm *Sym) { |
108 | 9 | assert(Sym->isEvent()); |
109 | 9 | OS << "\t.eventtype\t" << Sym->getName() << " "; |
110 | 9 | emitParamList(Sym->getSignature()); |
111 | 9 | OS << "\n"; |
112 | 9 | } |
113 | | |
114 | | void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym, |
115 | 1 | StringRef ImportModule) { |
116 | 1 | OS << "\t.import_module\t" << Sym->getName() << ", " |
117 | 1 | << ImportModule << '\n'; |
118 | 1 | } |
119 | | |
120 | | void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm *Sym, |
121 | 1 | StringRef ImportName) { |
122 | 1 | OS << "\t.import_name\t" << Sym->getName() << ", " |
123 | 1 | << ImportName << '\n'; |
124 | 1 | } |
125 | | |
126 | 2 | void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) { |
127 | 2 | OS << "\t.indidx \t" << *Value << '\n'; |
128 | 2 | } |
129 | | |
130 | 430 | void WebAssemblyTargetWasmStreamer::emitLocal(ArrayRef<wasm::ValType> Types) { |
131 | 430 | SmallVector<std::pair<wasm::ValType, uint32_t>, 4> Grouped; |
132 | 430 | for (auto Type : Types) { |
133 | 27 | if (Grouped.empty() || Grouped.back().first != Type14 ) |
134 | 19 | Grouped.push_back(std::make_pair(Type, 1)); |
135 | 8 | else |
136 | 8 | ++Grouped.back().second; |
137 | 27 | } |
138 | 430 | |
139 | 430 | Streamer.EmitULEB128IntValue(Grouped.size()); |
140 | 430 | for (auto Pair : Grouped) { |
141 | 19 | Streamer.EmitULEB128IntValue(Pair.second); |
142 | 19 | emitValueType(Pair.first); |
143 | 19 | } |
144 | 430 | } |
145 | | |
146 | 0 | void WebAssemblyTargetWasmStreamer::emitEndFunc() { |
147 | 0 | llvm_unreachable(".end_func is not needed for direct wasm output"); |
148 | 0 | } |
149 | | |
150 | 0 | void WebAssemblyTargetWasmStreamer::emitIndIdx(const MCExpr *Value) { |
151 | 0 | llvm_unreachable(".indidx encoding not yet implemented"); |
152 | 0 | } |