authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-18 15:57:20-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
log1a58ae2ed6d38a2a73cacb8b97f2885685505838
treea7e8fbb1b9d757514b422e360b4d596c89489c7e
parent3c70392210d3bd68943b3ebd09612fddfe52b082

wasm codegen: fix extra index not relative


1 files changed, 17 insertions(+), 12 deletions(-)

src/arch/wasm/CodeGen.zig+17-12
......@@ -76,6 +76,7 @@ pt: Zcu.PerThread,
7676mir_instructions: *std.MultiArrayList(Mir.Inst),
7777/// Contains extra data for MIR
7878mir_extra: *std.ArrayListUnmanaged(u32),
79start_mir_extra_off: u32,
7980/// List of all locals' types generated throughout this declaration
8081/// used to emit locals count at start of 'code' section.
8182locals: *std.ArrayListUnmanaged(u8),
......@@ -859,7 +860,7 @@ fn addTag(cg: *CodeGen, tag: Mir.Inst.Tag) error{OutOfMemory}!void {
859860}
860861
861862fn addExtended(cg: *CodeGen, opcode: std.wasm.MiscOpcode) error{OutOfMemory}!void {
862 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
863 const extra_index = cg.extraLen();
863864 try cg.mir_extra.append(cg.gpa, @intFromEnum(opcode));
864865 try cg.addInst(.{ .tag = .misc_prefix, .data = .{ .payload = extra_index } });
865866}
......@@ -890,7 +891,7 @@ fn addImm64(cg: *CodeGen, imm: u64) error{OutOfMemory}!void {
890891/// Accepts the index into the list of 128bit-immediates
891892fn addImm128(cg: *CodeGen, index: u32) error{OutOfMemory}!void {
892893 const simd_values = cg.simd_immediates.items[index];
893 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
894 const extra_index = cg.extraLen();
894895 // tag + 128bit value
895896 try cg.mir_extra.ensureUnusedCapacity(cg.gpa, 5);
896897 cg.mir_extra.appendAssumeCapacity(@intFromEnum(std.wasm.SimdOpcode.v128_const));
......@@ -935,7 +936,7 @@ fn addExtra(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {
935936/// Returns the index into `mir_extra`
936937fn addExtraAssumeCapacity(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {
937938 const fields = std.meta.fields(@TypeOf(extra));
938 const result = @as(u32, @intCast(cg.mir_extra.items.len));
939 const result = cg.extraLen();
939940 inline for (fields) |field| {
940941 cg.mir_extra.appendAssumeCapacity(switch (field.type) {
941942 u32 => @field(extra, field.name),
......@@ -1267,6 +1268,7 @@ pub fn function(
12671268 .mir_instructions = &wasm.mir_instructions,
12681269 .mir_extra = &wasm.mir_extra,
12691270 .locals = &wasm.all_zcu_locals,
1271 .start_mir_extra_off = @intCast(wasm.mir_extra.items.len),
12701272 };
12711273 defer code_gen.deinit();
12721274
......@@ -1281,7 +1283,6 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {
12811283 const zcu = cg.pt.zcu;
12821284
12831285 const start_mir_off: u32 = @intCast(wasm.mir_instructions.len);
1284 const start_mir_extra_off: u32 = @intCast(wasm.mir_extra.items.len);
12851286 const start_locals_off: u32 = @intCast(wasm.all_zcu_locals.items.len);
12861287
12871288 try cg.branches.append(cg.gpa, .{});
......@@ -1310,8 +1311,8 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {
13101311 return .{
13111312 .mir_off = start_mir_off,
13121313 .mir_len = @intCast(wasm.mir_instructions.len - start_mir_off),
1313 .mir_extra_off = start_mir_extra_off,
1314 .mir_extra_len = @intCast(wasm.mir_extra.items.len - start_mir_extra_off),
1314 .mir_extra_off = cg.start_mir_extra_off,
1315 .mir_extra_len = cg.extraLen(),
13151316 .locals_off = start_locals_off,
13161317 .locals_len = @intCast(wasm.all_zcu_locals.items.len - start_locals_off),
13171318 .prologue = if (cg.initial_stack_value == .none) .none else .{
......@@ -2349,7 +2350,7 @@ fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErr
23492350 try cg.emitWValue(lhs);
23502351 try cg.lowerToStack(rhs);
23512352 // TODO: Add helper functions for simd opcodes
2352 const extra_index: u32 = @intCast(cg.mir_extra.items.len);
2353 const extra_index = cg.extraLen();
23532354 // stores as := opcode, offset, alignment (opcode::memarg)
23542355 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
23552356 @intFromEnum(std.wasm.SimdOpcode.v128_store),
......@@ -2463,7 +2464,7 @@ fn load(cg: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue
24632464
24642465 if (ty.zigTypeTag(zcu) == .vector) {
24652466 // TODO: Add helper functions for simd opcodes
2466 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
2467 const extra_index = cg.extraLen();
24672468 // stores as := opcode, offset, alignment (opcode::memarg)
24682469 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
24692470 @intFromEnum(std.wasm.SimdOpcode.v128_load),
......@@ -4872,7 +4873,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
48724873
48734874 try cg.emitWValue(array);
48744875
4875 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
4876 const extra_index = cg.extraLen();
48764877 try cg.mir_extra.appendSlice(cg.gpa, &operands);
48774878 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });
48784879
......@@ -5024,7 +5025,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
50245025 else => break :blk, // Cannot make use of simd-instructions
50255026 };
50265027 try cg.emitWValue(operand);
5027 const extra_index: u32 = @intCast(cg.mir_extra.items.len);
5028 const extra_index: u32 = cg.extraLen();
50285029 // stores as := opcode, offset, alignment (opcode::memarg)
50295030 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
50305031 opcode,
......@@ -5043,7 +5044,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
50435044 else => break :blk, // Cannot make use of simd-instructions
50445045 };
50455046 try cg.emitWValue(operand);
5046 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
5047 const extra_index = cg.extraLen();
50475048 try cg.mir_extra.append(cg.gpa, opcode);
50485049 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });
50495050 return cg.finishAir(inst, .stack, &.{ty_op.operand});
......@@ -5131,7 +5132,7 @@ fn airShuffle(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
51315132 try cg.emitWValue(a);
51325133 try cg.emitWValue(b);
51335134
5134 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));
5135 const extra_index = cg.extraLen();
51355136 try cg.mir_extra.appendSlice(cg.gpa, &operands);
51365137 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });
51375138
......@@ -7482,3 +7483,7 @@ fn floatCmpIntrinsic(op: std.math.CompareOperator, bits: u16) Mir.Intrinsic {
74827483 },
74837484 };
74847485}
7486
7487fn extraLen(cg: *const CodeGen) u32 {
7488 return @intCast(cg.mir_extra.items.len - cg.start_mir_extra_off);
7489}