blob: 882f4498656ec5762f6fe63ed88b5102ce3c381a [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- WriterUtils.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "WriterUtils.h"
11
12#include "lld/Common/ErrorHandler.h"
13
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/EndianStream.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/LEB128.h"
18
19#define DEBUG_TYPE "lld"
20
21using namespace llvm;
22using namespace llvm::wasm;
23using namespace lld::wasm;
24
25static const char *valueTypeToString(int32_t Type) {
26 switch (Type) {
27 case WASM_TYPE_I32:
28 return "i32";
29 case WASM_TYPE_I64:
30 return "i64";
31 case WASM_TYPE_F32:
32 return "f32";
33 case WASM_TYPE_F64:
34 return "f64";
35 default:
36 llvm_unreachable("invalid value type");
37 }
38}
39
40namespace lld {
41
42void wasm::debugWrite(uint64_t offset, Twine msg) {
43 DEBUG(dbgs() << format(" | %08" PRIx64 ": ", offset) << msg << "\n");
44}
45
46void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, const char *msg) {
47 if (msg)
48 debugWrite(OS.tell(), msg + formatv(" [{0:x}]", Number));
49 encodeULEB128(Number, OS);
50}
51
52void wasm::writeSleb128(raw_ostream &OS, int32_t Number, const char *msg) {
53 if (msg)
54 debugWrite(OS.tell(), msg + formatv(" [{0:x}]", Number));
55 encodeSLEB128(Number, OS);
56}
57
58void wasm::writeBytes(raw_ostream &OS, const char *bytes, size_t count,
59 const char *msg) {
60 if (msg)
61 debugWrite(OS.tell(), msg + formatv(" [data[{0}]]", count));
62 OS.write(bytes, count);
63}
64
65void wasm::writeStr(raw_ostream &OS, const StringRef String, const char *msg) {
66 if (msg)
67 debugWrite(OS.tell(),
68 msg + formatv(" [str[{0}]: {1}]", String.size(), String));
69 writeUleb128(OS, String.size(), nullptr);
70 writeBytes(OS, String.data(), String.size());
71}
72
73void wasm::writeU8(raw_ostream &OS, uint8_t byte, const char *msg) {
74 OS << byte;
75}
76
77void wasm::writeU32(raw_ostream &OS, uint32_t Number, const char *msg) {
78 debugWrite(OS.tell(), msg + formatv("[{0:x}]", Number));
79 support::endian::Writer<support::little>(OS).write(Number);
80}
81
82void wasm::writeValueType(raw_ostream &OS, int32_t Type, const char *msg) {
83 debugWrite(OS.tell(), msg + formatv("[type: {0}]", valueTypeToString(Type)));
84 writeSleb128(OS, Type, nullptr);
85}
86
87void wasm::writeSig(raw_ostream &OS, const WasmSignature &Sig) {
88 writeSleb128(OS, WASM_TYPE_FUNC, "signature type");
89 writeUleb128(OS, Sig.ParamTypes.size(), "param count");
90 for (int32_t ParamType : Sig.ParamTypes) {
91 writeValueType(OS, ParamType, "param type");
92 }
93 if (Sig.ReturnType == WASM_TYPE_NORESULT) {
94 writeUleb128(OS, 0, "result count");
95 } else {
96 writeUleb128(OS, 1, "result count");
97 writeValueType(OS, Sig.ReturnType, "result type");
98 }
99}
100
101void wasm::writeInitExpr(raw_ostream &OS, const WasmInitExpr &InitExpr) {
102 writeU8(OS, InitExpr.Opcode, "opcode");
103 switch (InitExpr.Opcode) {
104 case WASM_OPCODE_I32_CONST:
105 writeSleb128(OS, InitExpr.Value.Int32, "literal (i32)");
106 break;
107 case WASM_OPCODE_I64_CONST:
108 writeSleb128(OS, InitExpr.Value.Int64, "literal (i64)");
109 break;
110 case WASM_OPCODE_GET_GLOBAL:
111 writeUleb128(OS, InitExpr.Value.Global, "literal (global index)");
112 break;
113 default:
114 fatal("unknown opcode in init expr: " + Twine(InitExpr.Opcode));
115 break;
116 }
117 writeU8(OS, WASM_OPCODE_END, "opcode:end");
118}
119
120void wasm::writeLimits(raw_ostream &OS, const WasmLimits &Limits) {
121 writeUleb128(OS, Limits.Flags, "limits flags");
122 writeUleb128(OS, Limits.Initial, "limits initial");
123 if (Limits.Flags & WASM_LIMITS_FLAG_HAS_MAX)
124 writeUleb128(OS, Limits.Maximum, "limits max");
125}
126
127void wasm::writeGlobal(raw_ostream &OS, const WasmGlobal &Global) {
128 writeValueType(OS, Global.Type, "global type");
129 writeUleb128(OS, Global.Mutable, "global mutable");
130 writeInitExpr(OS, Global.InitExpr);
131}
132
133void wasm::writeImport(raw_ostream &OS, const WasmImport &Import) {
134 writeStr(OS, Import.Module, "import module name");
135 writeStr(OS, Import.Field, "import field name");
136 writeU8(OS, Import.Kind, "import kind");
137 switch (Import.Kind) {
138 case WASM_EXTERNAL_FUNCTION:
139 writeUleb128(OS, Import.SigIndex, "import sig index");
140 break;
141 case WASM_EXTERNAL_GLOBAL:
142 writeValueType(OS, Import.Global.Type, "import global type");
143 writeUleb128(OS, Import.Global.Mutable, "import global mutable");
144 break;
145 case WASM_EXTERNAL_MEMORY:
146 writeLimits(OS, Import.Memory);
147 break;
148 default:
149 fatal("unsupported import type: " + Twine(Import.Kind));
150 break;
151 }
152}
153
154void wasm::writeExport(raw_ostream &OS, const WasmExport &Export) {
155 writeStr(OS, Export.Name, "export name");
156 writeU8(OS, Export.Kind, "export kind");
157 switch (Export.Kind) {
158 case WASM_EXTERNAL_FUNCTION:
159 writeUleb128(OS, Export.Index, "function index");
160 break;
161 case WASM_EXTERNAL_GLOBAL:
162 writeUleb128(OS, Export.Index, "global index");
163 break;
164 case WASM_EXTERNAL_MEMORY:
165 writeUleb128(OS, Export.Index, "memory index");
166 break;
167 default:
168 fatal("unsupported export type: " + Twine(Export.Kind));
169 break;
170 }
171}
172
173void wasm::writeReloc(raw_ostream &OS, const OutputRelocation &Reloc) {
174 writeUleb128(OS, Reloc.Reloc.Type, "reloc type");
175 writeUleb128(OS, Reloc.Reloc.Offset, "reloc offset");
176 writeUleb128(OS, Reloc.NewIndex, "reloc index");
177
178 switch (Reloc.Reloc.Type) {
179 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
180 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
181 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
182 writeUleb128(OS, Reloc.Reloc.Addend, "reloc addend");
183 break;
184 default:
185 break;
186 }
187}
188
189} // namespace lld