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