authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-10 21:39:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-10 21:39:17-07:00
log168da23d8f556d47ae9e029f70806cbd4bfad431
tree4f6b202adf6fadcc4bbca6263184f51498746520
parent3280fc98f3b0400c4ce0b8c54a157f1858490351

(wip) update wasm linker to new Writer API


6 files changed, 868 insertions(+), 1041 deletions(-)

src/arch/wasm/Emit.zig+103-134
......@@ -4,6 +4,7 @@ const std = @import("std");
44const assert = std.debug.assert;
55const Allocator = std.mem.Allocator;
66const leb = std.leb;
7const Writer = std.Io.Writer;
78
89const Wasm = link.File.Wasm;
910const Mir = @import("Mir.zig");
......@@ -14,16 +15,16 @@ const codegen = @import("../../codegen.zig");
1415
1516mir: Mir,
1617wasm: *Wasm,
17/// The binary representation that will be emitted by this module.
18code: *std.ArrayListUnmanaged(u8),
18/// The binary representation of this module is written here.
19writer: *Writer,
1920
2021pub const Error = error{
21 OutOfMemory,
22 WriteFailed,
2223};
2324
2425pub fn lowerToCode(emit: *Emit) Error!void {
2526 const mir = &emit.mir;
26 const code = emit.code;
27 const writer = emit.writer;
2728 const wasm = emit.wasm;
2829 const comp = wasm.base.comp;
2930 const gpa = comp.gpa;
......@@ -41,18 +42,19 @@ pub fn lowerToCode(emit: *Emit) Error!void {
4142 },
4243 .block, .loop => {
4344 const block_type = datas[inst].block_type;
44 try code.ensureUnusedCapacity(gpa, 2);
45 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
46 code.appendAssumeCapacity(@intFromEnum(block_type));
45 try writer.writeAll(&.{
46 @intFromEnum(tags[inst]),
47 @intFromEnum(block_type),
48 });
4749
4850 inst += 1;
4951 continue :loop tags[inst];
5052 },
5153 .uav_ref => {
5254 if (is_obj) {
53 try uavRefObj(wasm, code, datas[inst].ip_index, 0, is_wasm32);
55 try uavRefObj(wasm, writer, datas[inst].ip_index, 0, is_wasm32);
5456 } else {
55 try uavRefExe(wasm, code, datas[inst].ip_index, 0, is_wasm32);
57 try uavRefExe(wasm, writer, datas[inst].ip_index, 0, is_wasm32);
5658 }
5759 inst += 1;
5860 continue :loop tags[inst];
......@@ -60,20 +62,20 @@ pub fn lowerToCode(emit: *Emit) Error!void {
6062 .uav_ref_off => {
6163 const extra = mir.extraData(Mir.UavRefOff, datas[inst].payload).data;
6264 if (is_obj) {
63 try uavRefObj(wasm, code, extra.value, extra.offset, is_wasm32);
65 try uavRefObj(wasm, writer, extra.value, extra.offset, is_wasm32);
6466 } else {
65 try uavRefExe(wasm, code, extra.value, extra.offset, is_wasm32);
67 try uavRefExe(wasm, writer, extra.value, extra.offset, is_wasm32);
6668 }
6769 inst += 1;
6870 continue :loop tags[inst];
6971 },
7072 .nav_ref => {
71 try navRefOff(wasm, code, .{ .nav_index = datas[inst].nav_index, .offset = 0 }, is_wasm32);
73 try navRefOff(wasm, writer, .{ .nav_index = datas[inst].nav_index, .offset = 0 }, is_wasm32);
7274 inst += 1;
7375 continue :loop tags[inst];
7476 },
7577 .nav_ref_off => {
76 try navRefOff(wasm, code, mir.extraData(Mir.NavRefOff, datas[inst].payload).data, is_wasm32);
78 try navRefOff(wasm, writer, mir.extraData(Mir.NavRefOff, datas[inst].payload).data, is_wasm32);
7779 inst += 1;
7880 continue :loop tags[inst];
7981 },
......@@ -81,11 +83,11 @@ pub fn lowerToCode(emit: *Emit) Error!void {
8183 const indirect_func_idx: Wasm.ZcuIndirectFunctionSetIndex = @enumFromInt(
8284 wasm.zcu_indirect_function_set.getIndex(datas[inst].nav_index).?,
8385 );
84 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
86 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
8587 if (is_obj) {
8688 @panic("TODO");
8789 } else {
88 leb.writeUleb128(code.fixedWriter(), 1 + @intFromEnum(indirect_func_idx)) catch unreachable;
90 try writer.writeLeb128(1 + @intFromEnum(indirect_func_idx));
8991 }
9092 inst += 1;
9193 continue :loop tags[inst];
......@@ -95,52 +97,48 @@ pub fn lowerToCode(emit: *Emit) Error!void {
9597 continue :loop tags[inst];
9698 },
9799 .errors_len => {
98 try code.ensureUnusedCapacity(gpa, 6);
99 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
100 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
100101 // MIR is lowered during flush, so there is indeed only one thread at this time.
101 const errors_len = 1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len;
102 leb.writeIleb128(code.fixedWriter(), errors_len) catch unreachable;
102 const errors_len: u32 = @intCast(1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len);
103 try writer.writeLeb128(@as(i32, @bitCast(errors_len)));
103104
104105 inst += 1;
105106 continue :loop tags[inst];
106107 },
107108 .error_name_table_ref => {
108109 wasm.error_name_table_ref_count += 1;
109 try code.ensureUnusedCapacity(gpa, 11);
110110 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
111 code.appendAssumeCapacity(@intFromEnum(opcode));
111 try writer.writeByte(@intFromEnum(opcode));
112112 if (is_obj) {
113113 try wasm.out_relocs.append(gpa, .{
114 .offset = @intCast(code.items.len),
114 .offset = @intCast(writer.count),
115115 .pointee = .{ .symbol_index = try wasm.errorNameTableSymbolIndex() },
116116 .tag = if (is_wasm32) .memory_addr_leb else .memory_addr_leb64,
117117 .addend = 0,
118118 });
119 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
119 try writer.splatByteAll(0, if (is_wasm32) 5 else 10);
120120
121121 inst += 1;
122122 continue :loop tags[inst];
123123 } else {
124124 const addr: u32 = wasm.errorNameTableAddr();
125 leb.writeIleb128(code.fixedWriter(), addr) catch unreachable;
125 try writer.writeLeb128(@as(i32, @bitCast(addr)));
126126
127127 inst += 1;
128128 continue :loop tags[inst];
129129 }
130130 },
131131 .br_if, .br, .memory_grow, .memory_size => {
132 try code.ensureUnusedCapacity(gpa, 11);
133 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
134 leb.writeUleb128(code.fixedWriter(), datas[inst].label) catch unreachable;
132 try writer.writeByte(@intFromEnum(tags[inst]));
133 try writer.writeLeb128(datas[inst].label);
135134
136135 inst += 1;
137136 continue :loop tags[inst];
138137 },
139138
140139 .local_get, .local_set, .local_tee => {
141 try code.ensureUnusedCapacity(gpa, 11);
142 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
143 leb.writeUleb128(code.fixedWriter(), datas[inst].local) catch unreachable;
140 try writer.writeByte(@intFromEnum(tags[inst]));
141 try writer.writeLeb128(datas[inst].local);
144142
145143 inst += 1;
146144 continue :loop tags[inst];
......@@ -150,29 +148,27 @@ pub fn lowerToCode(emit: *Emit) Error!void {
150148 const extra_index = datas[inst].payload;
151149 const extra = mir.extraData(Mir.JumpTable, extra_index);
152150 const labels = mir.extra[extra.end..][0..extra.data.length];
153 try code.ensureUnusedCapacity(gpa, 11 + 10 * labels.len);
154 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table));
151 try writer.writeByte(@intFromEnum(std.wasm.Opcode.br_table));
155152 // -1 because default label is not part of length/depth.
156 leb.writeUleb128(code.fixedWriter(), extra.data.length - 1) catch unreachable;
157 for (labels) |label| leb.writeUleb128(code.fixedWriter(), label) catch unreachable;
153 try writer.writeLeb128(extra.data.length - 1);
154 for (labels) |label| try writer.writeLeb128(label);
158155
159156 inst += 1;
160157 continue :loop tags[inst];
161158 },
162159
163160 .call_nav => {
164 try code.ensureUnusedCapacity(gpa, 6);
165 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call));
161 try writer.writeByte(@intFromEnum(std.wasm.Opcode.call));
166162 if (is_obj) {
167163 try wasm.out_relocs.append(gpa, .{
168 .offset = @intCast(code.items.len),
164 .offset = @intCast(writer.count),
169165 .pointee = .{ .symbol_index = try wasm.navSymbolIndex(datas[inst].nav_index) },
170166 .tag = .function_index_leb,
171167 .addend = 0,
172168 });
173 code.appendNTimesAssumeCapacity(0, 5);
169 try writer.splatByteAll(0, 5);
174170 } else {
175 appendOutputFunctionIndex(code, .fromIpNav(wasm, datas[inst].nav_index));
171 try appendOutputFunctionIndex(writer, .fromIpNav(wasm, datas[inst].nav_index));
176172 }
177173
178174 inst += 1;
......@@ -180,7 +176,6 @@ pub fn lowerToCode(emit: *Emit) Error!void {
180176 },
181177
182178 .call_indirect => {
183 try code.ensureUnusedCapacity(gpa, 11);
184179 const fn_info = comp.zcu.?.typeToFunc(.fromInterned(datas[inst].ip_index)).?;
185180 const func_ty_index = wasm.getExistingFunctionType(
186181 fn_info.cc,
......@@ -188,38 +183,37 @@ pub fn lowerToCode(emit: *Emit) Error!void {
188183 .fromInterned(fn_info.return_type),
189184 target,
190185 ).?;
191 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call_indirect));
186 try writer.writeByte(@intFromEnum(std.wasm.Opcode.call_indirect));
192187 if (is_obj) {
193188 try wasm.out_relocs.append(gpa, .{
194 .offset = @intCast(code.items.len),
189 .offset = @intCast(writer.count),
195190 .pointee = .{ .type_index = func_ty_index },
196191 .tag = .type_index_leb,
197192 .addend = 0,
198193 });
199 code.appendNTimesAssumeCapacity(0, 5);
194 try writer.splatByteAll(0, 5);
200195 } else {
201196 const index: Wasm.Flush.FuncTypeIndex = .fromTypeIndex(func_ty_index, &wasm.flush_buffer);
202 leb.writeUleb128(code.fixedWriter(), @intFromEnum(index)) catch unreachable;
197 try writer.writeLeb128(@intFromEnum(index));
203198 }
204 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // table index
199 try writer.writeUleb128(0); // table index
205200
206201 inst += 1;
207202 continue :loop tags[inst];
208203 },
209204
210205 .call_tag_name => {
211 try code.ensureUnusedCapacity(gpa, 6);
212 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call));
206 try writer.writeByte(@intFromEnum(std.wasm.Opcode.call));
213207 if (is_obj) {
214208 try wasm.out_relocs.append(gpa, .{
215 .offset = @intCast(code.items.len),
209 .offset = @intCast(writer.count),
216210 .pointee = .{ .symbol_index = try wasm.tagNameSymbolIndex(datas[inst].ip_index) },
217211 .tag = .function_index_leb,
218212 .addend = 0,
219213 });
220 code.appendNTimesAssumeCapacity(0, 5);
214 try writer.splatByteAll(0, 5);
221215 } else {
222 appendOutputFunctionIndex(code, .fromTagNameType(wasm, datas[inst].ip_index));
216 try appendOutputFunctionIndex(writer, .fromTagNameType(wasm, datas[inst].ip_index));
223217 }
224218
225219 inst += 1;
......@@ -232,18 +226,17 @@ pub fn lowerToCode(emit: *Emit) Error!void {
232226 // table initialized based on the `Mir.Intrinsic` enum.
233227 const symbol_name = try wasm.internString(@tagName(datas[inst].intrinsic));
234228
235 try code.ensureUnusedCapacity(gpa, 6);
236 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call));
229 try writer.writeByte(@intFromEnum(std.wasm.Opcode.call));
237230 if (is_obj) {
238231 try wasm.out_relocs.append(gpa, .{
239 .offset = @intCast(code.items.len),
232 .offset = @intCast(writer.count),
240233 .pointee = .{ .symbol_index = try wasm.symbolNameIndex(symbol_name) },
241234 .tag = .function_index_leb,
242235 .addend = 0,
243236 });
244 code.appendNTimesAssumeCapacity(0, 5);
237 try writer.splatByteAll(0, 5);
245238 } else {
246 appendOutputFunctionIndex(code, .fromSymbolName(wasm, symbol_name));
239 try appendOutputFunctionIndex(writer, .fromSymbolName(wasm, symbol_name));
247240 }
248241
249242 inst += 1;
......@@ -251,19 +244,17 @@ pub fn lowerToCode(emit: *Emit) Error!void {
251244 },
252245
253246 .global_set_sp => {
254 try code.ensureUnusedCapacity(gpa, 6);
255 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
247 try writer.writeByte(@intFromEnum(std.wasm.Opcode.global_set));
256248 if (is_obj) {
257249 try wasm.out_relocs.append(gpa, .{
258 .offset = @intCast(code.items.len),
250 .offset = @intCast(writer.count),
259251 .pointee = .{ .symbol_index = try wasm.stackPointerSymbolIndex() },
260252 .tag = .global_index_leb,
261253 .addend = 0,
262254 });
263 code.appendNTimesAssumeCapacity(0, 5);
255 try writer.splatByteAll(0, 5);
264256 } else {
265 const sp_global: Wasm.GlobalIndex = .stack_pointer;
266 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
257 try writer.writeLeb128(@intFromEnum(Wasm.GlobalIndex.stack_pointer));
267258 }
268259
269260 inst += 1;
......@@ -271,36 +262,32 @@ pub fn lowerToCode(emit: *Emit) Error!void {
271262 },
272263
273264 .f32_const => {
274 try code.ensureUnusedCapacity(gpa, 5);
275 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.f32_const));
276 std.mem.writeInt(u32, code.addManyAsArrayAssumeCapacity(4), @bitCast(datas[inst].float32), .little);
265 try writer.writeByte(@intFromEnum(std.wasm.Opcode.f32_const));
266 try writer.writeInt(u32, @bitCast(datas[inst].float32), .little);
277267
278268 inst += 1;
279269 continue :loop tags[inst];
280270 },
281271
282272 .f64_const => {
283 try code.ensureUnusedCapacity(gpa, 9);
284 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.f64_const));
273 try writer.writeByte(@intFromEnum(std.wasm.Opcode.f64_const));
285274 const float64 = mir.extraData(Mir.Float64, datas[inst].payload).data;
286 std.mem.writeInt(u64, code.addManyAsArrayAssumeCapacity(8), float64.toInt(), .little);
275 try writer.writeInt(u64, float64.toInt(), .little);
287276
288277 inst += 1;
289278 continue :loop tags[inst];
290279 },
291280 .i32_const => {
292 try code.ensureUnusedCapacity(gpa, 6);
293 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
294 leb.writeIleb128(code.fixedWriter(), datas[inst].imm32) catch unreachable;
281 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
282 try writer.writeLeb128(datas[inst].imm32);
295283
296284 inst += 1;
297285 continue :loop tags[inst];
298286 },
299287 .i64_const => {
300 try code.ensureUnusedCapacity(gpa, 11);
301 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));
288 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i64_const));
302289 const int64: i64 = @bitCast(mir.extraData(Mir.Imm64, datas[inst].payload).data.toInt());
303 leb.writeIleb128(code.fixedWriter(), int64) catch unreachable;
290 try writer.writeLeb128(int64);
304291
305292 inst += 1;
306293 continue :loop tags[inst];
......@@ -330,9 +317,8 @@ pub fn lowerToCode(emit: *Emit) Error!void {
330317 .i64_store16,
331318 .i64_store32,
332319 => {
333 try code.ensureUnusedCapacity(gpa, 1 + 20);
334 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
335 encodeMemArg(code, mir.extraData(Mir.MemArg, datas[inst].payload).data);
320 try writer.writeByte(@intFromEnum(tags[inst]));
321 try encodeMemArg(writer, mir.extraData(Mir.MemArg, datas[inst].payload).data);
336322 inst += 1;
337323 continue :loop tags[inst];
338324 },
......@@ -466,43 +452,42 @@ pub fn lowerToCode(emit: *Emit) Error!void {
466452 .i64_clz,
467453 .i64_ctz,
468454 => {
469 try code.append(gpa, @intFromEnum(tags[inst]));
455 try writer.writeByte(@intFromEnum(tags[inst]));
470456 inst += 1;
471457 continue :loop tags[inst];
472458 },
473459
474460 .misc_prefix => {
475 try code.ensureUnusedCapacity(gpa, 6 + 6);
476461 const extra_index = datas[inst].payload;
477 const opcode = mir.extra[extra_index];
478 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
479 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
480 switch (@as(std.wasm.MiscOpcode, @enumFromInt(opcode))) {
462 const opcode: std.wasm.MiscOpcode = @enumFromInt(mir.extra[extra_index]);
463 try writer.writeByte(@intFromEnum(std.wasm.Opcode.misc_prefix));
464 try writer.writeLeb128(@intFromEnum(opcode));
465 switch (opcode) {
481466 // bulk-memory opcodes
482467 .data_drop => {
483468 const segment = mir.extra[extra_index + 1];
484 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;
469 try writer.writeLeb128(segment);
485470
486471 inst += 1;
487472 continue :loop tags[inst];
488473 },
489474 .memory_init => {
490475 const segment = mir.extra[extra_index + 1];
491 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;
492 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index
476 try writer.writeLeb128(segment);
477 try writer.writeByte(0); // memory index
493478
494479 inst += 1;
495480 continue :loop tags[inst];
496481 },
497482 .memory_fill => {
498 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index
483 try writer.writeByte(0); // memory index
499484
500485 inst += 1;
501486 continue :loop tags[inst];
502487 },
503488 .memory_copy => {
504 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // dst memory index
505 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // src memory index
489 try writer.writeByte(0); // dst memory index
490 try writer.writeByte(0); // src memory index
506491
507492 inst += 1;
508493 continue :loop tags[inst];
......@@ -534,12 +519,11 @@ pub fn lowerToCode(emit: *Emit) Error!void {
534519 comptime unreachable;
535520 },
536521 .simd_prefix => {
537 try code.ensureUnusedCapacity(gpa, 6 + 20);
538522 const extra_index = datas[inst].payload;
539 const opcode = mir.extra[extra_index];
540 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.simd_prefix));
541 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
542 switch (@as(std.wasm.SimdOpcode, @enumFromInt(opcode))) {
523 const opcode: std.wasm.SimdOpcode = @enumFromInt(mir.extra[extra_index]);
524 try writer.writeByte(@intFromEnum(std.wasm.Opcode.simd_prefix));
525 try writer.writeLeb128(@intFromEnum(opcode));
526 switch (opcode) {
543527 .v128_store,
544528 .v128_load,
545529 .v128_load8_splat,
......@@ -547,12 +531,12 @@ pub fn lowerToCode(emit: *Emit) Error!void {
547531 .v128_load32_splat,
548532 .v128_load64_splat,
549533 => {
550 encodeMemArg(code, mir.extraData(Mir.MemArg, extra_index + 1).data);
534 try encodeMemArg(writer, mir.extraData(Mir.MemArg, extra_index + 1).data);
551535 inst += 1;
552536 continue :loop tags[inst];
553537 },
554538 .v128_const, .i8x16_shuffle => {
555 code.appendSliceAssumeCapacity(std.mem.asBytes(mir.extra[extra_index + 1 ..][0..4]));
539 try writer.writeAll(std.mem.asBytes(mir.extra[extra_index + 1 ..][0..4]));
556540 inst += 1;
557541 continue :loop tags[inst];
558542 },
......@@ -571,7 +555,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
571555 .f64x2_extract_lane,
572556 .f64x2_replace_lane,
573557 => {
574 code.appendAssumeCapacity(@intCast(mir.extra[extra_index + 1]));
558 try writer.writeByte(@intCast(mir.extra[extra_index + 1]));
575559 inst += 1;
576560 continue :loop tags[inst];
577561 },
......@@ -819,13 +803,11 @@ pub fn lowerToCode(emit: *Emit) Error!void {
819803 comptime unreachable;
820804 },
821805 .atomics_prefix => {
822 try code.ensureUnusedCapacity(gpa, 6 + 20);
823
824806 const extra_index = datas[inst].payload;
825 const opcode = mir.extra[extra_index];
826 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
827 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
828 switch (@as(std.wasm.AtomicsOpcode, @enumFromInt(opcode))) {
807 const opcode: std.wasm.AtomicsOpcode = @enumFromInt(mir.extra[extra_index]);
808 try writer.writeByte(@intFromEnum(std.wasm.Opcode.atomics_prefix));
809 try writer.writeLeb128(@intFromEnum(opcode));
810 switch (opcode) {
829811 .i32_atomic_load,
830812 .i64_atomic_load,
831813 .i32_atomic_load8_u,
......@@ -892,15 +874,12 @@ pub fn lowerToCode(emit: *Emit) Error!void {
892874 .i64_atomic_rmw32_cmpxchg_u,
893875 => {
894876 const mem_arg = mir.extraData(Mir.MemArg, extra_index + 1).data;
895 encodeMemArg(code, mem_arg);
877 try encodeMemArg(writer, mem_arg);
896878 inst += 1;
897879 continue :loop tags[inst];
898880 },
899881 .atomic_fence => {
900 // Hard-codes memory index 0 since multi-memory proposal is
901 // not yet accepted nor implemented.
902 const memory_index: u32 = 0;
903 leb.writeUleb128(code.fixedWriter(), memory_index) catch unreachable;
882 try writer.writeByte(0); // memory index
904883 inst += 1;
905884 continue :loop tags[inst];
906885 },
......@@ -915,44 +894,36 @@ pub fn lowerToCode(emit: *Emit) Error!void {
915894}
916895
917896/// Asserts 20 unused capacity.
918fn encodeMemArg(code: *std.ArrayListUnmanaged(u8), mem_arg: Mir.MemArg) void {
919 assert(code.unusedCapacitySlice().len >= 20);
920 // Wasm encodes alignment as power of 2, rather than natural alignment.
921 const encoded_alignment = @ctz(mem_arg.alignment);
922 leb.writeUleb128(code.fixedWriter(), encoded_alignment) catch unreachable;
923 leb.writeUleb128(code.fixedWriter(), mem_arg.offset) catch unreachable;
897fn encodeMemArg(writer: *Writer, mem_arg: Mir.MemArg) Writer.Error!void {
898 try writer.writeLeb128(Wasm.Alignment.fromNonzeroByteUnits(mem_arg.alignment).toLog2Units());
899 try writer.writeLeb128(mem_arg.offset);
924900}
925901
926fn uavRefObj(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
902fn uavRefObj(wasm: *Wasm, writer: *Writer, value: InternPool.Index, offset: i32, is_wasm32: bool) Writer.Error!void {
927903 const comp = wasm.base.comp;
928904 const gpa = comp.gpa;
929905 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
930906
931 try code.ensureUnusedCapacity(gpa, 11);
932 code.appendAssumeCapacity(@intFromEnum(opcode));
907 try writer.writeByte(@intFromEnum(opcode));
933908
934909 try wasm.out_relocs.append(gpa, .{
935 .offset = @intCast(code.items.len),
910 .offset = @intCast(writer.count),
936911 .pointee = .{ .symbol_index = try wasm.uavSymbolIndex(value) },
937912 .tag = if (is_wasm32) .memory_addr_leb else .memory_addr_leb64,
938913 .addend = offset,
939914 });
940 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
915 try writer.splatByteAll(0, if (is_wasm32) 5 else 10);
941916}
942917
943fn uavRefExe(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
944 const comp = wasm.base.comp;
945 const gpa = comp.gpa;
918fn uavRefExe(wasm: *Wasm, writer: *Writer, value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
946919 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
947
948 try code.ensureUnusedCapacity(gpa, 11);
949 code.appendAssumeCapacity(@intFromEnum(opcode));
920 try writer.writeByte(@intFromEnum(opcode));
950921
951922 const addr = wasm.uavAddr(value);
952 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + offset))) catch unreachable;
923 try writer.writeLeb128(@as(u32, @intCast(@as(i64, addr) + offset)));
953924}
954925
955fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff, is_wasm32: bool) !void {
926fn navRefOff(wasm: *Wasm, writer: *Writer, data: Mir.NavRefOff, is_wasm32: bool) !void {
956927 const comp = wasm.base.comp;
957928 const zcu = comp.zcu.?;
958929 const ip = &zcu.intern_pool;
......@@ -961,24 +932,22 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff
961932 const nav_ty = ip.getNav(data.nav_index).typeOf(ip);
962933 assert(!ip.isFunctionType(nav_ty));
963934
964 try code.ensureUnusedCapacity(gpa, 11);
965
966935 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
967 code.appendAssumeCapacity(@intFromEnum(opcode));
936 try writer.writeByte(@intFromEnum(opcode));
968937 if (is_obj) {
969938 try wasm.out_relocs.append(gpa, .{
970 .offset = @intCast(code.items.len),
939 .offset = @intCast(writer.count),
971940 .pointee = .{ .symbol_index = try wasm.navSymbolIndex(data.nav_index) },
972941 .tag = if (is_wasm32) .memory_addr_leb else .memory_addr_leb64,
973942 .addend = data.offset,
974943 });
975 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
944 try writer.splatByteAll(0, if (is_wasm32) 5 else 10);
976945 } else {
977946 const addr = wasm.navAddr(data.nav_index);
978 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + data.offset))) catch unreachable;
947 try writer.writeLeb128(@as(i32, @bitCast(@as(u32, @intCast(@as(i64, addr) + data.offset)))));
979948 }
980949}
981950
982fn appendOutputFunctionIndex(code: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) void {
983 leb.writeUleb128(code.fixedWriter(), @intFromEnum(i)) catch unreachable;
951fn appendOutputFunctionIndex(writer: *Writer, i: Wasm.OutputFunctionIndex) Writer.Error!void {
952 return writer.writeLeb128(@intFromEnum(i));
984953}
src/arch/wasm/Mir.zig+20-22
......@@ -669,16 +669,14 @@ pub fn deinit(mir: *Mir, gpa: std.mem.Allocator) void {
669669 mir.* = undefined;
670670}
671671
672pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) std.mem.Allocator.Error!void {
673 const gpa = wasm.base.comp.gpa;
674
672pub fn lower(mir: *const Mir, wasm: *Wasm, writer: *std.Io.Writer) std.Io.Writer.Error!void {
675673 // Write the locals in the prologue of the function body.
676 try code.ensureUnusedCapacity(gpa, 5 + mir.locals.len * 6 + 38);
674 _ = try writer.writableSliceGreedy(5 + mir.locals.len * 6 + 38);
677675
678 std.leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(mir.locals.len))) catch unreachable;
676 writer.writeLeb128(@as(u32, @intCast(mir.locals.len))) catch unreachable;
679677 for (mir.locals) |local| {
680 std.leb.writeUleb128(code.fixedWriter(), @as(u32, 1)) catch unreachable;
681 code.appendAssumeCapacity(@intFromEnum(local));
678 writer.writeLeb128(@as(u32, 1)) catch unreachable;
679 writer.writeByte(@intFromEnum(local)) catch unreachable;
682680 }
683681
684682 // Stack management section of function prologue.
......@@ -686,37 +684,37 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
686684 if (stack_alignment.toByteUnits()) |align_bytes| {
687685 const sp_global: Wasm.GlobalIndex = .stack_pointer;
688686 // load stack pointer
689 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_get));
690 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
687 writer.writeByte(@intFromEnum(std.wasm.Opcode.global_get)) catch unreachable;
688 writer.writeLeb128(@intFromEnum(sp_global)) catch unreachable;
691689 // store stack pointer so we can restore it when we return from the function
692 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
693 leb.writeUleb128(code.fixedWriter(), mir.prologue.sp_local) catch unreachable;
690 writer.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
691 writer.writeLeb128(mir.prologue.sp_local) catch unreachable;
694692 // get the total stack size
695693 const aligned_stack: i32 = @intCast(stack_alignment.forward(mir.prologue.stack_size));
696 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
697 leb.writeIleb128(code.fixedWriter(), aligned_stack) catch unreachable;
694 writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
695 writer.writeLeb128(aligned_stack) catch unreachable;
698696 // subtract it from the current stack pointer
699 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_sub));
697 writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_sub)) catch unreachable;
700698 // Get negative stack alignment
701699 const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;
702 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
703 leb.writeIleb128(code.fixedWriter(), neg_stack_align) catch unreachable;
700 writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
701 writer.writeLeb128(neg_stack_align) catch unreachable;
704702 // Bitwise-and the value to get the new stack pointer to ensure the
705703 // pointers are aligned with the abi alignment.
706 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_and));
704 writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_and)) catch unreachable;
707705 // The bottom will be used to calculate all stack pointer offsets.
708 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
709 leb.writeUleb128(code.fixedWriter(), mir.prologue.bottom_stack_local) catch unreachable;
706 writer.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
707 writer.writeLeb128(mir.prologue.bottom_stack_local) catch unreachable;
710708 // Store the current stack pointer value into the global stack pointer so other function calls will
711709 // start from this value instead and not overwrite the current stack.
712 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
713 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
710 writer.writeByte(@intFromEnum(std.wasm.Opcode.global_set)) catch unreachable;
711 writer.writeLeb128(@intFromEnum(sp_global)) catch unreachable;
714712 }
715713
716714 var emit: Emit = .{
717715 .mir = mir.*,
718716 .wasm = wasm,
719 .code = code,
717 .writer = writer,
720718 };
721719 try emit.lowerToCode();
722720}
src/link/Wasm.zig+11-12
......@@ -28,6 +28,7 @@ const fs = std.fs;
2828const leb = std.leb;
2929const log = std.log.scoped(.link);
3030const mem = std.mem;
31const Writer = std.Io.Writer;
3132
3233const Mir = @import("../arch/wasm/Mir.zig");
3334const CodeGen = @import("../arch/wasm/CodeGen.zig");
......@@ -2087,11 +2088,9 @@ pub const Expr = enum(u32) {
20872088 pub const end = @intFromEnum(std.wasm.Opcode.end);
20882089
20892090 pub fn slice(index: Expr, wasm: *const Wasm) [:end]const u8 {
2090 const start_slice = wasm.string_bytes.items[@intFromEnum(index)..];
2091 const end_pos = Object.exprEndPos(start_slice, 0) catch |err| switch (err) {
2092 error.InvalidInitOpcode => unreachable,
2093 };
2094 return start_slice[0..end_pos :end];
2091 var r: std.Io.Reader = .fixed(wasm.string_bytes.items[@intFromEnum(index)..]);
2092 Object.skipInit(&r) catch unreachable;
2093 return r.buffered()[0 .. r.seek - 1 :end];
20952094 }
20962095};
20972096
......@@ -2126,7 +2125,7 @@ pub const FunctionType = extern struct {
21262125 wasm: *const Wasm,
21272126 ft: FunctionType,
21282127
2129 pub fn format(self: Formatter, writer: *std.io.Writer) std.io.Writer.Error!void {
2128 pub fn format(self: Formatter, writer: *Writer) Writer.Error!void {
21302129 const params = self.ft.params.slice(self.wasm);
21312130 const returns = self.ft.returns.slice(self.wasm);
21322131
......@@ -2905,7 +2904,7 @@ pub const Feature = packed struct(u8) {
29052904 @"=",
29062905 };
29072906
2908 pub fn format(feature: Feature, writer: *std.io.Writer) std.io.Writer.Error!void {
2907 pub fn format(feature: Feature, writer: *Writer) Writer.Error!void {
29092908 try writer.print("{s} {s}", .{ @tagName(feature.prefix), @tagName(feature.tag) });
29102909 }
29112910
......@@ -3037,16 +3036,16 @@ fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void {
30373036 const stat = try obj.file.stat();
30383037 const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig;
30393038
3040 const file_contents = try gpa.alloc(u8, size);
3041 defer gpa.free(file_contents);
3039 var br: std.Io.Reader = .fixed(try gpa.alloc(u8, size));
3040 defer gpa.free(br.buffered());
30423041
3043 const n = try obj.file.preadAll(file_contents, 0);
3044 if (n != file_contents.len) return error.UnexpectedEndOfFile;
3042 const n = try obj.file.preadAll(br.buffered(), 0);
3043 if (n != br.bufferedLen()) return error.UnexpectedEndOfFile;
30453044
30463045 var ss: Object.ScratchSpace = .{};
30473046 defer ss.deinit(gpa);
30483047
3049 const object = try Object.parse(wasm, file_contents, obj.path, null, wasm.object_host_name, &ss, obj.must_link, gc_sections);
3048 const object = try Object.parse(wasm, &br, obj.path, null, wasm.object_host_name, &ss, obj.must_link, gc_sections);
30503049 wasm.objects.appendAssumeCapacity(object);
30513050}
30523051
src/link/Wasm/Archive.zig+2-3
......@@ -167,9 +167,8 @@ pub fn parseObject(
167167 };
168168
169169 const object_file_size = try header.parsedSize();
170 const contents = file_contents[object_offset + @sizeOf(Header) ..][0..object_file_size];
171
172 return Object.parse(wasm, contents, path, object_name, host_name, scratch_space, must_link, gc_sections);
170 var r: std.io.Reader = .fixed(file_contents[object_offset + @sizeOf(Header) ..][0..object_file_size]);
171 return Object.parse(wasm, &r, path, object_name, host_name, scratch_space, must_link, gc_sections);
173172}
174173
175174const Archive = @This();
src/link/Wasm/Flush.zig+468-553
......@@ -16,9 +16,9 @@ const build_options = @import("build_options");
1616const std = @import("std");
1717const Allocator = std.mem.Allocator;
1818const mem = std.mem;
19const leb = std.leb;
2019const log = std.log.scoped(.link);
2120const assert = std.debug.assert;
21const Writer = std.Io.Writer;
2222
2323/// Ordered list of data segments that will appear in the final binary.
2424/// When sorted, to-be-merged segments will be made adjacent.
......@@ -557,13 +557,12 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
557557 // Index of the data section. Used to tell relocation table where the section lives.
558558 var data_section_index: ?u32 = null;
559559
560 const binary_bytes = &f.binary_bytes;
561 assert(binary_bytes.items.len == 0);
560 assert(f.binary_bytes.items.len == 0);
561 var aw: Writer.Allocating = .fromArrayList(gpa, &f.binary_bytes);
562 defer f.binary_bytes = aw.toArrayList();
563 const w = &aw.writer;
562564
563 try binary_bytes.appendSlice(gpa, &std.wasm.magic ++ &std.wasm.version);
564 assert(binary_bytes.items.len == 8);
565
566 const binary_writer = binary_bytes.writer(gpa);
565 try w.writeAll(&std.wasm.magic ++ &std.wasm.version);
567566
568567 // Type section.
569568 for (f.function_imports.values()) |id| {
......@@ -573,22 +572,18 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
573572 try f.func_types.put(gpa, function.typeIndex(wasm), {});
574573 }
575574 if (f.func_types.entries.len != 0) {
576 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
575 const header_offset = try reserveVecSectionHeader(w);
577576 for (f.func_types.keys()) |func_type_index| {
578577 const func_type = func_type_index.ptr(wasm);
579 try leb.writeUleb128(binary_writer, std.wasm.function_type);
578 try w.writeLeb128(std.wasm.function_type);
580579 const params = func_type.params.slice(wasm);
581 try leb.writeUleb128(binary_writer, @as(u32, @intCast(params.len)));
582 for (params) |param_ty| {
583 try leb.writeUleb128(binary_writer, @intFromEnum(param_ty));
584 }
580 try w.writeLeb128(params.len);
581 for (params) |param_ty| try w.writeLeb128(@intFromEnum(param_ty));
585582 const returns = func_type.returns.slice(wasm);
586 try leb.writeUleb128(binary_writer, @as(u32, @intCast(returns.len)));
587 for (returns) |ret_ty| {
588 try leb.writeUleb128(binary_writer, @intFromEnum(ret_ty));
589 }
583 try w.writeLeb128(returns.len);
584 for (returns) |ret_ty| try w.writeLeb128(@intFromEnum(ret_ty));
590585 }
591 replaceVecSectionHeader(binary_bytes, header_offset, .type, @intCast(f.func_types.entries.len));
586 replaceVecSectionHeader(&aw, header_offset, .type, @intCast(f.func_types.entries.len));
592587 section_index += 1;
593588 }
594589
......@@ -601,42 +596,42 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
601596 // Import section
602597 {
603598 var total_imports: usize = 0;
604 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
599 const header_offset = try reserveVecSectionHeader(w);
605600
606601 for (f.function_imports.values()) |id| {
607602 const module_name = id.moduleName(wasm).slice(wasm).?;
608 try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len)));
609 try binary_writer.writeAll(module_name);
603 try w.writeLeb128(module_name.len);
604 try w.writeAll(module_name);
610605
611606 const name = id.importName(wasm).slice(wasm);
612 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
613 try binary_writer.writeAll(name);
607 try w.writeLeb128(name.len);
608 try w.writeAll(name);
614609
615 try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.function));
610 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.function));
616611 const type_index: FuncTypeIndex = .fromTypeIndex(id.functionType(wasm), f);
617 try leb.writeUleb128(binary_writer, @intFromEnum(type_index));
612 try w.writeLeb128(@intFromEnum(type_index));
618613 }
619614 total_imports += f.function_imports.entries.len;
620615
621616 for (wasm.table_imports.values()) |id| {
622617 const table_import = id.value(wasm);
623618 const module_name = table_import.module_name.slice(wasm);
624 try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len)));
625 try binary_writer.writeAll(module_name);
619 try w.writeLeb128(module_name.len);
620 try w.writeAll(module_name);
626621
627622 const name = table_import.name.slice(wasm);
628 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
629 try binary_writer.writeAll(name);
623 try w.writeLeb128(name.len);
624 try w.writeAll(name);
630625
631 try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.table));
632 try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table_import.flags.ref_type.to())));
633 try emitLimits(gpa, binary_bytes, table_import.limits());
626 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.table));
627 try w.writeLeb128(@intFromEnum(@as(std.wasm.RefType, table_import.flags.ref_type.to())));
628 try emitLimits(w, table_import.limits());
634629 }
635630 total_imports += wasm.table_imports.entries.len;
636631
637632 if (import_memory) {
638633 const name = if (is_obj) wasm.preloaded_strings.__linear_memory else wasm.preloaded_strings.memory;
639 try emitMemoryImport(wasm, binary_bytes, name, &.{
634 try emitMemoryImport(wasm, w, name, &.{
640635 // TODO the import_memory option needs to specify from which module
641636 .module_name = wasm.object_host_name.unwrap().?,
642637 .limits_min = wasm.memories.limits.min,
......@@ -650,215 +645,209 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
650645
651646 for (f.global_imports.values()) |id| {
652647 const module_name = id.moduleName(wasm).slice(wasm).?;
653 try leb.writeUleb128(binary_writer, @as(u32, @intCast(module_name.len)));
654 try binary_writer.writeAll(module_name);
648 try w.writeLeb128(module_name.len);
649 try w.writeAll(module_name);
655650
656651 const name = id.importName(wasm).slice(wasm);
657 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
658 try binary_writer.writeAll(name);
652 try w.writeLeb128(name.len);
653 try w.writeAll(name);
659654
660 try binary_writer.writeByte(@intFromEnum(std.wasm.ExternalKind.global));
655 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.global));
661656 const global_type = id.globalType(wasm);
662 try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.Valtype, global_type.valtype)));
663 try binary_writer.writeByte(@intFromBool(global_type.mutable));
657 try w.writeLeb128(@intFromEnum(global_type.valtype));
658 try w.writeByte(@intFromBool(global_type.mutable));
664659 }
665660 total_imports += f.global_imports.entries.len;
666661
667662 if (total_imports > 0) {
668 replaceVecSectionHeader(binary_bytes, header_offset, .import, @intCast(total_imports));
663 replaceVecSectionHeader(&aw, header_offset, .import, @intCast(total_imports));
669664 section_index += 1;
670665 } else {
671 binary_bytes.shrinkRetainingCapacity(header_offset);
666 aw.shrinkRetainingCapacity(header_offset);
672667 }
673668 }
674669
675670 // Function section
676671 if (wasm.functions.count() != 0) {
677 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
672 const header_offset = try reserveVecSectionHeader(w);
678673 for (wasm.functions.keys()) |function| {
679674 const index: FuncTypeIndex = .fromTypeIndex(function.typeIndex(wasm), f);
680 try leb.writeUleb128(binary_writer, @intFromEnum(index));
675 try w.writeLeb128(@intFromEnum(index));
681676 }
682677
683 replaceVecSectionHeader(binary_bytes, header_offset, .function, @intCast(wasm.functions.count()));
678 replaceVecSectionHeader(&aw, header_offset, .function, @intCast(wasm.functions.count()));
684679 section_index += 1;
685680 }
686681
687682 // Table section
688683 if (wasm.tables.entries.len > 0) {
689 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
684 const header_offset = try reserveVecSectionHeader(w);
690685
691686 for (wasm.tables.keys()) |table| {
692 try leb.writeUleb128(binary_writer, @intFromEnum(@as(std.wasm.RefType, table.refType(wasm))));
693 try emitLimits(gpa, binary_bytes, table.limits(wasm));
687 try w.writeLeb128(@intFromEnum(table.refType(wasm)));
688 try emitLimits(w, table.limits(wasm));
694689 }
695690
696 replaceVecSectionHeader(binary_bytes, header_offset, .table, @intCast(wasm.tables.entries.len));
691 replaceVecSectionHeader(&aw, header_offset, .table, @intCast(wasm.tables.entries.len));
697692 section_index += 1;
698693 }
699694
700695 // Memory section. wasm currently only supports 1 linear memory segment.
701696 if (!import_memory) {
702 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
703 try emitLimits(gpa, binary_bytes, wasm.memories.limits);
704 replaceVecSectionHeader(binary_bytes, header_offset, .memory, 1);
697 const header_offset = try reserveVecSectionHeader(w);
698 try emitLimits(w, wasm.memories.limits);
699 replaceVecSectionHeader(&aw, header_offset, .memory, 1);
705700 section_index += 1;
706701 }
707702
708703 // Global section.
709704 const globals_len: u32 = @intCast(wasm.globals.entries.len);
710705 if (globals_len > 0) {
711 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
706 const header_offset = try reserveVecSectionHeader(w);
712707
713708 for (wasm.globals.keys()) |global_resolution| {
714709 switch (global_resolution.unpack(wasm)) {
715710 .unresolved => unreachable,
716 .__heap_base => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.heap_base),
717 .__heap_end => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.heap_end),
718 .__stack_pointer => try appendGlobal(gpa, binary_bytes, 1, virtual_addrs.stack_pointer),
719 .__tls_align => try appendGlobal(gpa, binary_bytes, 0, @intCast(virtual_addrs.tls_align.toByteUnits().?)),
720 .__tls_base => try appendGlobal(gpa, binary_bytes, 1, virtual_addrs.tls_base.?),
721 .__tls_size => try appendGlobal(gpa, binary_bytes, 0, virtual_addrs.tls_size.?),
711 .__heap_base => try appendGlobal(w, false, virtual_addrs.heap_base),
712 .__heap_end => try appendGlobal(w, false, virtual_addrs.heap_end),
713 .__stack_pointer => try appendGlobal(w, true, virtual_addrs.stack_pointer),
714 .__tls_align => try appendGlobal(w, false, @intCast(virtual_addrs.tls_align.toByteUnits().?)),
715 .__tls_base => try appendGlobal(w, true, virtual_addrs.tls_base.?),
716 .__tls_size => try appendGlobal(w, false, virtual_addrs.tls_size.?),
722717 .object_global => |i| {
723718 const global = i.ptr(wasm);
724 try binary_bytes.appendSlice(gpa, &.{
719 try w.writeAll(&.{
725720 @intFromEnum(@as(std.wasm.Valtype, global.flags.global_type.valtype.to())),
726721 @intFromBool(global.flags.global_type.mutable),
727722 });
728 try emitExpr(wasm, binary_bytes, global.expr);
723 try emitExpr(wasm, w, global.expr);
729724 },
730725 .nav_exe => unreachable, // Zig source code currently cannot represent this.
731726 .nav_obj => unreachable, // Zig source code currently cannot represent this.
732727 }
733728 }
734729
735 replaceVecSectionHeader(binary_bytes, header_offset, .global, globals_len);
730 replaceVecSectionHeader(&aw, header_offset, .global, globals_len);
736731 section_index += 1;
737732 }
738733
739734 // Export section
740735 {
741 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
736 const header_offset = try reserveVecSectionHeader(w);
742737 var exports_len: usize = 0;
743738
744739 for (wasm.function_exports.keys(), wasm.function_exports.values()) |exp_name, function_index| {
745740 const name = exp_name.slice(wasm);
746 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
747 try binary_bytes.appendSlice(gpa, name);
748 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.function));
741 try w.writeLeb128(name.len);
742 try w.writeAll(name);
743 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.function));
749744 const func_index = Wasm.OutputFunctionIndex.fromFunctionIndex(wasm, function_index);
750 try leb.writeUleb128(binary_writer, @intFromEnum(func_index));
745 try w.writeLeb128(@intFromEnum(func_index));
751746 }
752747 exports_len += wasm.function_exports.entries.len;
753748
754749 if (wasm.export_table and f.indirect_function_table.entries.len > 0) {
755750 const name = "__indirect_function_table";
756751 const index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?);
757 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
758 try binary_bytes.appendSlice(gpa, name);
759 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.table));
760 try leb.writeUleb128(binary_writer, index);
752 try w.writeLeb128(name.len);
753 try w.writeAll(name);
754 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.table));
755 try w.writeLeb128(index);
761756 exports_len += 1;
762757 }
763758
764759 if (export_memory) {
765760 const name = "memory";
766 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
767 try binary_bytes.appendSlice(gpa, name);
768 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory));
769 try leb.writeUleb128(binary_writer, @as(u32, 0));
761 try w.writeLeb128(name.len);
762 try w.writeAll(name);
763 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.memory));
764 try w.writeUleb128(0);
770765 exports_len += 1;
771766 }
772767
773768 for (wasm.global_exports.items) |exp| {
774769 const name = exp.name.slice(wasm);
775 try leb.writeUleb128(binary_writer, @as(u32, @intCast(name.len)));
776 try binary_bytes.appendSlice(gpa, name);
777 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.global));
778 try leb.writeUleb128(binary_writer, @intFromEnum(exp.global_index));
770 try w.writeLeb128(name.len);
771 try w.writeAll(name);
772 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.global));
773 try w.writeLeb128(@intFromEnum(exp.global_index));
779774 }
780775 exports_len += wasm.global_exports.items.len;
781776
782777 if (exports_len > 0) {
783 replaceVecSectionHeader(binary_bytes, header_offset, .@"export", @intCast(exports_len));
778 replaceVecSectionHeader(&aw, header_offset, .@"export", @intCast(exports_len));
784779 section_index += 1;
785780 } else {
786 binary_bytes.shrinkRetainingCapacity(header_offset);
781 aw.shrinkRetainingCapacity(header_offset);
787782 }
788783 }
789784
790785 // start section
791786 if (wasm.functions.getIndex(.__wasm_init_memory)) |func_index| {
792 try emitStartSection(gpa, binary_bytes, .fromFunctionIndex(wasm, @enumFromInt(func_index)));
787 try emitStartSection(&aw, .fromFunctionIndex(wasm, @enumFromInt(func_index)));
793788 } else if (Wasm.OutputFunctionIndex.fromResolution(wasm, wasm.entry_resolution)) |func_index| {
794 try emitStartSection(gpa, binary_bytes, func_index);
789 try emitStartSection(&aw, func_index);
795790 }
796791
797792 // element section
798793 if (f.indirect_function_table.entries.len > 0) {
799 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
794 const header_offset = try reserveVecSectionHeader(w);
800795
801796 // indirect function table elements
802797 const table_index: u32 = @intCast(wasm.tables.getIndex(.__indirect_function_table).?);
803798 // passive with implicit 0-index table or set table index manually
804799 const flags: u32 = if (table_index == 0) 0x0 else 0x02;
805 try leb.writeUleb128(binary_writer, flags);
806 if (flags == 0x02) {
807 try leb.writeUleb128(binary_writer, table_index);
808 }
800 try w.writeLeb128(flags);
801 if (flags == 0x02) try w.writeLeb128(table_index);
809802 // We start at index 1, so unresolved function pointers are invalid
810 try emitInit(binary_writer, .{ .i32_const = 1 });
811 if (flags == 0x02) {
812 try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref
813 }
814 try leb.writeUleb128(binary_writer, @as(u32, @intCast(f.indirect_function_table.entries.len)));
815 for (f.indirect_function_table.keys()) |func_index| {
816 try leb.writeUleb128(binary_writer, @intFromEnum(func_index));
817 }
803 try emitInit(w, .{ .i32_const = 1 });
804 if (flags == 0x02) try w.writeUleb128(0); // represents funcref
805 try w.writeLeb128(f.indirect_function_table.entries.len);
806 for (f.indirect_function_table.keys()) |func_index| try w.writeLeb128(@intFromEnum(func_index));
818807
819 replaceVecSectionHeader(binary_bytes, header_offset, .element, 1);
808 replaceVecSectionHeader(&aw, header_offset, .element, 1);
820809 section_index += 1;
821810 }
822811
823812 // When the shared-memory option is enabled, we *must* emit the 'data count' section.
824813 if (f.data_segment_groups.items.len > 0) {
825 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
826 replaceVecSectionHeader(binary_bytes, header_offset, .data_count, @intCast(f.data_segment_groups.items.len));
814 const header_offset = try reserveVecSectionHeader(w);
815 replaceVecSectionHeader(&aw, header_offset, .data_count, @intCast(f.data_segment_groups.items.len));
827816 }
828817
829818 // Code section.
830819 if (wasm.functions.count() != 0) {
831 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
820 const header_offset = try reserveVecSectionHeader(w);
832821
833822 for (wasm.functions.keys()) |resolution| switch (resolution.unpack(wasm)) {
834823 .unresolved => unreachable,
835824 .__wasm_apply_global_tls_relocs => @panic("TODO lower __wasm_apply_global_tls_relocs"),
836825 .__wasm_call_ctors => {
837 const code_start = try reserveSize(gpa, binary_bytes);
838 defer replaceSize(binary_bytes, code_start);
839 try emitCallCtorsFunction(wasm, binary_bytes);
826 const code_start = try reserveSizeHeader(w);
827 defer replaceSizeHeader(&aw, code_start);
828 try emitCallCtorsFunction(wasm, w);
840829 },
841830 .__wasm_init_memory => {
842 const code_start = try reserveSize(gpa, binary_bytes);
843 defer replaceSize(binary_bytes, code_start);
844 try emitInitMemoryFunction(wasm, binary_bytes, &virtual_addrs);
831 const code_start = try reserveSizeHeader(w);
832 defer replaceSizeHeader(&aw, code_start);
833 try emitInitMemoryFunction(wasm, w, &virtual_addrs);
845834 },
846835 .__wasm_init_tls => {
847 const code_start = try reserveSize(gpa, binary_bytes);
848 defer replaceSize(binary_bytes, code_start);
849 try emitInitTlsFunction(wasm, binary_bytes);
836 const code_start = try reserveSizeHeader(w);
837 defer replaceSizeHeader(&aw, code_start);
838 try emitInitTlsFunction(wasm, w);
850839 },
851840 .object_function => |i| {
852841 const ptr = i.ptr(wasm);
853842 const code = ptr.code.slice(wasm);
854 try leb.writeUleb128(binary_writer, code.len);
855 const code_start = binary_bytes.items.len;
856 try binary_bytes.appendSlice(gpa, code);
857 if (!is_obj) applyRelocs(binary_bytes.items[code_start..], ptr.offset, ptr.relocations(wasm), wasm);
843 try w.writeLeb128(code.len);
844 const code_start = w.end;
845 try w.writeAll(code);
846 if (!is_obj) applyRelocs(aw.getWritten()[code_start..], ptr.offset, ptr.relocations(wasm), wasm);
858847 },
859848 .zcu_func => |i| {
860 const code_start = try reserveSize(gpa, binary_bytes);
861 defer replaceSize(binary_bytes, code_start);
849 const code_start = try reserveSizeHeader(w);
850 defer replaceSizeHeader(&aw, code_start);
862851
863852 log.debug("lowering function code for '{s}'", .{resolution.name(wasm).?});
864853
......@@ -867,7 +856,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
867856 const ip_index = i.key(wasm).*;
868857 switch (ip.indexToKey(ip_index)) {
869858 .enum_type => {
870 try emitTagNameFunction(wasm, binary_bytes, f.data_segments.get(.__zig_tag_name_table).?, i.value(wasm).tag_name.table_index, ip_index);
859 try emitTagNameFunction(wasm, w, f.data_segments.get(.__zig_tag_name_table).?, i.value(wasm).tag_name.table_index, ip_index);
871860 },
872861 else => {
873862 const func = i.value(wasm).function;
......@@ -882,13 +871,13 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
882871 .func_tys = undefined,
883872 .error_name_table_ref_count = undefined,
884873 };
885 try mir.lower(wasm, binary_bytes);
874 try mir.lower(wasm, w);
886875 },
887876 }
888877 },
889878 };
890879
891 replaceVecSectionHeader(binary_bytes, header_offset, .code, @intCast(wasm.functions.entries.len));
880 replaceVecSectionHeader(&aw, header_offset, .code, @intCast(wasm.functions.entries.len));
892881 code_section_index = section_index;
893882 section_index += 1;
894883 }
......@@ -924,7 +913,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
924913
925914 // Data section.
926915 if (f.data_segment_groups.items.len != 0) {
927 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
916 const header_offset = try reserveVecSectionHeader(w);
928917
929918 var group_index: u32 = 0;
930919 var segment_offset: u32 = 0;
......@@ -932,7 +921,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
932921 var group_end_addr = f.data_segment_groups.items[group_index].end_addr;
933922 for (segment_ids, segment_vaddrs) |segment_id, segment_vaddr| {
934923 if (segment_vaddr >= group_end_addr) {
935 try binary_bytes.appendNTimes(gpa, 0, group_end_addr - group_start_addr - segment_offset);
924 try w.splatByteAll(0, group_end_addr - group_start_addr - segment_offset);
936925 group_index += 1;
937926 if (group_index >= f.data_segment_groups.items.len) {
938927 // All remaining segments are zero.
......@@ -946,12 +935,10 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
946935 const group_size = group_end_addr - group_start_addr;
947936 log.debug("emit data section group, {d} bytes", .{group_size});
948937 const flags: Object.DataSegmentFlags = if (segment_id.isPassive(wasm)) .passive else .active;
949 try leb.writeUleb128(binary_writer, @intFromEnum(flags));
938 try w.writeLeb128(@intFromEnum(flags));
950939 // Passive segments are initialized at runtime.
951 if (flags != .passive) {
952 try emitInit(binary_writer, .{ .i32_const = @as(i32, @bitCast(group_start_addr)) });
953 }
954 try leb.writeUleb128(binary_writer, group_size);
940 if (flags != .passive) try emitInit(w, .{ .i32_const = @as(i32, @bitCast(group_start_addr)) });
941 try w.writeLeb128(group_size);
955942 }
956943 if (segment_id.isEmpty(wasm)) {
957944 // It counted for virtual memory but it does not go into the binary.
......@@ -960,62 +947,62 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
960947
961948 // Padding for alignment.
962949 const needed_offset = segment_vaddr - group_start_addr;
963 try binary_bytes.appendNTimes(gpa, 0, needed_offset - segment_offset);
950 try w.splatByteAll(0, needed_offset - segment_offset);
964951 segment_offset = needed_offset;
965952
966 const code_start = binary_bytes.items.len;
953 const code_start = w.end;
967954 append: {
968955 const code = switch (segment_id.unpack(wasm)) {
969956 .__heap_base => {
970 mem.writeInt(u32, try binary_bytes.addManyAsArray(gpa, 4), virtual_addrs.heap_base, .little);
957 try w.writeInt(u32, virtual_addrs.heap_base, .little);
971958 break :append;
972959 },
973960 .__heap_end => {
974 mem.writeInt(u32, try binary_bytes.addManyAsArray(gpa, 4), virtual_addrs.heap_end, .little);
961 try w.writeInt(u32, virtual_addrs.heap_end, .little);
975962 break :append;
976963 },
977964 .__zig_error_names => {
978 try binary_bytes.appendSlice(gpa, wasm.error_name_bytes.items);
965 try w.writeAll(wasm.error_name_bytes.items);
979966 break :append;
980967 },
981968 .__zig_error_name_table => {
982969 if (is_obj) @panic("TODO error name table reloc");
983970 const base = f.data_segments.get(.__zig_error_names).?;
984971 if (!is64) {
985 try emitTagNameTable(gpa, binary_bytes, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u32);
972 try emitTagNameTable(w, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u32);
986973 } else {
987 try emitTagNameTable(gpa, binary_bytes, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u64);
974 try emitTagNameTable(w, wasm.error_name_offs.items, wasm.error_name_bytes.items, base, u64);
988975 }
989976 break :append;
990977 },
991978 .__zig_tag_names => {
992 try binary_bytes.appendSlice(gpa, wasm.tag_name_bytes.items);
979 try w.writeAll(wasm.tag_name_bytes.items);
993980 break :append;
994981 },
995982 .__zig_tag_name_table => {
996983 if (is_obj) @panic("TODO tag name table reloc");
997984 const base = f.data_segments.get(.__zig_tag_names).?;
998985 if (!is64) {
999 try emitTagNameTable(gpa, binary_bytes, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u32);
986 try emitTagNameTable(w, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u32);
1000987 } else {
1001 try emitTagNameTable(gpa, binary_bytes, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u64);
988 try emitTagNameTable(w, wasm.tag_name_offs.items, wasm.tag_name_bytes.items, base, u64);
1002989 }
1003990 break :append;
1004991 },
1005992 .object => |i| {
1006993 const ptr = i.ptr(wasm);
1007 try binary_bytes.appendSlice(gpa, ptr.payload.slice(wasm));
1008 if (!is_obj) applyRelocs(binary_bytes.items[code_start..], ptr.offset, ptr.relocations(wasm), wasm);
994 try w.writeAll(ptr.payload.slice(wasm));
995 if (!is_obj) applyRelocs(aw.getWritten()[code_start..], ptr.offset, ptr.relocations(wasm), wasm);
1009996 break :append;
1010997 },
1011998 inline .uav_exe, .uav_obj, .nav_exe, .nav_obj => |i| i.value(wasm).code,
1012999 };
1013 try binary_bytes.appendSlice(gpa, code.slice(wasm));
1000 try w.writeAll(code.slice(wasm));
10141001 }
1015 segment_offset += @intCast(binary_bytes.items.len - code_start);
1002 segment_offset += @intCast(w.end - code_start);
10161003 }
10171004
1018 replaceVecSectionHeader(binary_bytes, header_offset, .data, @intCast(f.data_segment_groups.items.len));
1005 replaceVecSectionHeader(&aw, header_offset, .data, @intCast(f.data_segment_groups.items.len));
10191006 data_section_index = section_index;
10201007 section_index += 1;
10211008 }
......@@ -1023,7 +1010,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
10231010 if (is_obj) {
10241011 @panic("TODO emit link section for object file and emit modified relocations");
10251012 } else if (comp.config.debug_format != .strip) {
1026 try emitNameSection(wasm, f.data_segment_groups.items, binary_bytes);
1013 try emitNameSection(wasm, &aw, f.data_segment_groups.items);
10271014 }
10281015
10291016 if (comp.config.debug_format != .strip) {
......@@ -1033,17 +1020,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
10331020 .none => {},
10341021 .fast => {
10351022 var id: [16]u8 = undefined;
1036 std.crypto.hash.sha3.TurboShake128(null).hash(binary_bytes.items, &id, .{});
1023 std.crypto.hash.sha3.TurboShake128(null).hash(w.buffered(), &id, .{});
10371024 var uuid: [36]u8 = undefined;
10381025 _ = try std.fmt.bufPrint(&uuid, "{x}-{x}-{x}-{x}-{x}", .{
10391026 id[0..4], id[4..6], id[6..8], id[8..10], id[10..],
10401027 });
1041 try emitBuildIdSection(gpa, binary_bytes, &uuid);
1028 try emitBuildIdSection(&aw, &uuid);
10421029 },
10431030 .hexstring => |hs| {
10441031 var buffer: [32 * 2]u8 = undefined;
10451032 const str = std.fmt.bufPrint(&buffer, "{x}", .{hs.toSlice()}) catch unreachable;
1046 try emitBuildIdSection(gpa, binary_bytes, str);
1033 try emitBuildIdSection(&aw, str);
10471034 },
10481035 else => |mode| {
10491036 var err = try diags.addErrorWithNotes(0);
......@@ -1054,14 +1041,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
10541041 var debug_bytes = std.ArrayList(u8).init(gpa);
10551042 defer debug_bytes.deinit();
10561043
1057 try emitProducerSection(gpa, binary_bytes);
1058 try emitFeaturesSection(gpa, binary_bytes, target);
1044 try emitProducerSection(&aw);
1045 emitFeaturesSection(&aw, target) catch |err| switch (err) {
1046 error.WriteFailed => return error.OutOfMemory,
1047 };
10591048 }
10601049
10611050 // Finally, write the entire binary into the file.
10621051 const file = wasm.base.file.?;
1063 try file.pwriteAll(binary_bytes.items, 0);
1064 try file.setEndPos(binary_bytes.items.len);
1052 const contents = aw.getWritten();
1053 try file.setEndPos(contents.len);
1054 try file.pwriteAll(contents, 0);
10651055}
10661056
10671057const VirtualAddrs = struct {
......@@ -1076,170 +1066,155 @@ const VirtualAddrs = struct {
10761066
10771067fn emitNameSection(
10781068 wasm: *Wasm,
1069 aw: *Writer.Allocating,
10791070 data_segment_groups: []const DataSegmentGroup,
1080 binary_bytes: *std.ArrayListUnmanaged(u8),
10811071) !void {
10821072 const f = &wasm.flush_buffer;
1083 const comp = wasm.base.comp;
1084 const gpa = comp.gpa;
1073 const w = &aw.writer;
1074 const header_offset = try reserveSectionHeader(w);
1075 defer replaceSectionHeader(aw, header_offset, @intFromEnum(std.wasm.Section.custom));
10851076
1086 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1087 defer writeCustomSectionHeader(binary_bytes, header_offset);
1088
1089 const name_name = "name";
1090 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, name_name.len));
1091 try binary_bytes.appendSlice(gpa, name_name);
1077 const section_name = "name";
1078 try w.writeLeb128(section_name.len);
1079 try w.writeAll(section_name);
10921080
10931081 {
1094 const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1095 defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.function));
1096
1097 const total_functions: u32 = @intCast(f.function_imports.entries.len + wasm.functions.entries.len);
1098 try leb.writeUleb128(binary_bytes.writer(gpa), total_functions);
1082 const sub_header_offset = try reserveSectionHeader(w);
1083 defer replaceSectionHeader(aw, sub_header_offset, @intFromEnum(std.wasm.NameSubsection.function));
10991084
1085 try w.writeLeb128(f.function_imports.entries.len + wasm.functions.entries.len);
11001086 for (f.function_imports.keys(), 0..) |name_index, function_index| {
11011087 const name = name_index.slice(wasm);
1102 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index)));
1103 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1104 try binary_bytes.appendSlice(gpa, name);
1088 try w.writeLeb128(function_index);
1089 try w.writeLeb128(name.len);
1090 try w.writeAll(name);
11051091 }
11061092 for (wasm.functions.keys(), f.function_imports.entries.len..) |resolution, function_index| {
11071093 const name = resolution.name(wasm).?;
1108 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(function_index)));
1109 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1110 try binary_bytes.appendSlice(gpa, name);
1094 try w.writeLeb128(function_index);
1095 try w.writeLeb128(name.len);
1096 try w.writeAll(name);
11111097 }
11121098 }
11131099
11141100 {
1115 const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1116 defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.global));
1117
1118 const total_globals: u32 = @intCast(f.global_imports.entries.len + wasm.globals.entries.len);
1119 try leb.writeUleb128(binary_bytes.writer(gpa), total_globals);
1101 const sub_header_offset = try reserveSectionHeader(w);
1102 defer replaceSectionHeader(aw, sub_header_offset, @intFromEnum(std.wasm.NameSubsection.global));
11201103
1104 try w.writeLeb128(f.global_imports.entries.len + wasm.globals.entries.len);
11211105 for (f.global_imports.keys(), 0..) |name_index, global_index| {
11221106 const name = name_index.slice(wasm);
1123 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index)));
1124 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1125 try binary_bytes.appendSlice(gpa, name);
1107 try w.writeLeb128(global_index);
1108 try w.writeLeb128(name.len);
1109 try w.writeAll(name);
11261110 }
11271111 for (wasm.globals.keys(), f.global_imports.entries.len..) |resolution, global_index| {
11281112 const name = resolution.name(wasm).?;
1129 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(global_index)));
1130 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1131 try binary_bytes.appendSlice(gpa, name);
1113 try w.writeLeb128(global_index);
1114 try w.writeLeb128(name.len);
1115 try w.writeAll(name);
11321116 }
11331117 }
11341118
11351119 {
1136 const sub_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1137 defer replaceHeader(binary_bytes, sub_offset, @intFromEnum(std.wasm.NameSubsection.data_segment));
1120 const sub_header_offset = try reserveSectionHeader(w);
1121 defer replaceSectionHeader(aw, sub_header_offset, @intFromEnum(std.wasm.NameSubsection.data_segment));
11381122
1139 const total_data_segments: u32 = @intCast(data_segment_groups.len);
1140 try leb.writeUleb128(binary_bytes.writer(gpa), total_data_segments);
1141
1142 for (data_segment_groups, 0..) |group, i| {
1123 try w.writeLeb128(data_segment_groups.len);
1124 for (data_segment_groups, 0..) |group, group_index| {
11431125 const name, _ = splitSegmentName(group.first_segment.name(wasm));
1144 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(i)));
1145 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1146 try binary_bytes.appendSlice(gpa, name);
1126 try w.writeLeb128(group_index);
1127 try w.writeLeb128(name.len);
1128 try w.writeAll(name);
11471129 }
11481130 }
11491131}
11501132
1151fn emitFeaturesSection(
1152 gpa: Allocator,
1153 binary_bytes: *std.ArrayListUnmanaged(u8),
1154 target: *const std.Target,
1155) Allocator.Error!void {
1133fn emitFeaturesSection(aw: *Writer.Allocating, target: *const std.Target) !void {
11561134 const feature_count = target.cpu.features.count();
11571135 if (feature_count == 0) return;
11581136
1159 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1160 defer writeCustomSectionHeader(binary_bytes, header_offset);
1161
1162 const writer = binary_bytes.writer(gpa);
1163 const target_features = "target_features";
1164 try leb.writeUleb128(writer, @as(u32, @intCast(target_features.len)));
1165 try writer.writeAll(target_features);
1137 const w = &aw.writer;
1138 const header_offset = try reserveSectionHeader(w);
1139 defer replaceSectionHeader(aw, header_offset, @intFromEnum(std.wasm.Section.custom));
11661140
1167 try leb.writeUleb128(writer, @as(u32, @intCast(feature_count)));
1141 const section_name = "target_features";
1142 try w.writeLeb128(section_name.len);
1143 try w.writeAll(section_name);
11681144
1145 try w.writeLeb128(feature_count);
11691146 var safety_count = feature_count;
11701147 for (target.cpu.arch.allFeaturesList(), 0..) |*feature, i| {
11711148 if (!target.cpu.has(.wasm, @as(std.Target.wasm.Feature, @enumFromInt(i)))) continue;
11721149 safety_count -= 1;
11731150
1174 try leb.writeUleb128(writer, @as(u32, '+'));
1151 try w.writeUleb128('+');
11751152 // Depends on llvm_name for the hyphenated version that matches wasm tooling conventions.
11761153 const name = feature.llvm_name.?;
1177 try leb.writeUleb128(writer, @as(u32, @intCast(name.len)));
1178 try writer.writeAll(name);
1154 try w.writeLeb128(name.len);
1155 try w.writeAll(name);
11791156 }
11801157 assert(safety_count == 0);
11811158}
11821159
1183fn emitBuildIdSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8), build_id: []const u8) !void {
1184 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1185 defer writeCustomSectionHeader(binary_bytes, header_offset);
1160fn emitBuildIdSection(aw: *Writer.Allocating, build_id: []const u8) !void {
1161 const w = &aw.writer;
1162 const header_offset = try reserveSectionHeader(w);
1163 defer replaceSectionHeader(aw, header_offset, @intFromEnum(std.wasm.Section.custom));
11861164
1187 const writer = binary_bytes.writer(gpa);
1188 const hdr_build_id = "build_id";
1189 try leb.writeUleb128(writer, @as(u32, @intCast(hdr_build_id.len)));
1190 try writer.writeAll(hdr_build_id);
1165 const section_name = "build_id";
1166 try w.writeLeb128(section_name.len);
1167 try w.writeAll(section_name);
11911168
1192 try leb.writeUleb128(writer, @as(u32, 1));
1193 try leb.writeUleb128(writer, @as(u32, @intCast(build_id.len)));
1194 try writer.writeAll(build_id);
1169 try w.writeUleb128(1);
1170 try w.writeLeb128(build_id.len);
1171 try w.writeAll(build_id);
11951172}
11961173
1197fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)) !void {
1198 const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1199 defer writeCustomSectionHeader(binary_bytes, header_offset);
1200
1201 const writer = binary_bytes.writer(gpa);
1202 const producers = "producers";
1203 try leb.writeUleb128(writer, @as(u32, @intCast(producers.len)));
1204 try writer.writeAll(producers);
1174fn emitProducerSection(aw: *Writer.Allocating) !void {
1175 const w = &aw.writer;
1176 const header_offset = try reserveSectionHeader(w);
1177 defer replaceSectionHeader(aw, header_offset, @intFromEnum(std.wasm.Section.custom));
12051178
1206 try leb.writeUleb128(writer, @as(u32, 2)); // 2 fields: Language + processed-by
1179 const section_name = "producers";
1180 try w.writeLeb128(section_name.len);
1181 try w.writeAll(section_name);
12071182
1208 // language field
1183 try w.writeUleb128(2); // 2 fields: language + processed-by
12091184 {
1210 const language = "language";
1211 try leb.writeUleb128(writer, @as(u32, @intCast(language.len)));
1212 try writer.writeAll(language);
1185 const field_name = "language";
1186 try w.writeLeb128(field_name.len);
1187 try w.writeAll(field_name);
12131188
12141189 // field_value_count (TODO: Parse object files for producer sections to detect their language)
1215 try leb.writeUleb128(writer, @as(u32, 1));
1190 try w.writeUleb128(1);
12161191
12171192 // versioned name
12181193 {
1219 try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig"
1220 try writer.writeAll("Zig");
1194 const field_value = "Zig";
1195 try w.writeLeb128(field_value.len);
1196 try w.writeAll(field_value);
12211197
1222 try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len)));
1223 try writer.writeAll(build_options.version);
1198 try w.writeLeb128(build_options.version.len);
1199 try w.writeAll(build_options.version);
12241200 }
12251201 }
1226
1227 // processed-by field
12281202 {
1229 const processed_by = "processed-by";
1230 try leb.writeUleb128(writer, @as(u32, @intCast(processed_by.len)));
1231 try writer.writeAll(processed_by);
1203 const field_name = "processed-by";
1204 try w.writeLeb128(field_name.len);
1205 try w.writeAll(field_name);
12321206
12331207 // field_value_count (TODO: Parse object files for producer sections to detect other used tools)
1234 try leb.writeUleb128(writer, @as(u32, 1));
1208 try w.writeUleb128(1);
12351209
12361210 // versioned name
12371211 {
1238 try leb.writeUleb128(writer, @as(u32, 3)); // len of "Zig"
1239 try writer.writeAll("Zig");
1212 const field_value = "Zig";
1213 try w.writeLeb128(field_value.len);
1214 try w.writeAll(field_value);
12401215
1241 try leb.writeUleb128(writer, @as(u32, @intCast(build_options.version.len)));
1242 try writer.writeAll(build_options.version);
1216 try w.writeLeb128(build_options.version.len);
1217 try w.writeAll(build_options.version);
12431218 }
12441219 }
12451220}
......@@ -1277,170 +1252,130 @@ fn wantSegmentMerge(
12771252}
12781253
12791254/// section id + fixed leb contents size + fixed leb vector length
1280const section_header_reserve_size = 1 + 5 + 5;
1281const section_header_size = 5 + 1;
1255const vec_section_header_size = section_header_size + size_header_size;
12821256
1283fn reserveVecSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 {
1284 try bytes.appendNTimes(gpa, 0, section_header_reserve_size);
1285 return @intCast(bytes.items.len - section_header_reserve_size);
1257fn reserveVecSectionHeader(w: *Writer) Writer.Error!u32 {
1258 const offset = w.end;
1259 _ = try w.writableSlice(vec_section_header_size);
1260 return @intCast(offset);
12861261}
12871262
12881263fn replaceVecSectionHeader(
1289 bytes: *std.ArrayListUnmanaged(u8),
1264 aw: *Writer.Allocating,
12901265 offset: u32,
12911266 section: std.wasm.Section,
12921267 n_items: u32,
12931268) void {
1294 const size: u32 = @intCast(bytes.items.len - offset - section_header_reserve_size + uleb128size(n_items));
1295 var buf: [section_header_reserve_size]u8 = undefined;
1296 var fbw = std.io.fixedBufferStream(&buf);
1297 const w = fbw.writer();
1298 w.writeByte(@intFromEnum(section)) catch unreachable;
1299 leb.writeUleb128(w, size) catch unreachable;
1300 leb.writeUleb128(w, n_items) catch unreachable;
1301 bytes.replaceRangeAssumeCapacity(offset, section_header_reserve_size, fbw.getWritten());
1269 const header = aw.getWritten()[offset..][0..vec_section_header_size];
1270 header[0] = @intFromEnum(section);
1271 std.leb.writeUnsignedFixed(5, header[1..6], @intCast(aw.writer.end - offset - section_header_size));
1272 std.leb.writeUnsignedFixed(5, header[6..], n_items);
13021273}
13031274
1304fn reserveCustomSectionHeader(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 {
1305 try bytes.appendNTimes(gpa, 0, section_header_size);
1306 return @intCast(bytes.items.len - section_header_size);
1307}
1275const section_header_size = 1 + size_header_size;
13081276
1309fn writeCustomSectionHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void {
1310 return replaceHeader(bytes, offset, 0); // 0 = 'custom' section
1277fn reserveSectionHeader(w: *Writer) Writer.Error!u32 {
1278 const offset = w.end;
1279 _ = try w.writableSlice(section_header_size);
1280 return @intCast(offset);
13111281}
13121282
1313fn replaceHeader(bytes: *std.ArrayListUnmanaged(u8), offset: u32, tag: u8) void {
1314 const size: u32 = @intCast(bytes.items.len - offset - section_header_size);
1315 var buf: [section_header_size]u8 = undefined;
1316 var fbw = std.io.fixedBufferStream(&buf);
1317 const w = fbw.writer();
1318 w.writeByte(tag) catch unreachable;
1319 leb.writeUleb128(w, size) catch unreachable;
1320 bytes.replaceRangeAssumeCapacity(offset, section_header_size, fbw.getWritten());
1283fn replaceSectionHeader(aw: *Writer.Allocating, offset: u32, section: u8) void {
1284 const header = aw.getWritten()[offset..][0..section_header_size];
1285 header[0] = section;
1286 std.leb.writeUnsignedFixed(5, header[1..6], @intCast(aw.writer.end - offset - section_header_size));
13211287}
13221288
1323const max_size_encoding = 5;
1289const size_header_size = 5;
13241290
1325fn reserveSize(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!u32 {
1326 try bytes.appendNTimes(gpa, 0, max_size_encoding);
1327 return @intCast(bytes.items.len - max_size_encoding);
1291fn reserveSizeHeader(w: *Writer) Writer.Error!u32 {
1292 const offset = w.end;
1293 _ = try w.writableSlice(size_header_size);
1294 return @intCast(offset);
13281295}
13291296
1330fn replaceSize(bytes: *std.ArrayListUnmanaged(u8), offset: u32) void {
1331 const size: u32 = @intCast(bytes.items.len - offset - max_size_encoding);
1332 var buf: [max_size_encoding]u8 = undefined;
1333 var fbw = std.io.fixedBufferStream(&buf);
1334 leb.writeUleb128(fbw.writer(), size) catch unreachable;
1335 bytes.replaceRangeAssumeCapacity(offset, max_size_encoding, fbw.getWritten());
1297fn replaceSizeHeader(aw: *Writer.Allocating, offset: u32) void {
1298 const header = aw.getWritten()[offset..][0..size_header_size];
1299 std.leb.writeUnsignedFixed(5, header[0..5], @intCast(aw.writer.end - offset - size_header_size));
13361300}
13371301
1338fn emitLimits(
1339 gpa: Allocator,
1340 binary_bytes: *std.ArrayListUnmanaged(u8),
1341 limits: std.wasm.Limits,
1342) Allocator.Error!void {
1343 try binary_bytes.append(gpa, @bitCast(limits.flags));
1344 try leb.writeUleb128(binary_bytes.writer(gpa), limits.min);
1345 if (limits.flags.has_max) try leb.writeUleb128(binary_bytes.writer(gpa), limits.max);
1302fn emitLimits(w: *Writer, limits: std.wasm.Limits) Writer.Error!void {
1303 try w.writeByte(@bitCast(limits.flags));
1304 try w.writeLeb128(limits.min);
1305 if (limits.flags.has_max) try w.writeLeb128(limits.max);
13461306}
13471307
13481308fn emitMemoryImport(
13491309 wasm: *Wasm,
1350 binary_bytes: *std.ArrayListUnmanaged(u8),
1310 w: *Writer,
13511311 name_index: String,
13521312 memory_import: *const Wasm.MemoryImport,
1353) Allocator.Error!void {
1354 const gpa = wasm.base.comp.gpa;
1313) Writer.Error!void {
13551314 const module_name = memory_import.module_name.slice(wasm);
1356 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(module_name.len)));
1357 try binary_bytes.appendSlice(gpa, module_name);
1315 try w.writeLeb128(module_name.len);
1316 try w.writeAll(module_name);
13581317
13591318 const name = name_index.slice(wasm);
1360 try leb.writeUleb128(binary_bytes.writer(gpa), @as(u32, @intCast(name.len)));
1361 try binary_bytes.appendSlice(gpa, name);
1319 try w.writeLeb128(name.len);
1320 try w.writeAll(name);
13621321
1363 try binary_bytes.append(gpa, @intFromEnum(std.wasm.ExternalKind.memory));
1364 try emitLimits(gpa, binary_bytes, memory_import.limits());
1322 try w.writeByte(@intFromEnum(std.wasm.ExternalKind.memory));
1323 try emitLimits(w, memory_import.limits());
13651324}
13661325
1367pub fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void {
1326pub fn emitInit(w: *Writer, init_expr: std.wasm.InitExpression) Writer.Error!void {
13681327 switch (init_expr) {
1369 .i32_const => |val| {
1370 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1371 try leb.writeIleb128(writer, val);
1372 },
1373 .i64_const => |val| {
1374 try writer.writeByte(@intFromEnum(std.wasm.Opcode.i64_const));
1375 try leb.writeIleb128(writer, val);
1376 },
1377 .f32_const => |val| {
1378 try writer.writeByte(@intFromEnum(std.wasm.Opcode.f32_const));
1379 try writer.writeInt(u32, @bitCast(val), .little);
1380 },
1381 .f64_const => |val| {
1382 try writer.writeByte(@intFromEnum(std.wasm.Opcode.f64_const));
1383 try writer.writeInt(u64, @bitCast(val), .little);
1384 },
1385 .global_get => |val| {
1386 try writer.writeByte(@intFromEnum(std.wasm.Opcode.global_get));
1387 try leb.writeUleb128(writer, val);
1328 inline else => |val, tag| {
1329 try w.writeByte(@intFromEnum(@field(std.wasm.Opcode, @tagName(tag))));
1330 switch (@typeInfo(@TypeOf(val))) {
1331 .int => try w.writeLeb128(val),
1332 .float => |float| try w.writeInt(
1333 @Type(.{ .int = .{ .signedness = .unsigned, .bits = float.bits } }),
1334 @bitCast(val),
1335 .little,
1336 ),
1337 else => comptime unreachable,
1338 }
13881339 },
13891340 }
1390 try writer.writeByte(@intFromEnum(std.wasm.Opcode.end));
1341 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
13911342}
13921343
1393pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), expr: Wasm.Expr) Allocator.Error!void {
1394 const gpa = wasm.base.comp.gpa;
1344pub fn emitExpr(wasm: *const Wasm, w: *Writer, expr: Wasm.Expr) Writer.Error!void {
13951345 const slice = expr.slice(wasm);
1396 try binary_bytes.appendSlice(gpa, slice[0 .. slice.len + 1]); // +1 to include end opcode
1346 try w.writeAll(slice[0 .. slice.len + 1]); // +1 to include end opcode
13971347}
13981348
1399fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void {
1400 const gpa = wasm.base.comp.gpa;
1401 const writer = binary_bytes.writer(gpa);
1402 try leb.writeUleb128(writer, @intFromEnum(Wasm.SubsectionType.segment_info));
1403 const segment_offset = binary_bytes.items.len;
1349fn emitSegmentInfo(wasm: *Wasm, aw: *Writer) Writer.Error!void {
1350 const w = &aw.writer;
1351 const header_offset = try reserveSectionHeader(w);
1352 defer replaceSectionHeader(aw, header_offset, @intFromEnum(Wasm.SubsectionType.segment_info));
14041353
1405 try leb.writeUleb128(writer, @as(u32, @intCast(wasm.segment_info.count())));
1354 try w.writeLeb128(wasm.segment_info.count());
14061355 for (wasm.segment_info.values()) |segment_info| {
14071356 log.debug("Emit segment: {s} align({d}) flags({b})", .{
14081357 segment_info.name,
14091358 segment_info.alignment,
14101359 segment_info.flags,
14111360 });
1412 try leb.writeUleb128(writer, @as(u32, @intCast(segment_info.name.len)));
1413 try writer.writeAll(segment_info.name);
1414 try leb.writeUleb128(writer, segment_info.alignment.toLog2Units());
1415 try leb.writeUleb128(writer, segment_info.flags);
1361 try w.writeLeb128(segment_info.name.len);
1362 try w.writeAll(segment_info.name);
1363 try w.writeLeb128(segment_info.alignment.toLog2Units());
1364 try w.writeLeb128(segment_info.flags);
14161365 }
1417
1418 var buf: [5]u8 = undefined;
1419 leb.writeUnsignedFixed(5, &buf, @as(u32, @intCast(binary_bytes.items.len - segment_offset)));
1420 try binary_bytes.insertSlice(segment_offset, &buf);
1421}
1422
1423fn uleb128size(x: u32) u32 {
1424 var value = x;
1425 var size: u32 = 0;
1426 while (value != 0) : (size += 1) value >>= 7;
1427 return size;
14281366}
14291367
14301368fn emitTagNameTable(
1431 gpa: Allocator,
1432 code: *std.ArrayListUnmanaged(u8),
1369 w: *Writer,
14331370 tag_name_offs: []const u32,
14341371 tag_name_bytes: []const u8,
14351372 base: u32,
14361373 comptime Int: type,
1437) error{OutOfMemory}!void {
1438 const ptr_size_bytes = @divExact(@bitSizeOf(Int), 8);
1439 try code.ensureUnusedCapacity(gpa, ptr_size_bytes * 2 * tag_name_offs.len);
1374) Writer.Error!void {
14401375 for (tag_name_offs) |off| {
14411376 const name_len: u32 = @intCast(mem.indexOfScalar(u8, tag_name_bytes[off..], 0).?);
1442 mem.writeInt(Int, code.addManyAsArrayAssumeCapacity(ptr_size_bytes), base + off, .little);
1443 mem.writeInt(Int, code.addManyAsArrayAssumeCapacity(ptr_size_bytes), name_len, .little);
1377 try w.writeInt(Int, base + off, .little);
1378 try w.writeInt(Int, name_len, .little);
14441379 }
14451380}
14461381
......@@ -1525,11 +1460,11 @@ fn reloc_u64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
15251460}
15261461
15271462fn reloc_sleb_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1528 leb.writeSignedFixed(5, code[0..5], i.toAbi());
1463 std.leb.writeSignedFixed(5, code[0..5], i.toAbi());
15291464}
15301465
15311466fn reloc_sleb64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1532 leb.writeSignedFixed(11, code[0..11], i.toAbi());
1467 std.leb.writeSignedFixed(11, code[0..11], i.toAbi());
15331468}
15341469
15351470fn reloc_u32_function(code: []u8, function: Wasm.OutputFunctionIndex) void {
......@@ -1537,7 +1472,7 @@ fn reloc_u32_function(code: []u8, function: Wasm.OutputFunctionIndex) void {
15371472}
15381473
15391474fn reloc_leb_function(code: []u8, function: Wasm.OutputFunctionIndex) void {
1540 leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(function));
1475 std.leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(function));
15411476}
15421477
15431478fn reloc_u32_global(code: []u8, global: Wasm.GlobalIndex) void {
......@@ -1545,7 +1480,7 @@ fn reloc_u32_global(code: []u8, global: Wasm.GlobalIndex) void {
15451480}
15461481
15471482fn reloc_leb_global(code: []u8, global: Wasm.GlobalIndex) void {
1548 leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(global));
1483 std.leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(global));
15491484}
15501485
15511486const RelocAddr = struct {
......@@ -1581,35 +1516,31 @@ fn reloc_u64_addr(code: []u8, ra: RelocAddr) void {
15811516}
15821517
15831518fn reloc_leb_addr(code: []u8, ra: RelocAddr) void {
1584 leb.writeUnsignedFixed(5, code[0..5], ra.addr);
1519 std.leb.writeUnsignedFixed(5, code[0..5], ra.addr);
15851520}
15861521
15871522fn reloc_leb64_addr(code: []u8, ra: RelocAddr) void {
1588 leb.writeUnsignedFixed(11, code[0..11], ra.addr);
1523 std.leb.writeUnsignedFixed(11, code[0..11], ra.addr);
15891524}
15901525
15911526fn reloc_sleb_addr(code: []u8, ra: RelocAddr) void {
1592 leb.writeSignedFixed(5, code[0..5], ra.addr);
1527 std.leb.writeSignedFixed(5, code[0..5], ra.addr);
15931528}
15941529
15951530fn reloc_sleb64_addr(code: []u8, ra: RelocAddr) void {
1596 leb.writeSignedFixed(11, code[0..11], ra.addr);
1531 std.leb.writeSignedFixed(11, code[0..11], ra.addr);
15971532}
15981533
15991534fn reloc_leb_table(code: []u8, table: Wasm.TableIndex) void {
1600 leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(table));
1535 std.leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(table));
16011536}
16021537
16031538fn reloc_leb_type(code: []u8, index: FuncTypeIndex) void {
1604 leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(index));
1539 std.leb.writeUnsignedFixed(5, code[0..5], @intFromEnum(index));
16051540}
16061541
1607fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void {
1608 const gpa = wasm.base.comp.gpa;
1609
1610 try binary_bytes.ensureUnusedCapacity(gpa, 5 + 1);
1611 appendReservedUleb32(binary_bytes, 0); // no locals
1612
1542fn emitCallCtorsFunction(wasm: *const Wasm, w: *Writer) Writer.Error!void {
1543 try w.writeUleb128(0); // no locals
16131544 for (wasm.object_init_funcs.items) |init_func| {
16141545 const func = init_func.function_index.ptr(wasm);
16151546 if (!func.object_index.ptr(wasm).is_included) continue;
......@@ -1617,25 +1548,18 @@ fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanage
16171548 const n_returns = ty.returns.slice(wasm).len;
16181549
16191550 // Call function by its function index
1620 try binary_bytes.ensureUnusedCapacity(gpa, 1 + 5 + n_returns + 1);
16211551 const call_index: Wasm.OutputFunctionIndex = .fromObjectFunction(wasm, init_func.function_index);
1622 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call));
1623 appendReservedUleb32(binary_bytes, @intFromEnum(call_index));
1552 try w.writeByte(@intFromEnum(std.wasm.Opcode.call));
1553 try w.writeLeb128(@intFromEnum(call_index));
16241554
16251555 // drop all returned values from the stack as __wasm_call_ctors has no return value
1626 binary_bytes.appendNTimesAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop), n_returns);
1556 try w.splatByteAll(@intFromEnum(std.wasm.Opcode.drop), n_returns);
16271557 }
1628
1629 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end function body
1558 try w.writeByte(@intFromEnum(std.wasm.Opcode.end)); // end function body
16301559}
16311560
1632fn emitInitMemoryFunction(
1633 wasm: *const Wasm,
1634 binary_bytes: *std.ArrayListUnmanaged(u8),
1635 virtual_addrs: *const VirtualAddrs,
1636) Allocator.Error!void {
1561fn emitInitMemoryFunction(wasm: *const Wasm, w: *Writer, virtual_addrs: *const VirtualAddrs) Writer.Error!void {
16371562 const comp = wasm.base.comp;
1638 const gpa = comp.gpa;
16391563 const shared_memory = comp.config.shared_memory;
16401564
16411565 // Passive segments are used to avoid memory being reinitialized on each
......@@ -1645,39 +1569,40 @@ fn emitInitMemoryFunction(
16451569 // function.
16461570 assert(wasm.any_passive_inits);
16471571
1648 try binary_bytes.ensureUnusedCapacity(gpa, 5 + 1);
1649 appendReservedUleb32(binary_bytes, 0); // no locals
1572 try w.writeUleb128(0); // no locals
16501573
16511574 if (virtual_addrs.init_memory_flag) |flag_address| {
16521575 assert(shared_memory);
1653 try binary_bytes.ensureUnusedCapacity(gpa, 2 * 3 + 6 * 3 + 1 + 6 * 3 + 1 + 5 * 4 + 1 + 1);
16541576 // destination blocks
16551577 // based on values we jump to corresponding label
1656 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $drop
1657 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty));
1658
1659 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $wait
1660 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty));
1661
1662 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block)); // $init
1663 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty));
1578 try w.writeAll(&.{
1579 @intFromEnum(std.wasm.Opcode.block), // $drop
1580 @intFromEnum(std.wasm.BlockType.empty),
1581 @intFromEnum(std.wasm.Opcode.block), // $wait
1582 @intFromEnum(std.wasm.BlockType.empty),
1583 @intFromEnum(std.wasm.Opcode.block), // $init
1584 @intFromEnum(std.wasm.BlockType.empty),
1585 });
16641586
16651587 // atomically check
1666 appendReservedI32Const(binary_bytes, flag_address);
1667 appendReservedI32Const(binary_bytes, 0);
1668 appendReservedI32Const(binary_bytes, 1);
1669 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1670 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_rmw_cmpxchg));
1671 appendReservedUleb32(binary_bytes, 2); // alignment
1672 appendReservedUleb32(binary_bytes, 0); // offset
1588 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1589 try w.writeLeb128(@as(i32, @bitCast(flag_address)));
1590 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1591 try w.writeSleb128(0);
1592 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1593 try w.writeSleb128(1);
1594 try w.writeByte(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1595 try w.writeLeb128(@intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_rmw_cmpxchg));
1596 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1597 try w.writeUleb128(0); // offset
16731598
16741599 // based on the value from the atomic check, jump to the label.
1675 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table));
1676 appendReservedUleb32(binary_bytes, 2); // length of the table (we have 3 blocks but because of the mandatory default the length is 2).
1677 appendReservedUleb32(binary_bytes, 0); // $init
1678 appendReservedUleb32(binary_bytes, 1); // $wait
1679 appendReservedUleb32(binary_bytes, 2); // $drop
1680 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1600 try w.writeByte(@intFromEnum(std.wasm.Opcode.br_table));
1601 try w.writeUleb128(3 - 1); // length of the table (we have 3 blocks but because of the mandatory default the length is 2).
1602 try w.writeUleb128(0); // $init
1603 try w.writeUleb128(1); // $wait
1604 try w.writeUleb128(2); // $drop
1605 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
16811606 }
16821607
16831608 const segment_groups = wasm.flush_buffer.data_segment_groups.items;
......@@ -1690,74 +1615,82 @@ fn emitInitMemoryFunction(
16901615 const start_addr: u32 = @intCast(segment.alignment(wasm).forward(prev_end));
16911616 const segment_size: u32 = group.end_addr - start_addr;
16921617
1693 try binary_bytes.ensureUnusedCapacity(gpa, 6 + 6 + 1 + 5 + 6 + 6 + 1 + 6 * 2 + 1 + 1);
1694
16951618 // For passive BSS segments we can simply issue a memory.fill(0). For
16961619 // non-BSS segments we do a memory.init. Both instructions take as
16971620 // their first argument the destination address.
1698 appendReservedI32Const(binary_bytes, start_addr);
1621 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1622 try w.writeLeb128(@as(i32, @bitCast(start_addr)));
16991623
17001624 if (shared_memory and segment.isTls(wasm)) {
17011625 // When we initialize the TLS segment we also set the `__tls_base`
17021626 // global. This allows the runtime to use this static copy of the
17031627 // TLS data for the first/main thread.
1704 appendReservedI32Const(binary_bytes, start_addr);
1705 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
1706 appendReservedUleb32(binary_bytes, virtual_addrs.tls_base.?);
1628 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1629 try w.writeLeb128(@as(i32, @bitCast(start_addr)));
1630 try w.writeByte(@intFromEnum(std.wasm.Opcode.global_set));
1631 try w.writeLeb128(virtual_addrs.tls_base.?);
17071632 }
17081633
1709 appendReservedI32Const(binary_bytes, 0);
1710 appendReservedI32Const(binary_bytes, segment_size);
1711 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
1634 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1635 try w.writeSleb128(0);
1636 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1637 try w.writeLeb128(@as(i32, @bitCast(segment_size)));
1638 try w.writeByte(@intFromEnum(std.wasm.Opcode.misc_prefix));
17121639 if (segment.isBss(wasm)) {
17131640 // fill bss segment with zeroes
1714 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.memory_fill));
1641 try w.writeLeb128(@intFromEnum(std.wasm.MiscOpcode.memory_fill));
17151642 } else {
17161643 // initialize the segment
1717 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.memory_init));
1718 appendReservedUleb32(binary_bytes, @intCast(segment_index));
1644 try w.writeLeb128(@intFromEnum(std.wasm.MiscOpcode.memory_init));
1645 try w.writeLeb128(segment_index);
17191646 }
1720 binary_bytes.appendAssumeCapacity(0); // memory index immediate
1647 try w.writeByte(0); // memory index immediate
17211648 }
17221649
17231650 if (virtual_addrs.init_memory_flag) |flag_address| {
17241651 assert(shared_memory);
1725 try binary_bytes.ensureUnusedCapacity(gpa, 6 + 6 + 1 + 3 * 5 + 6 + 1 + 5 + 1 + 3 * 5 + 1 + 1 + 5 + 1 + 6 * 2 + 1 + 5 + 1 + 3 * 5 + 1 + 1 + 1);
1652
17261653 // we set the init memory flag to value '2'
1727 appendReservedI32Const(binary_bytes, flag_address);
1728 appendReservedI32Const(binary_bytes, 2);
1729 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1730 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_store));
1731 appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment
1732 appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset
1654 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1655 try w.writeLeb128(@as(i32, @bitCast(flag_address)));
1656 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1657 try w.writeSleb128(2);
1658 try w.writeByte(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1659 try w.writeLeb128(@intFromEnum(std.wasm.AtomicsOpcode.i32_atomic_store));
1660 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1661 try w.writeUleb128(0); // offset
17331662
17341663 // notify any waiters for segment initialization completion
1735 appendReservedI32Const(binary_bytes, flag_address);
1736 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
1737 leb.writeIleb128(binary_bytes.fixedWriter(), @as(i32, -1)) catch unreachable; // number of waiters
1738 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1739 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_notify));
1740 appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment
1741 appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset
1742 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop));
1664 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1665 try w.writeLeb128(@as(i32, @bitCast(flag_address)));
1666 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1667 try w.writeSleb128(-1); // number of waiters
1668
1669 try w.writeByte(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1670 try w.writeLeb128(@intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_notify));
1671 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1672 try w.writeUleb128(0); // offset
1673 try w.writeByte(@intFromEnum(std.wasm.Opcode.drop));
17431674
17441675 // branch and drop segments
1745 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br));
1746 appendReservedUleb32(binary_bytes, @as(u32, 1));
1676 try w.writeByte(@intFromEnum(std.wasm.Opcode.br));
1677 try w.writeUleb128(1);
17471678
17481679 // wait for thread to initialize memory segments
1749 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end $wait
1750 appendReservedI32Const(binary_bytes, flag_address);
1751 appendReservedI32Const(binary_bytes, 1); // expected flag value
1752 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));
1753 leb.writeIleb128(binary_bytes.fixedWriter(), @as(i64, -1)) catch unreachable; // timeout
1754 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1755 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_wait32));
1756 appendReservedUleb32(binary_bytes, @as(u32, 2)); // alignment
1757 appendReservedUleb32(binary_bytes, @as(u32, 0)); // offset
1758 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.drop));
1759
1760 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end)); // end $drop
1680 try w.writeByte(@intFromEnum(std.wasm.Opcode.end)); // end $wait
1681 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1682 try w.writeLeb128(@as(i32, @bitCast(flag_address)));
1683 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1684 try w.writeSleb128(1); // expected flag value
1685 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_const));
1686 try w.writeSleb128(-1); // timeout
1687 try w.writeByte(@intFromEnum(std.wasm.Opcode.atomics_prefix));
1688 try w.writeByte(@intFromEnum(std.wasm.AtomicsOpcode.memory_atomic_wait32));
1689 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1690 try w.writeUleb128(0); // offset
1691 try w.writeByte(@intFromEnum(std.wasm.Opcode.drop));
1692
1693 try w.writeByte(@intFromEnum(std.wasm.Opcode.end)); // end $drop
17611694 }
17621695
17631696 for (segment_groups, 0..) |group, segment_index| {
......@@ -1768,26 +1701,20 @@ fn emitInitMemoryFunction(
17681701 // during the initialization of each thread (__wasm_init_tls).
17691702 if (shared_memory and segment.isTls(wasm)) continue;
17701703
1771 try binary_bytes.ensureUnusedCapacity(gpa, 1 + 5 + 5 + 1);
1772
1773 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
1774 appendReservedUleb32(binary_bytes, @intFromEnum(std.wasm.MiscOpcode.data_drop));
1775 appendReservedUleb32(binary_bytes, @intCast(segment_index));
1704 try w.writeByte(@intFromEnum(std.wasm.Opcode.misc_prefix));
1705 try w.writeLeb128(@intFromEnum(std.wasm.MiscOpcode.data_drop));
1706 try w.writeLeb128(segment_index);
17761707 }
17771708
17781709 // End of the function body
1779 binary_bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1710 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
17801711}
17811712
1782fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Allocator.Error!void {
1713fn emitInitTlsFunction(wasm: *const Wasm, w: *Writer) Writer.Error!void {
17831714 const comp = wasm.base.comp;
1784 const gpa = comp.gpa;
1785
17861715 assert(comp.config.shared_memory);
17871716
1788 try bytes.ensureUnusedCapacity(gpa, 5 * 10 + 8);
1789
1790 appendReservedUleb32(bytes, 0); // no locals
1717 try w.writeUleb128(0); // no locals
17911718
17921719 // If there's a TLS segment, initialize it during runtime using the bulk-memory feature
17931720 // TLS segment is always the first one due to how we sort the data segments.
......@@ -1796,36 +1723,35 @@ fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Al
17961723 const start_addr = wasm.flush_buffer.data_segments.values()[0];
17971724 const end_addr = wasm.flush_buffer.data_segment_groups.items[0].end_addr;
17981725 const group_size = end_addr - start_addr;
1799 const data_segment_index = 0;
1726 const data_segment_index: u32 = 0;
18001727
18011728 const param_local: u32 = 0;
18021729
1803 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1804 appendReservedUleb32(bytes, param_local);
1730 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1731 try w.writeLeb128(param_local);
18051732
18061733 const tls_base_global_index: Wasm.GlobalIndex = @enumFromInt(wasm.globals.getIndex(.__tls_base).?);
1807 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
1808 appendReservedUleb32(bytes, @intFromEnum(tls_base_global_index));
1734 try w.writeByte(@intFromEnum(std.wasm.Opcode.global_set));
1735 try w.writeLeb128(@intFromEnum(tls_base_global_index));
18091736
18101737 // load stack values for the bulk-memory operation
18111738 {
1812 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1813 appendReservedUleb32(bytes, param_local);
1739 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1740 try w.writeLeb128(param_local);
18141741
1815 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
1816 appendReservedUleb32(bytes, 0); //segment offset
1742 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1743 try w.writeSleb128(0); // segment offset
18171744
1818 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
1819 appendReservedUleb32(bytes, group_size); //segment offset
1745 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1746 try w.writeLeb128(@as(i32, @bitCast(group_size))); // segment offset
18201747 }
18211748
18221749 // perform the bulk-memory operation to initialize the data segment
1823 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
1824 appendReservedUleb32(bytes, @intFromEnum(std.wasm.MiscOpcode.memory_init));
1750 try w.writeByte(@intFromEnum(std.wasm.Opcode.misc_prefix));
1751 try w.writeLeb128(@intFromEnum(std.wasm.MiscOpcode.memory_init));
18251752 // segment immediate
1826 appendReservedUleb32(bytes, data_segment_index);
1827 // memory index immediate (always 0)
1828 appendReservedUleb32(bytes, 0);
1753 try w.writeLeb128(data_segment_index);
1754 try w.writeByte(0); // memory index immediate
18291755 }
18301756
18311757 // If we have to perform any TLS relocations, call the corresponding function
......@@ -1833,56 +1759,59 @@ fn emitInitTlsFunction(wasm: *const Wasm, bytes: *std.ArrayListUnmanaged(u8)) Al
18331759 // generated by the linker.
18341760 if (wasm.functions.getIndex(.__wasm_apply_global_tls_relocs)) |function_index| {
18351761 const output_function_index: Wasm.OutputFunctionIndex = .fromFunctionIndex(wasm, @enumFromInt(function_index));
1836 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.call));
1837 appendReservedUleb32(bytes, @intFromEnum(output_function_index));
1762 try w.writeByte(@intFromEnum(std.wasm.Opcode.call));
1763 try w.writeLeb128(@intFromEnum(output_function_index));
18381764 }
18391765
1840 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1766 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
18411767}
18421768
1843fn emitStartSection(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) !void {
1844 const header_offset = try reserveVecSectionHeader(gpa, bytes);
1845 replaceVecSectionHeader(bytes, header_offset, .start, @intFromEnum(i));
1769fn emitStartSection(aw: *Writer.Allocating, i: Wasm.OutputFunctionIndex) !void {
1770 const header_offset = try reserveVecSectionHeader(&aw.writer);
1771 defer replaceVecSectionHeader(aw, header_offset, .start, @intFromEnum(i));
18461772}
18471773
18481774fn emitTagNameFunction(
18491775 wasm: *Wasm,
1850 code: *std.ArrayListUnmanaged(u8),
1776 w: *Writer,
18511777 table_base_addr: u32,
18521778 table_index: u32,
18531779 enum_type_ip: InternPool.Index,
18541780) !void {
18551781 const comp = wasm.base.comp;
1856 const gpa = comp.gpa;
18571782 const diags = &comp.link_diags;
18581783 const zcu = comp.zcu.?;
18591784 const ip = &zcu.intern_pool;
18601785 const enum_type = ip.loadEnumType(enum_type_ip);
18611786 const tag_values = enum_type.values.get(ip);
18621787
1863 try code.ensureUnusedCapacity(gpa, 7 * 5 + 6 + 1 * 6);
1864 appendReservedUleb32(code, 0); // no locals
1788 try w.writeUleb128(0); // no locals
18651789
1866 const slice_abi_size = 8;
1867 const encoded_alignment = @ctz(@as(u32, 4));
1790 const slice_abi_size: u32 = 8;
18681791 if (tag_values.len == 0) {
18691792 // Then it's auto-numbered and therefore a direct table lookup.
1870 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1871 appendReservedUleb32(code, 0);
1793 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1794 try w.writeUleb128(0);
18721795
1873 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1874 appendReservedUleb32(code, 1);
1796 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1797 try w.writeUleb128(1);
18751798
1876 appendReservedI32Const(code, slice_abi_size);
1877 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_mul));
1799 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1800 if (std.math.isPowerOfTwo(slice_abi_size)) {
1801 try w.writeLeb128(@as(i32, @bitCast(@as(u32, std.math.log2_int(u32, slice_abi_size)))));
1802 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_shl));
1803 } else {
1804 try w.writeLeb128(@as(i32, @bitCast(slice_abi_size)));
1805 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_mul));
1806 }
18781807
1879 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_load));
1880 appendReservedUleb32(code, encoded_alignment);
1881 appendReservedUleb32(code, table_base_addr + table_index * 8);
1808 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_load));
1809 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1810 try w.writeLeb128(table_base_addr + slice_abi_size * table_index);
18821811
1883 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_store));
1884 appendReservedUleb32(code, encoded_alignment);
1885 appendReservedUleb32(code, 0);
1812 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_store));
1813 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1814 try w.writeUleb128(0);
18861815 } else {
18871816 const int_info = Zcu.Type.intInfo(.fromInterned(enum_type.tag_ty), zcu);
18881817 const outer_block_type: std.wasm.BlockType = switch (int_info.bits) {
......@@ -1891,94 +1820,80 @@ fn emitTagNameFunction(
18911820 else => return diags.fail("wasm linker does not yet implement @tagName for sparse enums with more than 64 bit integer tag types", .{}),
18921821 };
18931822
1894 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1895 appendReservedUleb32(code, 0);
1823 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1824 try w.writeUleb128(0);
18961825
18971826 // Outer block that computes table offset.
1898 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block));
1899 code.appendAssumeCapacity(@intFromEnum(outer_block_type));
1827 try w.writeByte(@intFromEnum(std.wasm.Opcode.block));
1828 try w.writeByte(@intFromEnum(outer_block_type));
19001829
19011830 for (tag_values, 0..) |tag_value, tag_index| {
19021831 // block for this if case
1903 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.block));
1904 code.appendAssumeCapacity(@intFromEnum(std.wasm.BlockType.empty));
1832 try w.writeByte(@intFromEnum(std.wasm.Opcode.block));
1833 try w.writeByte(@intFromEnum(std.wasm.BlockType.empty));
19051834
19061835 // Tag value whose name should be returned.
1907 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_get));
1908 appendReservedUleb32(code, 1);
1836 try w.writeByte(@intFromEnum(std.wasm.Opcode.local_get));
1837 try w.writeUleb128(1);
19091838
19101839 const val: Zcu.Value = .fromInterned(tag_value);
19111840 switch (outer_block_type) {
19121841 .i32 => {
1913 const x: u32 = switch (int_info.signedness) {
1914 .signed => @bitCast(@as(i32, @intCast(val.toSignedInt(zcu)))),
1915 .unsigned => @intCast(val.toUnsignedInt(zcu)),
1916 };
1917 appendReservedI32Const(code, x);
1918 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_ne));
1842 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1843 try w.writeLeb128(@as(i32, switch (int_info.signedness) {
1844 .signed => @intCast(val.toSignedInt(zcu)),
1845 .unsigned => @bitCast(@as(u32, @intCast(val.toUnsignedInt(zcu)))),
1846 }));
1847 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_ne));
19191848 },
19201849 .i64 => {
1921 const x: u64 = switch (int_info.signedness) {
1922 .signed => @bitCast(val.toSignedInt(zcu)),
1923 .unsigned => val.toUnsignedInt(zcu),
1924 };
1925 appendReservedI64Const(code, x);
1926 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_ne));
1850 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_const));
1851 try w.writeLeb128(@as(i64, switch (int_info.signedness) {
1852 .signed => val.toSignedInt(zcu),
1853 .unsigned => @bitCast(val.toUnsignedInt(zcu)),
1854 }));
1855 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_ne));
19271856 },
19281857 else => unreachable,
19291858 }
19301859
19311860 // if they're not equal, break out of current branch
1932 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_if));
1933 appendReservedUleb32(code, 0);
1861 try w.writeByte(@intFromEnum(std.wasm.Opcode.br_if));
1862 try w.writeUleb128(0);
19341863
19351864 // Put the table offset of the result on the stack.
1936 appendReservedI32Const(code, @intCast(tag_index * slice_abi_size));
1865 try w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const));
1866 try w.writeLeb128(@as(i32, @bitCast(@as(u32, @intCast(slice_abi_size * tag_index)))));
19371867
19381868 // break outside blocks
1939 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br));
1940 appendReservedUleb32(code, 1);
1869 try w.writeByte(@intFromEnum(std.wasm.Opcode.br));
1870 try w.writeUleb128(1);
19411871
19421872 // end the block for this case
1943 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1873 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
19441874 }
1945 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.@"unreachable"));
1946 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1875 try w.writeByte(@intFromEnum(std.wasm.Opcode.@"unreachable"));
1876 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
19471877
1948 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_load));
1949 appendReservedUleb32(code, encoded_alignment);
1950 appendReservedUleb32(code, table_base_addr + table_index * 8);
1878 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_load));
1879 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1880 try w.writeLeb128(table_base_addr + slice_abi_size * table_index);
19511881
1952 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_store));
1953 appendReservedUleb32(code, encoded_alignment);
1954 appendReservedUleb32(code, 0);
1882 try w.writeByte(@intFromEnum(std.wasm.Opcode.i64_store));
1883 try w.writeLeb128(comptime Alignment.@"4".toLog2Units());
1884 try w.writeUleb128(0);
19551885 }
19561886
19571887 // End of the function body
1958 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1959}
1960
1961/// Writes an unsigned 32-bit integer as a LEB128-encoded 'i32.const' value.
1962fn appendReservedI32Const(bytes: *std.ArrayListUnmanaged(u8), val: u32) void {
1963 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
1964 leb.writeIleb128(bytes.fixedWriter(), @as(i32, @bitCast(val))) catch unreachable;
1965}
1966
1967/// Writes an unsigned 64-bit integer as a LEB128-encoded 'i64.const' value.
1968fn appendReservedI64Const(bytes: *std.ArrayListUnmanaged(u8), val: u64) void {
1969 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));
1970 leb.writeIleb128(bytes.fixedWriter(), @as(i64, @bitCast(val))) catch unreachable;
1971}
1972
1973fn appendReservedUleb32(bytes: *std.ArrayListUnmanaged(u8), val: u32) void {
1974 leb.writeUleb128(bytes.fixedWriter(), val) catch unreachable;
1888 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
19751889}
19761890
1977fn appendGlobal(gpa: Allocator, bytes: *std.ArrayListUnmanaged(u8), mutable: u8, val: u32) Allocator.Error!void {
1978 try bytes.ensureUnusedCapacity(gpa, 9);
1979 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Valtype.i32));
1980 bytes.appendAssumeCapacity(mutable);
1981 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
1982 appendReservedUleb32(bytes, val);
1983 bytes.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.end));
1891fn appendGlobal(w: *Writer, mutable: bool, val: u32) Writer.Error!void {
1892 try w.writeAll(&.{
1893 @intFromEnum(std.wasm.Valtype.i32),
1894 @intFromBool(mutable),
1895 @intFromEnum(std.wasm.Opcode.i32_const),
1896 });
1897 try w.writeLeb128(val);
1898 try w.writeByte(@intFromEnum(std.wasm.Opcode.end));
19841899}
src/link/Wasm/Object.zig+264-317
......@@ -8,6 +8,7 @@ const Allocator = std.mem.Allocator;
88const Path = std.Build.Cache.Path;
99const log = std.log.scoped(.object);
1010const assert = std.debug.assert;
11const Reader = std.Io.Reader;
1112
1213/// Wasm spec version used for this `Object`
1314version: u32,
......@@ -252,25 +253,21 @@ pub const ScratchSpace = struct {
252253
253254pub fn parse(
254255 wasm: *Wasm,
255 bytes: []const u8,
256 br: *Reader,
256257 path: Path,
257258 archive_member_name: ?[]const u8,
258259 host_name: Wasm.OptionalString,
259260 ss: *ScratchSpace,
260261 must_link: bool,
261262 gc_sections: bool,
262) anyerror!Object {
263) !Object {
263264 const comp = wasm.base.comp;
264265 const gpa = comp.gpa;
265266 const diags = &comp.link_diags;
266267
267 var pos: usize = 0;
268 if (!std.mem.eql(u8, try br.takeArray(std.wasm.magic.len), &std.wasm.magic)) return error.BadObjectMagic;
268269
269 if (!std.mem.eql(u8, bytes[0..std.wasm.magic.len], &std.wasm.magic)) return error.BadObjectMagic;
270 pos += std.wasm.magic.len;
271
272 const version = std.mem.readInt(u32, bytes[pos..][0..4], .little);
273 pos += 4;
270 const version = try br.takeInt(u32, .little);
274271
275272 const data_segment_start: u32 = @intCast(wasm.object_data_segments.items.len);
276273 const custom_segment_start: u32 = @intCast(wasm.object_custom_segments.entries.len);
......@@ -298,200 +295,187 @@ pub fn parse(
298295 var code_section_index: ?Wasm.ObjectSectionIndex = null;
299296 var global_section_index: ?Wasm.ObjectSectionIndex = null;
300297 var data_section_index: ?Wasm.ObjectSectionIndex = null;
301 while (pos < bytes.len) : (wasm.object_total_sections += 1) {
298 while (br.takeEnum(std.wasm.Section, .little)) |section_tag| : (wasm.object_total_sections += 1) {
302299 const section_index: Wasm.ObjectSectionIndex = @enumFromInt(wasm.object_total_sections);
303300
304 const section_tag: std.wasm.Section = @enumFromInt(bytes[pos]);
305 pos += 1;
306
307 const len, pos = readLeb(u32, bytes, pos);
308 const section_end = pos + len;
301 const len = try br.takeLeb128(u32);
302 const section_end = br.seek + len;
309303 switch (section_tag) {
310304 .custom => {
311 const section_name, pos = readBytes(bytes, pos);
305 const section_name = try br.take(try br.takeLeb128(u32));
312306 if (std.mem.eql(u8, section_name, "linking")) {
313307 saw_linking_section = true;
314 const section_version, pos = readLeb(u32, bytes, pos);
308 const section_version = try br.takeLeb128(u32);
315309 log.debug("link meta data version: {d}", .{section_version});
316310 if (section_version != 2) return error.UnsupportedVersion;
317 while (pos < section_end) {
318 const sub_type, pos = readLeb(u8, bytes, pos);
319 log.debug("found subsection: {s}", .{@tagName(@as(SubsectionType, @enumFromInt(sub_type)))});
320 const payload_len, pos = readLeb(u32, bytes, pos);
311 while (br.seek < section_end) {
312 const sub_type = try br.takeEnum(SubsectionType, .little);
313 log.debug("found subsection: {s}", .{@tagName(sub_type)});
314 const payload_len = try br.takeLeb128(u32);
321315 if (payload_len == 0) break;
322316
323 const count, pos = readLeb(u32, bytes, pos);
324
325 switch (@as(SubsectionType, @enumFromInt(sub_type))) {
326 .segment_info => {
327 for (try ss.segment_info.addManyAsSlice(gpa, count)) |*segment| {
328 const name, pos = readBytes(bytes, pos);
329 const alignment, pos = readLeb(u32, bytes, pos);
330 const flags_u32, pos = readLeb(u32, bytes, pos);
331 const flags: SegmentInfo.Flags = @bitCast(flags_u32);
332 const tls = flags.tls or
333 // Supports legacy object files that specified
334 // being TLS by the name instead of the TLS flag.
335 std.mem.startsWith(u8, name, ".tdata") or
336 std.mem.startsWith(u8, name, ".tbss");
337 has_tls = has_tls or tls;
338 segment.* = .{
339 .name = try wasm.internString(name),
340 .flags = .{
341 .strings = flags.strings,
342 .tls = tls,
343 .alignment = @enumFromInt(alignment),
344 .retain = flags.retain,
345 },
346 };
347 }
317 const count = try br.takeLeb128(u32);
318 switch (sub_type) {
319 .segment_info => for (try ss.segment_info.addManyAsSlice(gpa, count)) |*segment| {
320 const name = try br.take(try br.takeLeb128(u32));
321 const alignment: Alignment = .fromLog2Units(try br.takeLeb128(u32));
322 const flags: SegmentInfo.Flags = @bitCast(try br.takeLeb128(u32));
323 const tls = flags.tls or
324 // Supports legacy object files that specified
325 // being TLS by the name instead of the TLS flag.
326 std.mem.startsWith(u8, name, ".tdata") or
327 std.mem.startsWith(u8, name, ".tbss");
328 has_tls = has_tls or tls;
329 segment.* = .{
330 .name = try wasm.internString(name),
331 .flags = .{
332 .strings = flags.strings,
333 .tls = tls,
334 .alignment = alignment,
335 .retain = flags.retain,
336 },
337 };
348338 },
349 .init_funcs => {
350 for (try wasm.object_init_funcs.addManyAsSlice(gpa, count)) |*func| {
351 const priority, pos = readLeb(u32, bytes, pos);
352 const symbol_index, pos = readLeb(u32, bytes, pos);
353 if (symbol_index > ss.symbol_table.items.len)
354 return diags.failParse(path, "init_funcs before symbol table", .{});
355 const sym = &ss.symbol_table.items[symbol_index];
356 if (sym.pointee != .function) {
357 return diags.failParse(path, "init_func symbol '{s}' not a function", .{
358 sym.name.slice(wasm).?,
359 });
360 } else if (sym.flags.undefined) {
361 return diags.failParse(path, "init_func symbol '{s}' is an import", .{
362 sym.name.slice(wasm).?,
363 });
364 }
365 func.* = .{
366 .priority = priority,
367 .function_index = sym.pointee.function,
368 };
339 .init_funcs => for (try wasm.object_init_funcs.addManyAsSlice(gpa, count)) |*func| {
340 const priority = try br.takeLeb128(u32);
341 const symbol_index = try br.takeLeb128(u32);
342 if (symbol_index > ss.symbol_table.items.len)
343 return diags.failParse(path, "init_funcs before symbol table", .{});
344 const sym = &ss.symbol_table.items[symbol_index];
345 if (sym.pointee != .function) {
346 return diags.failParse(path, "init_func symbol '{s}' not a function", .{
347 sym.name.slice(wasm).?,
348 });
349 } else if (sym.flags.undefined) {
350 return diags.failParse(path, "init_func symbol '{s}' is an import", .{
351 sym.name.slice(wasm).?,
352 });
369353 }
354 func.* = .{
355 .priority = priority,
356 .function_index = sym.pointee.function,
357 };
370358 },
371 .comdat_info => {
372 for (try wasm.object_comdats.addManyAsSlice(gpa, count)) |*comdat| {
373 const name, pos = readBytes(bytes, pos);
374 const flags, pos = readLeb(u32, bytes, pos);
375 if (flags != 0) return error.UnexpectedComdatFlags;
376 const symbol_count, pos = readLeb(u32, bytes, pos);
377 const start_off: u32 = @intCast(wasm.object_comdat_symbols.len);
378 try wasm.object_comdat_symbols.ensureUnusedCapacity(gpa, symbol_count);
379 for (0..symbol_count) |_| {
380 const kind, pos = readEnum(Wasm.Comdat.Symbol.Type, bytes, pos);
381 const index, pos = readLeb(u32, bytes, pos);
382 if (true) @panic("TODO rebase index depending on kind");
383 wasm.object_comdat_symbols.appendAssumeCapacity(.{
384 .kind = kind,
385 .index = index,
386 });
387 }
388 comdat.* = .{
389 .name = try wasm.internString(name),
390 .flags = flags,
391 .symbols = .{
392 .off = start_off,
393 .len = @intCast(wasm.object_comdat_symbols.len - start_off),
394 },
395 };
359 .comdat_info => for (try wasm.object_comdats.addManyAsSlice(gpa, count)) |*comdat| {
360 const name = try br.take(try br.takeLeb128(u32));
361 const flags = try br.takeLeb128(u32);
362 if (flags != 0) return error.UnexpectedComdatFlags;
363 const symbol_count = try br.takeLeb128(u32);
364 const start_off: u32 = @intCast(wasm.object_comdat_symbols.len);
365 try wasm.object_comdat_symbols.ensureUnusedCapacity(gpa, symbol_count);
366 for (0..symbol_count) |_| {
367 const kind = try br.takeEnum(Wasm.Comdat.Symbol.Type, .little);
368 const index = try br.takeLeb128(u32);
369 if (true) @panic("TODO rebase index depending on kind");
370 wasm.object_comdat_symbols.appendAssumeCapacity(.{
371 .kind = kind,
372 .index = index,
373 });
396374 }
375 comdat.* = .{
376 .name = try wasm.internString(name),
377 .flags = flags,
378 .symbols = .{
379 .off = start_off,
380 .len = @intCast(wasm.object_comdat_symbols.len - start_off),
381 },
382 };
397383 },
398 .symbol_table => {
399 for (try ss.symbol_table.addManyAsSlice(gpa, count)) |*symbol| {
400 const tag, pos = readEnum(Symbol.Tag, bytes, pos);
401 const flags, pos = readLeb(u32, bytes, pos);
402 symbol.* = .{
403 .flags = @bitCast(flags),
404 .name = .none,
405 .pointee = undefined,
406 };
407 symbol.flags.initZigSpecific(must_link, gc_sections);
408
409 switch (tag) {
410 .data => {
411 const name, pos = readBytes(bytes, pos);
412 const interned_name = try wasm.internString(name);
413 symbol.name = interned_name.toOptional();
414 if (symbol.flags.undefined) {
415 symbol.pointee = .data_import;
384 .symbol_table => for (try ss.symbol_table.addManyAsSlice(gpa, count)) |*symbol| {
385 const tag = try br.takeEnum(Symbol.Tag, .little);
386 const flags: Wasm.SymbolFlags = @bitCast(try br.takeLeb128(u32));
387 symbol.* = .{
388 .flags = flags,
389 .name = .none,
390 .pointee = undefined,
391 };
392 symbol.flags.initZigSpecific(must_link, gc_sections);
393
394 switch (tag) {
395 .data => {
396 const name = try br.take(try br.takeLeb128(u32));
397 const interned_name = try wasm.internString(name);
398 symbol.name = interned_name.toOptional();
399 if (symbol.flags.undefined) {
400 symbol.pointee = .data_import;
401 } else {
402 const segment_index = try br.takeLeb128(u32);
403 const segment_offset = try br.takeLeb128(u32);
404 const size = try br.takeLeb128(u32);
405 try wasm.object_datas.append(gpa, .{
406 .segment = @enumFromInt(data_segment_start + segment_index),
407 .offset = segment_offset,
408 .size = size,
409 .name = interned_name,
410 .flags = symbol.flags,
411 });
412 symbol.pointee = .{
413 .data = @enumFromInt(wasm.object_datas.items.len - 1),
414 };
415 }
416 },
417 .section => {
418 const local_section = try br.takeLeb128(u32);
419 const section: Wasm.ObjectSectionIndex = @enumFromInt(local_section_index_base + local_section);
420 symbol.pointee = .{ .section = section };
421 },
422
423 .function => {
424 const local_index = try br.takeLeb128(u32);
425 if (symbol.flags.undefined) {
426 const function_import: ScratchSpace.FuncImportIndex = @enumFromInt(local_index);
427 symbol.pointee = .{ .function_import = function_import };
428 if (symbol.flags.explicit_name) {
429 const name = try br.take(try br.takeLeb128(u32));
430 symbol.name = (try wasm.internString(name)).toOptional();
416431 } else {
417 const segment_index, pos = readLeb(u32, bytes, pos);
418 const segment_offset, pos = readLeb(u32, bytes, pos);
419 const size, pos = readLeb(u32, bytes, pos);
420 try wasm.object_datas.append(gpa, .{
421 .segment = @enumFromInt(data_segment_start + segment_index),
422 .offset = segment_offset,
423 .size = size,
424 .name = interned_name,
425 .flags = symbol.flags,
426 });
427 symbol.pointee = .{
428 .data = @enumFromInt(wasm.object_datas.items.len - 1),
429 };
432 symbol.name = function_import.ptr(ss).name.toOptional();
430433 }
431 },
432 .section => {
433 const local_section, pos = readLeb(u32, bytes, pos);
434 const section: Wasm.ObjectSectionIndex = @enumFromInt(local_section_index_base + local_section);
435 symbol.pointee = .{ .section = section };
436 },
437
438 .function => {
439 const local_index, pos = readLeb(u32, bytes, pos);
440 if (symbol.flags.undefined) {
441 const function_import: ScratchSpace.FuncImportIndex = @enumFromInt(local_index);
442 symbol.pointee = .{ .function_import = function_import };
443 if (symbol.flags.explicit_name) {
444 const name, pos = readBytes(bytes, pos);
445 symbol.name = (try wasm.internString(name)).toOptional();
446 } else {
447 symbol.name = function_import.ptr(ss).name.toOptional();
448 }
449 } else {
450 symbol.pointee = .{ .function = @enumFromInt(functions_start + (local_index - ss.func_imports.items.len)) };
451 const name, pos = readBytes(bytes, pos);
434 } else {
435 symbol.pointee = .{ .function = @enumFromInt(functions_start + (local_index - ss.func_imports.items.len)) };
436 const name = try br.take(try br.takeLeb128(u32));
437 symbol.name = (try wasm.internString(name)).toOptional();
438 }
439 },
440 .global => {
441 const local_index = try br.takeLeb128(u32);
442 if (symbol.flags.undefined) {
443 const global_import: ScratchSpace.GlobalImportIndex = @enumFromInt(local_index);
444 symbol.pointee = .{ .global_import = global_import };
445 if (symbol.flags.explicit_name) {
446 const name = try br.take(try br.takeLeb128(u32));
452447 symbol.name = (try wasm.internString(name)).toOptional();
453 }
454 },
455 .global => {
456 const local_index, pos = readLeb(u32, bytes, pos);
457 if (symbol.flags.undefined) {
458 const global_import: ScratchSpace.GlobalImportIndex = @enumFromInt(local_index);
459 symbol.pointee = .{ .global_import = global_import };
460 if (symbol.flags.explicit_name) {
461 const name, pos = readBytes(bytes, pos);
462 symbol.name = (try wasm.internString(name)).toOptional();
463 } else {
464 symbol.name = global_import.ptr(ss).name.toOptional();
465 }
466448 } else {
467 symbol.pointee = .{ .global = @enumFromInt(globals_start + (local_index - ss.global_imports.items.len)) };
468 const name, pos = readBytes(bytes, pos);
469 symbol.name = (try wasm.internString(name)).toOptional();
449 symbol.name = global_import.ptr(ss).name.toOptional();
470450 }
471 },
472 .table => {
473 const local_index, pos = readLeb(u32, bytes, pos);
474 if (symbol.flags.undefined) {
475 table_import_symbol_count += 1;
476 const table_import: ScratchSpace.TableImportIndex = @enumFromInt(local_index);
477 symbol.pointee = .{ .table_import = table_import };
478 if (symbol.flags.explicit_name) {
479 const name, pos = readBytes(bytes, pos);
480 symbol.name = (try wasm.internString(name)).toOptional();
481 } else {
482 symbol.name = table_import.ptr(ss).name.toOptional();
483 }
484 } else {
485 symbol.pointee = .{ .table = @enumFromInt(tables_start + (local_index - ss.table_imports.items.len)) };
486 const name, pos = readBytes(bytes, pos);
451 } else {
452 symbol.pointee = .{ .global = @enumFromInt(globals_start + (local_index - ss.global_imports.items.len)) };
453 const name = try br.take(try br.takeLeb128(u32));
454 symbol.name = (try wasm.internString(name)).toOptional();
455 }
456 },
457 .table => {
458 const local_index = try br.takeLeb128(u32);
459 if (symbol.flags.undefined) {
460 table_import_symbol_count += 1;
461 const table_import: ScratchSpace.TableImportIndex = @enumFromInt(local_index);
462 symbol.pointee = .{ .table_import = table_import };
463 if (symbol.flags.explicit_name) {
464 const name = try br.take(try br.takeLeb128(u32));
487465 symbol.name = (try wasm.internString(name)).toOptional();
466 } else {
467 symbol.name = table_import.ptr(ss).name.toOptional();
488468 }
489 },
490 else => {
491 log.debug("unrecognized symbol type tag: {x}", .{@intFromEnum(tag)});
492 return error.UnrecognizedSymbolType;
493 },
494 }
469 } else {
470 symbol.pointee = .{ .table = @enumFromInt(tables_start + (local_index - ss.table_imports.items.len)) };
471 const name = try br.take(try br.takeLeb128(u32));
472 symbol.name = (try wasm.internString(name)).toOptional();
473 }
474 },
475 else => {
476 log.debug("unrecognized symbol type tag: {x}", .{@intFromEnum(tag)});
477 return error.UnrecognizedSymbolType;
478 },
495479 }
496480 },
497481 }
......@@ -504,8 +488,8 @@ pub fn parse(
504488 // which section they apply to, and must be sequenced in
505489 // the module after that section."
506490 // "Relocation sections can only target code, data and custom sections."
507 const local_section, pos = readLeb(u32, bytes, pos);
508 const count, pos = readLeb(u32, bytes, pos);
491 const local_section = try br.takeLeb128(u32);
492 const count = try br.takeLeb128(u32);
509493 const section: Wasm.ObjectSectionIndex = @enumFromInt(local_section_index_base + local_section);
510494
511495 log.debug("found {d} relocations for section={d}", .{ count, section });
......@@ -513,10 +497,9 @@ pub fn parse(
513497 var prev_offset: u32 = 0;
514498 try wasm.object_relocations.ensureUnusedCapacity(gpa, count);
515499 for (0..count) |_| {
516 const tag: RelocationType = @enumFromInt(bytes[pos]);
517 pos += 1;
518 const offset, pos = readLeb(u32, bytes, pos);
519 const index, pos = readLeb(u32, bytes, pos);
500 const tag = try br.takeEnum(RelocationType, .little);
501 const offset = try br.takeLeb128(u32);
502 const index = try br.takeLeb128(u32);
520503
521504 if (offset < prev_offset)
522505 return diags.failParse(path, "relocation entries not sorted by offset", .{});
......@@ -537,7 +520,7 @@ pub fn parse(
537520 .memory_addr_locrel_i32,
538521 .memory_addr_tls_sleb64,
539522 => {
540 const addend: i32, pos = readLeb(i32, bytes, pos);
523 const addend = try br.takeLeb128(i32);
541524 wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) {
542525 .data => |data| .{
543526 .tag = .fromType(tag),
......@@ -555,7 +538,7 @@ pub fn parse(
555538 });
556539 },
557540 .function_offset_i32, .function_offset_i64 => {
558 const addend: i32, pos = readLeb(i32, bytes, pos);
541 const addend = try br.takeLeb128(i32);
559542 wasm.object_relocations.appendAssumeCapacity(switch (sym.pointee) {
560543 .function => .{
561544 .tag = .fromType(tag),
......@@ -573,7 +556,7 @@ pub fn parse(
573556 });
574557 },
575558 .section_offset_i32 => {
576 const addend: i32, pos = readLeb(i32, bytes, pos);
559 const addend = try br.takeLeb128(i32);
577560 wasm.object_relocations.appendAssumeCapacity(.{
578561 .tag = .section_offset_i32,
579562 .offset = offset,
......@@ -658,10 +641,9 @@ pub fn parse(
658641 .len = count,
659642 });
660643 } else if (std.mem.eql(u8, section_name, "target_features")) {
661 opt_features, pos = try parseFeatures(wasm, bytes, pos, path);
644 opt_features = try parseFeatures(wasm, br, path);
662645 } else if (std.mem.startsWith(u8, section_name, ".debug")) {
663 const debug_content = bytes[pos..section_end];
664 pos = section_end;
646 const debug_content = try br.take(len);
665647
666648 const data_off: u32 = @intCast(wasm.string_bytes.items.len);
667649 try wasm.string_bytes.appendSlice(gpa, debug_content);
......@@ -669,23 +651,20 @@ pub fn parse(
669651 try wasm.object_custom_segments.put(gpa, section_index, .{
670652 .payload = .{
671653 .off = @enumFromInt(data_off),
672 .len = @intCast(debug_content.len),
654 .len = @intCast(len),
673655 },
674656 .flags = .{},
675657 .section_name = try wasm.internString(section_name),
676658 });
677 } else {
678 pos = section_end;
679 }
659 } else br.seek = section_end;
680660 },
681661 .type => {
682 const func_types_len, pos = readLeb(u32, bytes, pos);
662 const func_types_len = try br.takeLeb128(u32);
683663 for (try ss.func_types.addManyAsSlice(gpa, func_types_len)) |*func_type| {
684 if (bytes[pos] != std.wasm.function_type) return error.ExpectedFuncType;
685 pos += 1;
664 if (try br.takeByte() != std.wasm.function_type) return error.ExpectedFuncType;
686665
687 const params, pos = readBytes(bytes, pos);
688 const returns, pos = readBytes(bytes, pos);
666 const params = try br.take(try br.takeLeb128(u32));
667 const returns = try br.take(try br.takeLeb128(u32));
689668 func_type.* = try wasm.addFuncType(.{
690669 .params = .fromString(try wasm.internString(params)),
691670 .returns = .fromString(try wasm.internString(returns)),
......@@ -693,16 +672,16 @@ pub fn parse(
693672 }
694673 },
695674 .import => {
696 const imports_len, pos = readLeb(u32, bytes, pos);
675 const imports_len = try br.takeLeb128(u32);
697676 for (0..imports_len) |_| {
698 const module_name, pos = readBytes(bytes, pos);
699 const name, pos = readBytes(bytes, pos);
700 const kind, pos = readEnum(std.wasm.ExternalKind, bytes, pos);
677 const module_name = try br.take(try br.takeLeb128(u32));
678 const name = try br.take(try br.takeLeb128(u32));
679 const kind = try br.takeEnum(std.wasm.ExternalKind, .little);
701680 const interned_module_name = try wasm.internString(module_name);
702681 const interned_name = try wasm.internString(name);
703682 switch (kind) {
704683 .function => {
705 const function, pos = readLeb(u32, bytes, pos);
684 const function = try br.takeLeb128(u32);
706685 try ss.func_imports.append(gpa, .{
707686 .module_name = interned_module_name,
708687 .name = interned_name,
......@@ -710,7 +689,7 @@ pub fn parse(
710689 });
711690 },
712691 .memory => {
713 const limits, pos = readLimits(bytes, pos);
692 const limits = try readLimits(br);
714693 const gop = try wasm.object_memory_imports.getOrPut(gpa, interned_name);
715694 if (gop.found_existing) {
716695 if (gop.value_ptr.module_name != interned_module_name) {
......@@ -736,9 +715,12 @@ pub fn parse(
736715 }
737716 },
738717 .global => {
739 const valtype, pos = readEnum(std.wasm.Valtype, bytes, pos);
740 const mutable = bytes[pos] == 0x01;
741 pos += 1;
718 const valtype = try br.takeEnum(std.wasm.Valtype, .little);
719 const mutable = switch (try br.takeByte()) {
720 0 => false,
721 1 => true,
722 else => return error.InvalidMutability,
723 };
742724 try ss.global_imports.append(gpa, .{
743725 .name = interned_name,
744726 .valtype = valtype,
......@@ -747,8 +729,8 @@ pub fn parse(
747729 });
748730 },
749731 .table => {
750 const ref_type, pos = readEnum(std.wasm.RefType, bytes, pos);
751 const limits, pos = readLimits(bytes, pos);
732 const ref_type = try br.takeEnum(std.wasm.RefType, .little);
733 const limits = try readLimits(br);
752734 try ss.table_imports.append(gpa, .{
753735 .name = interned_name,
754736 .module_name = interned_module_name,
......@@ -763,17 +745,16 @@ pub fn parse(
763745 }
764746 },
765747 .function => {
766 const functions_len, pos = readLeb(u32, bytes, pos);
748 const functions_len = try br.takeLeb128(u32);
767749 for (try ss.func_type_indexes.addManyAsSlice(gpa, functions_len)) |*func_type_index| {
768 const i, pos = readLeb(u32, bytes, pos);
769 func_type_index.* = @enumFromInt(i);
750 func_type_index.* = @enumFromInt(try br.takeLeb128(u32));
770751 }
771752 },
772753 .table => {
773 const tables_len, pos = readLeb(u32, bytes, pos);
754 const tables_len = try br.takeLeb128(u32);
774755 for (try wasm.object_tables.addManyAsSlice(gpa, tables_len)) |*table| {
775 const ref_type, pos = readEnum(std.wasm.RefType, bytes, pos);
776 const limits, pos = readLimits(bytes, pos);
756 const ref_type = try br.takeEnum(std.wasm.RefType, .little);
757 const limits = try readLimits(br);
777758 table.* = .{
778759 .name = .none,
779760 .module_name = .none,
......@@ -788,9 +769,9 @@ pub fn parse(
788769 }
789770 },
790771 .memory => {
791 const memories_len, pos = readLeb(u32, bytes, pos);
772 const memories_len = try br.takeLeb128(u32);
792773 for (try wasm.object_memories.addManyAsSlice(gpa, memories_len)) |*memory| {
793 const limits, pos = readLimits(bytes, pos);
774 const limits = try readLimits(br);
794775 memory.* = .{
795776 .name = .none,
796777 .flags = .{
......@@ -807,14 +788,17 @@ pub fn parse(
807788 return diags.failParse(path, "object has more than one global section", .{});
808789 global_section_index = section_index;
809790
810 const section_start = pos;
811 const globals_len, pos = readLeb(u32, bytes, pos);
791 const section_start = br.seek;
792 const globals_len = try br.takeLeb128(u32);
812793 for (try wasm.object_globals.addManyAsSlice(gpa, globals_len)) |*global| {
813 const valtype, pos = readEnum(std.wasm.Valtype, bytes, pos);
814 const mutable = bytes[pos] == 0x01;
815 pos += 1;
816 const init_start = pos;
817 const expr, pos = try readInit(wasm, bytes, pos);
794 const valtype = try br.takeEnum(std.wasm.Valtype, .little);
795 const mutable = switch (try br.takeByte()) {
796 0 => false,
797 1 => true,
798 else => return error.InvalidMutability,
799 };
800 const init_start = br.seek;
801 const expr = try readInit(wasm, br);
818802 global.* = .{
819803 .name = .none,
820804 .flags = .{
......@@ -826,20 +810,19 @@ pub fn parse(
826810 .expr = expr,
827811 .object_index = object_index,
828812 .offset = @intCast(init_start - section_start),
829 .size = @intCast(pos - init_start),
813 .size = @intCast(br.seek - init_start),
830814 };
831815 }
832816 },
833817 .@"export" => {
834 const exports_len, pos = readLeb(u32, bytes, pos);
818 const exports_len = try br.takeLeb128(u32);
835819 // Read into scratch space, and then later add this data as if
836820 // it were extra symbol table entries, but allow merging with
837821 // existing symbol table data if the name matches.
838822 for (try ss.exports.addManyAsSlice(gpa, exports_len)) |*exp| {
839 const name, pos = readBytes(bytes, pos);
840 const kind: std.wasm.ExternalKind = @enumFromInt(bytes[pos]);
841 pos += 1;
842 const index, pos = readLeb(u32, bytes, pos);
823 const name = try br.take(try br.takeLeb128(u32));
824 const kind = try br.takeEnum(std.wasm.ExternalKind, .little);
825 const index = try br.takeLeb128(u32);
843826 exp.* = .{
844827 .name = try wasm.internString(name),
845828 .pointee = switch (kind) {
......@@ -852,25 +835,24 @@ pub fn parse(
852835 }
853836 },
854837 .start => {
855 const index, pos = readLeb(u32, bytes, pos);
838 const index = try br.takeLeb128(u32);
856839 start_function = @enumFromInt(functions_start + index);
857840 },
858841 .element => {
859842 log.warn("unimplemented: element section in {f} {?s}", .{ path, archive_member_name });
860 pos = section_end;
843 br.seek = section_end;
861844 },
862845 .code => {
863846 if (code_section_index != null)
864847 return diags.failParse(path, "object has more than one code section", .{});
865848 code_section_index = section_index;
866849
867 const start = pos;
868 const count, pos = readLeb(u32, bytes, pos);
850 const start = br.seek;
851 const count = try br.takeLeb128(u32);
869852 for (try wasm.object_functions.addManyAsSlice(gpa, count)) |*elem| {
870 const code_len, pos = readLeb(u32, bytes, pos);
871 const offset: u32 = @intCast(pos - start);
872 const payload = try wasm.addRelocatableDataPayload(bytes[pos..][0..code_len]);
873 pos += code_len;
853 const code_len = try br.takeLeb128(u32);
854 const offset: u32 = @intCast(br.seek - start);
855 const payload = try wasm.addRelocatableDataPayload(try br.take(code_len));
874856 elem.* = .{
875857 .flags = .{}, // populated from symbol table
876858 .name = .none, // populated from symbol table
......@@ -886,20 +868,19 @@ pub fn parse(
886868 return diags.failParse(path, "object has more than one data section", .{});
887869 data_section_index = section_index;
888870
889 const section_start = pos;
890 const count, pos = readLeb(u32, bytes, pos);
871 const section_start = br.seek;
872 const count = try br.takeLeb128(u32);
891873 for (try wasm.object_data_segments.addManyAsSlice(gpa, count)) |*elem| {
892 const flags, pos = readEnum(DataSegmentFlags, bytes, pos);
874 const flags: DataSegmentFlags = @enumFromInt(try br.takeLeb128(u32));
893875 if (flags == .active_memidx) {
894 const memidx, pos = readLeb(u32, bytes, pos);
876 const memidx = try br.takeLeb128(u32);
895877 if (memidx != 0) return diags.failParse(path, "data section uses mem index {d}", .{memidx});
896878 }
897 //const expr, pos = if (flags != .passive) try readInit(wasm, bytes, pos) else .{ .none, pos };
898 if (flags != .passive) pos = try skipInit(bytes, pos);
899 const data_len, pos = readLeb(u32, bytes, pos);
900 const segment_start = pos;
901 const payload = try wasm.addRelocatableDataPayload(bytes[pos..][0..data_len]);
902 pos += data_len;
879 //const expr = if (flags != .passive) try readInit(wasm, br) else .none;
880 if (flags != .passive) try skipInit(br);
881 const data_len = try br.takeLeb128(u32);
882 const segment_start = br.seek;
883 const payload = try wasm.addRelocatableDataPayload(try br.take(data_len));
903884 elem.* = .{
904885 .payload = payload,
905886 .name = .none, // Populated from segment_info
......@@ -911,10 +892,10 @@ pub fn parse(
911892 };
912893 }
913894 },
914 else => pos = section_end,
895 else => br.seek = section_end,
915896 }
916 if (pos != section_end) return error.MalformedSection;
917 }
897 if (br.seek != section_end) return error.MalformedSection;
898 } else |_| {}
918899 if (!saw_linking_section) return error.MissingLinkingSection;
919900
920901 const cpu = comp.root_mod.resolved_target.result.cpu;
......@@ -1422,27 +1403,27 @@ pub fn parse(
14221403/// Based on the "features" custom section, parses it into a list of
14231404/// features that tell the linker what features were enabled and may be mandatory
14241405/// to be able to link.
1425fn parseFeatures(
1426 wasm: *Wasm,
1427 bytes: []const u8,
1428 start_pos: usize,
1429 path: Path,
1430) error{ OutOfMemory, LinkFailure }!struct { Wasm.Feature.Set, usize } {
1406fn parseFeatures(wasm: *Wasm, reader: *Reader, path: Path) error{ OutOfMemory, LinkFailure }!Wasm.Feature.Set {
14311407 const gpa = wasm.base.comp.gpa;
14321408 const diags = &wasm.base.comp.link_diags;
1433 const features_len, var pos = readLeb(u32, bytes, start_pos);
1409 const features_len = reader.takeLeb128(u32) catch |err|
1410 return diags.failParse(path, "invalid features length: {t}", .{err});
14341411 // This temporary allocation could be avoided by using the string_bytes buffer as a scratch space.
14351412 const feature_buffer = try gpa.alloc(Wasm.Feature, features_len);
14361413 defer gpa.free(feature_buffer);
14371414 for (feature_buffer) |*feature| {
1438 const prefix: Wasm.Feature.Prefix = switch (bytes[pos]) {
1415 const prefix: Wasm.Feature.Prefix = switch (reader.takeByte() catch |err| {
1416 return diags.failParse(path, "invalid feature prefix: {t}", .{err});
1417 }) {
14391418 '-' => .@"-",
14401419 '+' => .@"+",
14411420 '=' => .@"=",
14421421 else => |b| return diags.failParse(path, "invalid feature prefix: 0x{x}", .{b}),
14431422 };
1444 pos += 1;
1445 const name, pos = readBytes(bytes, pos);
1423 const name_len = reader.takeLeb128(u32) catch |err|
1424 return diags.failParse(path, "bad feature name length: {t}", .{err});
1425 const name = reader.take(name_len) catch |err|
1426 return diags.failParse(path, "bad feature name: {t}", .{err});
14461427 const tag = std.meta.stringToEnum(Wasm.Feature.Tag, name) orelse {
14471428 return diags.failParse(path, "unrecognized wasm feature in object: {s}", .{name});
14481429 };
......@@ -1453,68 +1434,34 @@ fn parseFeatures(
14531434 }
14541435 std.mem.sortUnstable(Wasm.Feature, feature_buffer, {}, Wasm.Feature.lessThan);
14551436
1456 return .{
1457 .fromString(try wasm.internString(@ptrCast(feature_buffer))),
1458 pos,
1459 };
1437 return .fromString(try wasm.internString(@ptrCast(feature_buffer)));
14601438}
14611439
1462fn readLeb(comptime T: type, bytes: []const u8, pos: usize) struct { T, usize } {
1463 var fbr = std.io.fixedBufferStream(bytes[pos..]);
1440fn readLimits(reader: *Reader) !std.wasm.Limits {
1441 const flags: std.wasm.Limits.Flags = @bitCast(try reader.takeByte());
1442 const min = try reader.takeLeb128(u32);
1443 const max = if (flags.has_max) try reader.takeLeb128(u32) else 0;
14641444 return .{
1465 switch (@typeInfo(T).int.signedness) {
1466 .signed => std.leb.readIleb128(T, fbr.reader()) catch unreachable,
1467 .unsigned => std.leb.readUleb128(T, fbr.reader()) catch unreachable,
1468 },
1469 pos + fbr.pos,
1470 };
1471}
1472
1473fn readBytes(bytes: []const u8, start_pos: usize) struct { []const u8, usize } {
1474 const len, const pos = readLeb(u32, bytes, start_pos);
1475 return .{
1476 bytes[pos..][0..len],
1477 pos + len,
1478 };
1479}
1480
1481fn readEnum(comptime T: type, bytes: []const u8, pos: usize) struct { T, usize } {
1482 const Tag = @typeInfo(T).@"enum".tag_type;
1483 const int, const new_pos = readLeb(Tag, bytes, pos);
1484 return .{ @enumFromInt(int), new_pos };
1485}
1486
1487fn readLimits(bytes: []const u8, start_pos: usize) struct { std.wasm.Limits, usize } {
1488 const flags: std.wasm.Limits.Flags = @bitCast(bytes[start_pos]);
1489 const min, const max_pos = readLeb(u32, bytes, start_pos + 1);
1490 const max, const end_pos = if (flags.has_max) readLeb(u32, bytes, max_pos) else .{ 0, max_pos };
1491 return .{ .{
14921445 .flags = flags,
14931446 .min = min,
14941447 .max = max,
1495 }, end_pos };
1448 };
14961449}
14971450
1498fn readInit(wasm: *Wasm, bytes: []const u8, pos: usize) !struct { Wasm.Expr, usize } {
1499 const end_pos = try skipInit(bytes, pos); // one after the end opcode
1500 return .{ try wasm.addExpr(bytes[pos..end_pos]), end_pos };
1451fn readInit(wasm: *Wasm, reader: *Reader) !Wasm.Expr {
1452 const start = reader.seek;
1453 try skipInit(reader); // one after the end opcode
1454 return wasm.addExpr(reader.buffered()[start..reader.seek]);
15011455}
15021456
1503pub fn exprEndPos(bytes: []const u8, pos: usize) error{InvalidInitOpcode}!usize {
1504 const opcode = bytes[pos];
1505 return switch (@as(std.wasm.Opcode, @enumFromInt(opcode))) {
1506 .i32_const => readLeb(i32, bytes, pos + 1)[1],
1507 .i64_const => readLeb(i64, bytes, pos + 1)[1],
1508 .f32_const => pos + 5,
1509 .f64_const => pos + 9,
1510 .global_get => readLeb(u32, bytes, pos + 1)[1],
1457pub fn skipInit(reader: *Reader) !void {
1458 switch (try reader.takeEnumNonexhaustive(std.wasm.Opcode, .little)) {
1459 .i32_const => _ = try reader.takeLeb128(i32),
1460 .i64_const => _ = try reader.takeLeb128(i64),
1461 .f32_const => try reader.discardAll(5),
1462 .f64_const => try reader.discardAll(9),
1463 .global_get => _ = try reader.takeLeb128(u32),
15111464 else => return error.InvalidInitOpcode,
1512 };
1513}
1514
1515fn skipInit(bytes: []const u8, pos: usize) !usize {
1516 const end_pos = try exprEndPos(bytes, pos);
1517 const op, const final_pos = readEnum(std.wasm.Opcode, bytes, end_pos);
1518 if (op != .end) return error.InitExprMissingEnd;
1519 return final_pos;
1465 }
1466 if (try reader.takeEnum(std.wasm.Opcode, .little) != .end) return error.InitExprMissingEnd;
15201467}