authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 10:37:05+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 20:11:12+03:00
log51491186cb045520ace001e005164a97a8695504
tree4f1dad21c514bfd59badee340940331e48ce9f13
parentce95cc170bfa763d31fc498169ee9e44adca57fa

stage2: fix x86_64 C ABI of struct with array member

Closes #12185

3 files changed, 44 insertions(+), 0 deletions(-)

src/arch/x86_64/abi.zig+13
......@@ -388,6 +388,19 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
388388 }
389389 return result;
390390 },
391 .Array => {
392 const ty_size = ty.abiSize(target);
393 if (ty_size <= 64) {
394 result[0] = .integer;
395 return result;
396 }
397 if (ty_size <= 128) {
398 result[0] = .integer;
399 result[1] = .integer;
400 return result;
401 }
402 return memory_class;
403 },
391404 else => unreachable,
392405 }
393406}
test/c_abi/cfuncs.c+15
......@@ -596,3 +596,18 @@ int32_t c_ret_i32() {
596596int64_t c_ret_i64() {
597597 return -1;
598598}
599
600typedef struct {
601 uint32_t a;
602 uint8_t padding[4];
603 uint64_t b;
604} StructWithArray;
605
606void c_struct_with_array(StructWithArray x) {
607 assert_or_panic(x.a == 1);
608 assert_or_panic(x.b == 2);
609}
610
611StructWithArray c_ret_struct_with_array() {
612 return (StructWithArray) { 4, {}, 155 };
613}
test/c_abi/main.zig+16
......@@ -665,3 +665,19 @@ test "C ABI integer return types" {
665665 try expect(c_ret_i32() == -1);
666666 try expect(c_ret_i64() == -1);
667667}
668
669const StructWithArray = extern struct {
670 a: i32,
671 padding: [4]u8,
672 b: i64,
673};
674extern fn c_struct_with_array(StructWithArray) void;
675extern fn c_ret_struct_with_array() StructWithArray;
676
677test "Struct with array as padding." {
678 c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
679
680 var x = c_ret_struct_with_array();
681 try std.testing.expect(x.a == 4);
682 try std.testing.expect(x.b == 155);
683}