xref: /openbsd-src/gnu/llvm/lld/wasm/WriterUtils.cpp (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 //===- WriterUtils.cpp ----------------------------------------------------===//
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 #include "WriterUtils.h"
10 #include "lld/Common/ErrorHandler.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/EndianStream.h"
13 #include "llvm/Support/LEB128.h"
14 
15 #define DEBUG_TYPE "lld"
16 
17 using namespace llvm;
18 using namespace llvm::wasm;
19 
20 namespace lld {
21 std::string toString(ValType type) {
22   switch (type) {
23   case ValType::I32:
24     return "i32";
25   case ValType::I64:
26     return "i64";
27   case ValType::F32:
28     return "f32";
29   case ValType::F64:
30     return "f64";
31   case ValType::V128:
32     return "v128";
33   case ValType::EXNREF:
34     return "exnref";
35   case ValType::EXTERNREF:
36     return "externref";
37   }
38   llvm_unreachable("Invalid wasm::ValType");
39 }
40 
41 std::string toString(const WasmSignature &sig) {
42   SmallString<128> s("(");
43   for (ValType type : sig.Params) {
44     if (s.size() != 1)
45       s += ", ";
46     s += toString(type);
47   }
48   s += ") -> ";
49   if (sig.Returns.empty())
50     s += "void";
51   else
52     s += toString(sig.Returns[0]);
53   return std::string(s.str());
54 }
55 
56 std::string toString(const WasmGlobalType &type) {
57   return (type.Mutable ? "var " : "const ") +
58          toString(static_cast<ValType>(type.Type));
59 }
60 
61 std::string toString(const WasmEventType &type) {
62   if (type.Attribute == WASM_EVENT_ATTRIBUTE_EXCEPTION)
63     return "exception";
64   return "unknown";
65 }
66 
67 namespace wasm {
68 void debugWrite(uint64_t offset, const Twine &msg) {
69   LLVM_DEBUG(dbgs() << format("  | %08lld: ", offset) << msg << "\n");
70 }
71 
72 void writeUleb128(raw_ostream &os, uint64_t number, const Twine &msg) {
73   debugWrite(os.tell(), msg + "[" + utohexstr(number) + "]");
74   encodeULEB128(number, os);
75 }
76 
77 void writeSleb128(raw_ostream &os, int64_t number, const Twine &msg) {
78   debugWrite(os.tell(), msg + "[" + utohexstr(number) + "]");
79   encodeSLEB128(number, os);
80 }
81 
82 void writeBytes(raw_ostream &os, const char *bytes, size_t count,
83                       const Twine &msg) {
84   debugWrite(os.tell(), msg + " [data[" + Twine(count) + "]]");
85   os.write(bytes, count);
86 }
87 
88 void writeStr(raw_ostream &os, StringRef string, const Twine &msg) {
89   debugWrite(os.tell(),
90              msg + " [str[" + Twine(string.size()) + "]: " + string + "]");
91   encodeULEB128(string.size(), os);
92   os.write(string.data(), string.size());
93 }
94 
95 void writeU8(raw_ostream &os, uint8_t byte, const Twine &msg) {
96   debugWrite(os.tell(), msg + " [0x" + utohexstr(byte) + "]");
97   os << byte;
98 }
99 
100 void writeU32(raw_ostream &os, uint32_t number, const Twine &msg) {
101   debugWrite(os.tell(), msg + "[0x" + utohexstr(number) + "]");
102   support::endian::write(os, number, support::little);
103 }
104 
105 void writeU64(raw_ostream &os, uint64_t number, const Twine &msg) {
106   debugWrite(os.tell(), msg + "[0x" + utohexstr(number) + "]");
107   support::endian::write(os, number, support::little);
108 }
109 
110 void writeValueType(raw_ostream &os, ValType type, const Twine &msg) {
111   writeU8(os, static_cast<uint8_t>(type),
112           msg + "[type: " + toString(type) + "]");
113 }
114 
115 void writeSig(raw_ostream &os, const WasmSignature &sig) {
116   writeU8(os, WASM_TYPE_FUNC, "signature type");
117   writeUleb128(os, sig.Params.size(), "param Count");
118   for (ValType paramType : sig.Params) {
119     writeValueType(os, paramType, "param type");
120   }
121   writeUleb128(os, sig.Returns.size(), "result Count");
122   if (sig.Returns.size()) {
123     writeValueType(os, sig.Returns[0], "result type");
124   }
125 }
126 
127 void writeI32Const(raw_ostream &os, int32_t number, const Twine &msg) {
128   writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
129   writeSleb128(os, number, msg);
130 }
131 
132 void writeI64Const(raw_ostream &os, int64_t number, const Twine &msg) {
133   writeU8(os, WASM_OPCODE_I64_CONST, "i64.const");
134   writeSleb128(os, number, msg);
135 }
136 
137 void writeMemArg(raw_ostream &os, uint32_t alignment, uint64_t offset) {
138   writeUleb128(os, alignment, "alignment");
139   writeUleb128(os, offset, "offset");
140 }
141 
142 void writeInitExpr(raw_ostream &os, const WasmInitExpr &initExpr) {
143   writeU8(os, initExpr.Opcode, "opcode");
144   switch (initExpr.Opcode) {
145   case WASM_OPCODE_I32_CONST:
146     writeSleb128(os, initExpr.Value.Int32, "literal (i32)");
147     break;
148   case WASM_OPCODE_I64_CONST:
149     writeSleb128(os, initExpr.Value.Int64, "literal (i64)");
150     break;
151   case WASM_OPCODE_F32_CONST:
152     writeU32(os, initExpr.Value.Float32, "literal (f32)");
153     break;
154   case WASM_OPCODE_F64_CONST:
155     writeU64(os, initExpr.Value.Float64, "literal (f64)");
156     break;
157   case WASM_OPCODE_GLOBAL_GET:
158     writeUleb128(os, initExpr.Value.Global, "literal (global index)");
159     break;
160   case WASM_OPCODE_REF_NULL:
161     writeValueType(os, ValType::EXTERNREF, "literal (externref type)");
162     break;
163   default:
164     fatal("unknown opcode in init expr: " + Twine(initExpr.Opcode));
165   }
166   writeU8(os, WASM_OPCODE_END, "opcode:end");
167 }
168 
169 void writeLimits(raw_ostream &os, const WasmLimits &limits) {
170   writeU8(os, limits.Flags, "limits flags");
171   writeUleb128(os, limits.Initial, "limits initial");
172   if (limits.Flags & WASM_LIMITS_FLAG_HAS_MAX)
173     writeUleb128(os, limits.Maximum, "limits max");
174 }
175 
176 void writeGlobalType(raw_ostream &os, const WasmGlobalType &type) {
177   // TODO: Update WasmGlobalType to use ValType and remove this cast.
178   writeValueType(os, ValType(type.Type), "global type");
179   writeU8(os, type.Mutable, "global mutable");
180 }
181 
182 void writeGlobal(raw_ostream &os, const WasmGlobal &global) {
183   writeGlobalType(os, global.Type);
184   writeInitExpr(os, global.InitExpr);
185 }
186 
187 void writeEventType(raw_ostream &os, const WasmEventType &type) {
188   writeUleb128(os, type.Attribute, "event attribute");
189   writeUleb128(os, type.SigIndex, "sig index");
190 }
191 
192 void writeEvent(raw_ostream &os, const WasmEvent &event) {
193   writeEventType(os, event.Type);
194 }
195 
196 void writeTableType(raw_ostream &os, const llvm::wasm::WasmTable &type) {
197   writeU8(os, WASM_TYPE_FUNCREF, "table type");
198   writeLimits(os, type.Limits);
199 }
200 
201 void writeImport(raw_ostream &os, const WasmImport &import) {
202   writeStr(os, import.Module, "import module name");
203   writeStr(os, import.Field, "import field name");
204   writeU8(os, import.Kind, "import kind");
205   switch (import.Kind) {
206   case WASM_EXTERNAL_FUNCTION:
207     writeUleb128(os, import.SigIndex, "import sig index");
208     break;
209   case WASM_EXTERNAL_GLOBAL:
210     writeGlobalType(os, import.Global);
211     break;
212   case WASM_EXTERNAL_EVENT:
213     writeEventType(os, import.Event);
214     break;
215   case WASM_EXTERNAL_MEMORY:
216     writeLimits(os, import.Memory);
217     break;
218   case WASM_EXTERNAL_TABLE:
219     writeTableType(os, import.Table);
220     break;
221   default:
222     fatal("unsupported import type: " + Twine(import.Kind));
223   }
224 }
225 
226 void writeExport(raw_ostream &os, const WasmExport &export_) {
227   writeStr(os, export_.Name, "export name");
228   writeU8(os, export_.Kind, "export kind");
229   switch (export_.Kind) {
230   case WASM_EXTERNAL_FUNCTION:
231     writeUleb128(os, export_.Index, "function index");
232     break;
233   case WASM_EXTERNAL_GLOBAL:
234     writeUleb128(os, export_.Index, "global index");
235     break;
236   case WASM_EXTERNAL_EVENT:
237     writeUleb128(os, export_.Index, "event index");
238     break;
239   case WASM_EXTERNAL_MEMORY:
240     writeUleb128(os, export_.Index, "memory index");
241     break;
242   case WASM_EXTERNAL_TABLE:
243     writeUleb128(os, export_.Index, "table index");
244     break;
245   default:
246     fatal("unsupported export type: " + Twine(export_.Kind));
247   }
248 }
249 
250 } // namespace wasm
251 } // namespace lld
252