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