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