blob: e3be081b3add2fa887d20a8c5188a1fac80dc607 [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));
Sam Cleggc94d3932017-11-17 18:14:09 +0000115 }
116 writeU8(OS, WASM_OPCODE_END, "opcode:end");
117}
118
119void wasm::writeLimits(raw_ostream &OS, const WasmLimits &Limits) {
120 writeUleb128(OS, Limits.Flags, "limits flags");
121 writeUleb128(OS, Limits.Initial, "limits initial");
122 if (Limits.Flags & WASM_LIMITS_FLAG_HAS_MAX)
123 writeUleb128(OS, Limits.Maximum, "limits max");
124}
125
126void wasm::writeGlobal(raw_ostream &OS, const WasmGlobal &Global) {
127 writeValueType(OS, Global.Type, "global type");
128 writeUleb128(OS, Global.Mutable, "global mutable");
129 writeInitExpr(OS, Global.InitExpr);
130}
131
132void wasm::writeImport(raw_ostream &OS, const WasmImport &Import) {
133 writeStr(OS, Import.Module, "import module name");
134 writeStr(OS, Import.Field, "import field name");
135 writeU8(OS, Import.Kind, "import kind");
136 switch (Import.Kind) {
137 case WASM_EXTERNAL_FUNCTION:
138 writeUleb128(OS, Import.SigIndex, "import sig index");
139 break;
140 case WASM_EXTERNAL_GLOBAL:
141 writeValueType(OS, Import.Global.Type, "import global type");
142 writeUleb128(OS, Import.Global.Mutable, "import global mutable");
143 break;
144 case WASM_EXTERNAL_MEMORY:
145 writeLimits(OS, Import.Memory);
146 break;
147 default:
148 fatal("unsupported import type: " + Twine(Import.Kind));
Sam Cleggc94d3932017-11-17 18:14:09 +0000149 }
150}
151
152void wasm::writeExport(raw_ostream &OS, const WasmExport &Export) {
153 writeStr(OS, Export.Name, "export name");
154 writeU8(OS, Export.Kind, "export kind");
155 switch (Export.Kind) {
156 case WASM_EXTERNAL_FUNCTION:
157 writeUleb128(OS, Export.Index, "function index");
158 break;
159 case WASM_EXTERNAL_GLOBAL:
160 writeUleb128(OS, Export.Index, "global index");
161 break;
162 case WASM_EXTERNAL_MEMORY:
163 writeUleb128(OS, Export.Index, "memory index");
164 break;
165 default:
166 fatal("unsupported export type: " + Twine(Export.Kind));
Sam Cleggc94d3932017-11-17 18:14:09 +0000167 }
168}
169
170void wasm::writeReloc(raw_ostream &OS, const OutputRelocation &Reloc) {
171 writeUleb128(OS, Reloc.Reloc.Type, "reloc type");
172 writeUleb128(OS, Reloc.Reloc.Offset, "reloc offset");
173 writeUleb128(OS, Reloc.NewIndex, "reloc index");
174
175 switch (Reloc.Reloc.Type) {
176 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
177 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
178 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
179 writeUleb128(OS, Reloc.Reloc.Addend, "reloc addend");
180 break;
181 default:
182 break;
183 }
184}
185
186} // namespace lld