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,...@@ -76,6 +76,7 @@ pt: Zcu.PerThread,
76mir_instructions: *std.MultiArrayList(Mir.Inst),76mir_instructions: *std.MultiArrayList(Mir.Inst),
77/// Contains extra data for MIR77/// Contains extra data for MIR
78mir_extra: *std.ArrayListUnmanaged(u32),78mir_extra: *std.ArrayListUnmanaged(u32),
79start_mir_extra_off: u32,
79/// List of all locals' types generated throughout this declaration80/// List of all locals' types generated throughout this declaration
80/// used to emit locals count at start of 'code' section.81/// used to emit locals count at start of 'code' section.
81locals: *std.ArrayListUnmanaged(u8),82locals: *std.ArrayListUnmanaged(u8),
...@@ -859,7 +860,7 @@ fn addTag(cg: *CodeGen, tag: Mir.Inst.Tag) error{OutOfMemory}!void {...@@ -859,7 +860,7 @@ fn addTag(cg: *CodeGen, tag: Mir.Inst.Tag) error{OutOfMemory}!void {
859}860}
860861
861fn addExtended(cg: *CodeGen, opcode: std.wasm.MiscOpcode) error{OutOfMemory}!void {862fn 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();
863 try cg.mir_extra.append(cg.gpa, @intFromEnum(opcode));864 try cg.mir_extra.append(cg.gpa, @intFromEnum(opcode));
864 try cg.addInst(.{ .tag = .misc_prefix, .data = .{ .payload = extra_index } });865 try cg.addInst(.{ .tag = .misc_prefix, .data = .{ .payload = extra_index } });
865}866}
...@@ -890,7 +891,7 @@ fn addImm64(cg: *CodeGen, imm: u64) error{OutOfMemory}!void {...@@ -890,7 +891,7 @@ fn addImm64(cg: *CodeGen, imm: u64) error{OutOfMemory}!void {
890/// Accepts the index into the list of 128bit-immediates891/// Accepts the index into the list of 128bit-immediates
891fn addImm128(cg: *CodeGen, index: u32) error{OutOfMemory}!void {892fn addImm128(cg: *CodeGen, index: u32) error{OutOfMemory}!void {
892 const simd_values = cg.simd_immediates.items[index];893 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();
894 // tag + 128bit value895 // tag + 128bit value
895 try cg.mir_extra.ensureUnusedCapacity(cg.gpa, 5);896 try cg.mir_extra.ensureUnusedCapacity(cg.gpa, 5);
896 cg.mir_extra.appendAssumeCapacity(@intFromEnum(std.wasm.SimdOpcode.v128_const));897 cg.mir_extra.appendAssumeCapacity(@intFromEnum(std.wasm.SimdOpcode.v128_const));
...@@ -935,7 +936,7 @@ fn addExtra(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {...@@ -935,7 +936,7 @@ fn addExtra(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {
935/// Returns the index into `mir_extra`936/// Returns the index into `mir_extra`
936fn addExtraAssumeCapacity(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {937fn addExtraAssumeCapacity(cg: *CodeGen, extra: anytype) error{OutOfMemory}!u32 {
937 const fields = std.meta.fields(@TypeOf(extra));938 const fields = std.meta.fields(@TypeOf(extra));
938 const result = @as(u32, @intCast(cg.mir_extra.items.len));939 const result = cg.extraLen();
939 inline for (fields) |field| {940 inline for (fields) |field| {
940 cg.mir_extra.appendAssumeCapacity(switch (field.type) {941 cg.mir_extra.appendAssumeCapacity(switch (field.type) {
941 u32 => @field(extra, field.name),942 u32 => @field(extra, field.name),
...@@ -1267,6 +1268,7 @@ pub fn function(...@@ -1267,6 +1268,7 @@ pub fn function(
1267 .mir_instructions = &wasm.mir_instructions,1268 .mir_instructions = &wasm.mir_instructions,
1268 .mir_extra = &wasm.mir_extra,1269 .mir_extra = &wasm.mir_extra,
1269 .locals = &wasm.all_zcu_locals,1270 .locals = &wasm.all_zcu_locals,
1271 .start_mir_extra_off = @intCast(wasm.mir_extra.items.len),
1270 };1272 };
1271 defer code_gen.deinit();1273 defer code_gen.deinit();
12721274
...@@ -1281,7 +1283,6 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {...@@ -1281,7 +1283,6 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {
1281 const zcu = cg.pt.zcu;1283 const zcu = cg.pt.zcu;
12821284
1283 const start_mir_off: u32 = @intCast(wasm.mir_instructions.len);1285 const start_mir_off: u32 = @intCast(wasm.mir_instructions.len);
1284 const start_mir_extra_off: u32 = @intCast(wasm.mir_extra.items.len);
1285 const start_locals_off: u32 = @intCast(wasm.all_zcu_locals.items.len);1286 const start_locals_off: u32 = @intCast(wasm.all_zcu_locals.items.len);
12861287
1287 try cg.branches.append(cg.gpa, .{});1288 try cg.branches.append(cg.gpa, .{});
...@@ -1310,8 +1311,8 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {...@@ -1310,8 +1311,8 @@ fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {
1310 return .{1311 return .{
1311 .mir_off = start_mir_off,1312 .mir_off = start_mir_off,
1312 .mir_len = @intCast(wasm.mir_instructions.len - start_mir_off),1313 .mir_len = @intCast(wasm.mir_instructions.len - start_mir_off),
1313 .mir_extra_off = start_mir_extra_off,1314 .mir_extra_off = cg.start_mir_extra_off,
1314 .mir_extra_len = @intCast(wasm.mir_extra.items.len - start_mir_extra_off),1315 .mir_extra_len = cg.extraLen(),
1315 .locals_off = start_locals_off,1316 .locals_off = start_locals_off,
1316 .locals_len = @intCast(wasm.all_zcu_locals.items.len - start_locals_off),1317 .locals_len = @intCast(wasm.all_zcu_locals.items.len - start_locals_off),
1317 .prologue = if (cg.initial_stack_value == .none) .none else .{1318 .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...@@ -2349,7 +2350,7 @@ fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErr
2349 try cg.emitWValue(lhs);2350 try cg.emitWValue(lhs);
2350 try cg.lowerToStack(rhs);2351 try cg.lowerToStack(rhs);
2351 // TODO: Add helper functions for simd opcodes2352 // TODO: Add helper functions for simd opcodes
2352 const extra_index: u32 = @intCast(cg.mir_extra.items.len);2353 const extra_index = cg.extraLen();
2353 // stores as := opcode, offset, alignment (opcode::memarg)2354 // stores as := opcode, offset, alignment (opcode::memarg)
2354 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{2355 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
2355 @intFromEnum(std.wasm.SimdOpcode.v128_store),2356 @intFromEnum(std.wasm.SimdOpcode.v128_store),
...@@ -2463,7 +2464,7 @@ fn load(cg: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue...@@ -2463,7 +2464,7 @@ fn load(cg: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue
24632464
2464 if (ty.zigTypeTag(zcu) == .vector) {2465 if (ty.zigTypeTag(zcu) == .vector) {
2465 // TODO: Add helper functions for simd opcodes2466 // 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();
2467 // stores as := opcode, offset, alignment (opcode::memarg)2468 // stores as := opcode, offset, alignment (opcode::memarg)
2468 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{2469 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
2469 @intFromEnum(std.wasm.SimdOpcode.v128_load),2470 @intFromEnum(std.wasm.SimdOpcode.v128_load),
...@@ -4872,7 +4873,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -4872,7 +4873,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
48724873
4873 try cg.emitWValue(array);4874 try cg.emitWValue(array);
48744875
4875 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));4876 const extra_index = cg.extraLen();
4876 try cg.mir_extra.appendSlice(cg.gpa, &operands);4877 try cg.mir_extra.appendSlice(cg.gpa, &operands);
4877 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });4878 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 {...@@ -5024,7 +5025,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5024 else => break :blk, // Cannot make use of simd-instructions5025 else => break :blk, // Cannot make use of simd-instructions
5025 };5026 };
5026 try cg.emitWValue(operand);5027 try cg.emitWValue(operand);
5027 const extra_index: u32 = @intCast(cg.mir_extra.items.len);5028 const extra_index: u32 = cg.extraLen();
5028 // stores as := opcode, offset, alignment (opcode::memarg)5029 // stores as := opcode, offset, alignment (opcode::memarg)
5029 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{5030 try cg.mir_extra.appendSlice(cg.gpa, &[_]u32{
5030 opcode,5031 opcode,
...@@ -5043,7 +5044,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5043,7 +5044,7 @@ fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5043 else => break :blk, // Cannot make use of simd-instructions5044 else => break :blk, // Cannot make use of simd-instructions
5044 };5045 };
5045 try cg.emitWValue(operand);5046 try cg.emitWValue(operand);
5046 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));5047 const extra_index = cg.extraLen();
5047 try cg.mir_extra.append(cg.gpa, opcode);5048 try cg.mir_extra.append(cg.gpa, opcode);
5048 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });5049 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });
5049 return cg.finishAir(inst, .stack, &.{ty_op.operand});5050 return cg.finishAir(inst, .stack, &.{ty_op.operand});
...@@ -5131,7 +5132,7 @@ fn airShuffle(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5131,7 +5132,7 @@ fn airShuffle(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5131 try cg.emitWValue(a);5132 try cg.emitWValue(a);
5132 try cg.emitWValue(b);5133 try cg.emitWValue(b);
51335134
5134 const extra_index = @as(u32, @intCast(cg.mir_extra.items.len));5135 const extra_index = cg.extraLen();
5135 try cg.mir_extra.appendSlice(cg.gpa, &operands);5136 try cg.mir_extra.appendSlice(cg.gpa, &operands);
5136 try cg.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } });5137 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 {...@@ -7482,3 +7483,7 @@ fn floatCmpIntrinsic(op: std.math.CompareOperator, bits: u16) Mir.Intrinsic {
7482 },7483 },
7483 };7484 };
7484}7485}
7486
7487fn extraLen(cg: *const CodeGen) u32 {
7488 return @intCast(cg.mir_extra.items.len - cg.start_mir_extra_off);
7489}