authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-11-26 16:21:59+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-11-30 17:56:02+01:00
loga314e86772c539e803dfd51c34de52675189b34a
tree561398e2941c16a2dee739d597e78cf18f4f0958
parent4af5bbde53da7f6ac8acc630135e569885bd94c4
signaturelock-open Commit is signed but in an unrecognized format.

wasm: support passing packed struct over C-ABI

This also adds support loading a runtime pointer from a packed struct. Also, this commit improves many utility functions such as `trunc` and `intcast` to also support non-integer types such as booleans.

2 files changed, 80 insertions(+), 40 deletions(-)

src/arch/wasm/CodeGen.zig+64-34
......@@ -483,7 +483,12 @@ fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode {
483483 .f32 => if (args.signedness.? == .signed) return .i32_trunc_f32_s else return .i32_trunc_f32_u,
484484 .f64 => if (args.signedness.? == .signed) return .i32_trunc_f64_s else return .i32_trunc_f64_u,
485485 } else return .f32_trunc, // when no valtype2, it's an f16 instead which is stored in an i32.
486 .i64 => unreachable,
486 .i64 => switch (args.valtype2.?) {
487 .i32 => unreachable,
488 .i64 => unreachable,
489 .f32 => if (args.signedness.? == .signed) return .i64_trunc_f32_s else return .i64_trunc_f32_u,
490 .f64 => if (args.signedness.? == .signed) return .i64_trunc_f64_s else return .i64_trunc_f64_u,
491 },
487492 .f32 => return .f32_trunc,
488493 .f64 => return .f64_trunc,
489494 },
......@@ -687,7 +692,10 @@ const InnerError = error{
687692};
688693
689694pub fn deinit(func: *CodeGen) void {
690 assert(func.branches.items.len == 0); // we should end with no branches left. Forgot a call to `branches.pop()`?
695 // in case of an error and we still have branches
696 for (func.branches.items) |*branch| {
697 branch.deinit(func.gpa);
698 }
691699 func.branches.deinit(func.gpa);
692700 func.blocks.deinit(func.gpa);
693701 func.locals.deinit(func.gpa);
......@@ -1317,17 +1325,21 @@ fn lowerArg(func: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value:
13171325 assert(ty_classes[0] == .direct);
13181326 const scalar_type = abi.scalarType(ty, func.target);
13191327 const abi_size = scalar_type.abiSize(func.target);
1320 const opcode = buildOpcode(.{
1321 .op = .load,
1322 .width = @intCast(u8, abi_size),
1323 .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned,
1324 .valtype1 = typeToValtype(scalar_type, func.target),
1325 });
13261328 try func.emitWValue(value);
1327 try func.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{
1328 .offset = value.offset(),
1329 .alignment = scalar_type.abiAlignment(func.target),
1330 });
1329
1330 // When the value lives in the virtual stack, we must load it onto the actual stack
1331 if (value != .imm32 and value != .imm64) {
1332 const opcode = buildOpcode(.{
1333 .op = .load,
1334 .width = @intCast(u8, abi_size),
1335 .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned,
1336 .valtype1 = typeToValtype(scalar_type, func.target),
1337 });
1338 try func.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{
1339 .offset = value.offset(),
1340 .alignment = scalar_type.abiAlignment(func.target),
1341 });
1342 }
13311343 },
13321344 .Int, .Float => {
13331345 if (ty_classes[1] == .none) {
......@@ -2478,18 +2490,21 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, ptr_child_ty: Type) InnerError
24782490 const parent_ptr = try func.lowerParentPtr(field_ptr.container_ptr, parent_ty);
24792491
24802492 const offset = switch (parent_ty.zigTypeTag()) {
2481 .Struct => blk: {
2482 const offset = parent_ty.structFieldOffset(field_ptr.field_index, func.target);
2483 break :blk offset;
2493 .Struct => switch (parent_ty.containerLayout()) {
2494 .Packed => parent_ty.packedStructFieldByteOffset(field_ptr.field_index, func.target),
2495 else => parent_ty.structFieldOffset(field_ptr.field_index, func.target),
24842496 },
2485 .Union => blk: {
2486 const layout: Module.Union.Layout = parent_ty.unionGetLayout(func.target);
2487 if (layout.payload_size == 0) break :blk 0;
2488 if (layout.payload_align > layout.tag_align) break :blk 0;
2489
2490 // tag is stored first so calculate offset from where payload starts
2491 const offset = @intCast(u32, std.mem.alignForwardGeneric(u64, layout.tag_size, layout.tag_align));
2492 break :blk offset;
2497 .Union => switch (parent_ty.containerLayout()) {
2498 .Packed => 0,
2499 else => blk: {
2500 const layout: Module.Union.Layout = parent_ty.unionGetLayout(func.target);
2501 if (layout.payload_size == 0) break :blk 0;
2502 if (layout.payload_align > layout.tag_align) break :blk 0;
2503
2504 // tag is stored first so calculate offset from where payload starts
2505 const offset = @intCast(u32, std.mem.alignForwardGeneric(u64, layout.tag_size, layout.tag_align));
2506 break :blk offset;
2507 },
24932508 },
24942509 .Pointer => switch (parent_ty.ptrSize()) {
24952510 .Slice => switch (field_ptr.field_index) {
......@@ -2751,6 +2766,11 @@ fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue {
27512766 .ErrorUnion => {
27522767 return WValue{ .imm32 = 0xaaaaaaaa };
27532768 },
2769 .Struct => {
2770 const struct_obj = ty.castTag(.@"struct").?.data;
2771 assert(struct_obj.layout == .Packed);
2772 return func.emitUndefined(struct_obj.backing_int_ty);
2773 },
27542774 else => return func.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty.zigTypeTag()}),
27552775 }
27562776}
......@@ -3201,6 +3221,18 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
32013221 const truncated = try func.trunc(shifted_value, int_type, backing_ty);
32023222 const bitcasted = try func.bitcast(field_ty, int_type, truncated);
32033223 break :result try bitcasted.toLocal(func, field_ty);
3224 } else if (field_ty.isPtrAtRuntime() and struct_obj.fields.count() == 1) {
3225 // In this case we do not have to perform any transformations,
3226 // we can simply reuse the operand.
3227 break :result func.reuseOperand(struct_field.struct_operand, operand);
3228 } else if (field_ty.isPtrAtRuntime()) {
3229 var payload: Type.Payload.Bits = .{
3230 .base = .{ .tag = .int_unsigned },
3231 .data = @intCast(u16, field_ty.bitSize(func.target)),
3232 };
3233 const int_type = Type.initPayload(&payload.base);
3234 const truncated = try func.trunc(shifted_value, int_type, backing_ty);
3235 break :result try truncated.toLocal(func, field_ty);
32043236 }
32053237 const truncated = try func.trunc(shifted_value, field_ty, backing_ty);
32063238 break :result try truncated.toLocal(func, field_ty);
......@@ -3225,6 +3257,7 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
32253257 break :result try field.toLocal(func, field_ty);
32263258 },
32273259 };
3260
32283261 func.finishAir(inst, result, &.{struct_field.struct_operand});
32293262}
32303263
......@@ -3571,13 +3604,13 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
35713604/// Asserts type's bitsize <= 128
35723605/// NOTE: May leave the result on the top of the stack.
35733606fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue {
3574 const given_info = given.intInfo(func.target);
3575 const wanted_info = wanted.intInfo(func.target);
3576 assert(given_info.bits <= 128);
3577 assert(wanted_info.bits <= 128);
3607 const given_bitsize = @intCast(u16, given.bitSize(func.target));
3608 const wanted_bitsize = @intCast(u16, wanted.bitSize(func.target));
3609 assert(given_bitsize <= 128);
3610 assert(wanted_bitsize <= 128);
35783611
3579 const op_bits = toWasmBits(given_info.bits).?;
3580 const wanted_bits = toWasmBits(wanted_info.bits).?;
3612 const op_bits = toWasmBits(given_bitsize).?;
3613 const wanted_bits = toWasmBits(wanted_bitsize).?;
35813614 if (op_bits == wanted_bits) return operand;
35823615
35833616 if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) {
......@@ -3585,10 +3618,7 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro
35853618 try func.addTag(.i32_wrap_i64);
35863619 } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) {
35873620 try func.emitWValue(operand);
3588 try func.addTag(switch (wanted_info.signedness) {
3589 .signed => .i64_extend_i32_s,
3590 .unsigned => .i64_extend_i32_u,
3591 });
3621 try func.addTag(if (wanted.isSignedInt()) .i64_extend_i32_s else .i64_extend_i32_u);
35923622 } else if (wanted_bits == 128) {
35933623 // for 128bit integers we store the integer in the virtual stack, rather than a local
35943624 const stack_ptr = try func.allocStack(wanted);
......@@ -3869,7 +3899,7 @@ fn trunc(func: *CodeGen, operand: WValue, wanted_ty: Type, given_ty: Type) Inner
38693899 }
38703900
38713901 var result = try func.intcast(operand, given_ty, wanted_ty);
3872 const wanted_bits = wanted_ty.intInfo(func.target).bits;
3902 const wanted_bits = @intCast(u16, wanted_ty.bitSize(func.target));
38733903 const wasm_bits = toWasmBits(wanted_bits).?;
38743904 if (wasm_bits != wanted_bits) {
38753905 result = try func.wrapOperand(result, wanted_ty);
src/arch/wasm/abi.zig+16-6
......@@ -94,15 +94,25 @@ pub fn classifyType(ty: Type, target: Target) [2]Class {
9494pub fn scalarType(ty: Type, target: std.Target) Type {
9595 switch (ty.zigTypeTag()) {
9696 .Struct => {
97 std.debug.assert(ty.structFieldCount() == 1);
98 return scalarType(ty.structFieldType(0), target);
97 switch (ty.containerLayout()) {
98 .Packed => {
99 const struct_obj = ty.castTag(.@"struct").?.data;
100 return scalarType(struct_obj.backing_int_ty, target);
101 },
102 else => {
103 std.debug.assert(ty.structFieldCount() == 1);
104 return scalarType(ty.structFieldType(0), target);
105 },
106 }
99107 },
100108 .Union => {
101 const layout = ty.unionGetLayout(target);
102 if (layout.payload_size == 0 and layout.tag_size != 0) {
103 return scalarType(ty.unionTagTypeSafety().?, target);
109 if (ty.containerLayout() != .Packed) {
110 const layout = ty.unionGetLayout(target);
111 if (layout.payload_size == 0 and layout.tag_size != 0) {
112 return scalarType(ty.unionTagTypeSafety().?, target);
113 }
114 std.debug.assert(ty.unionFields().count() == 1);
104115 }
105 std.debug.assert(ty.unionFields().count() == 1);
106116 return scalarType(ty.unionFields().values()[0].ty, target);
107117 },
108118 else => return ty,