From 51491186cb045520ace001e005164a97a8695504 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 20 Oct 2022 10:37:05 +0300 Subject: [PATCH] stage2: fix x86_64 C ABI of struct with array member Closes #12185 --- src/arch/x86_64/abi.zig | 13 +++++++++++++ test/c_abi/cfuncs.c | 15 +++++++++++++++ test/c_abi/main.zig | 16 ++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/arch/x86_64/abi.zig b/src/arch/x86_64/abi.zig index 298fc6656f72b2f860338124cd42121e2d46db2e..45c576054097d2b66bddaa10c8b7c7376daaa680 100644 --- a/src/arch/x86_64/abi.zig +++ b/src/arch/x86_64/abi.zig @@ -388,6 +388,19 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { } return result; }, + .Array => { + const ty_size = ty.abiSize(target); + if (ty_size <= 64) { + result[0] = .integer; + return result; + } + if (ty_size <= 128) { + result[0] = .integer; + result[1] = .integer; + return result; + } + return memory_class; + }, else => unreachable, } } diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index 004dc9c4068c6754a50f5711d6af6b6bd3f70e44..382840e2aa17f3fe0385779bae8c9e742b9ca911 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -596,3 +596,18 @@ int32_t c_ret_i32() { int64_t c_ret_i64() { return -1; } + +typedef struct { + uint32_t a; + uint8_t padding[4]; + uint64_t b; +} StructWithArray; + +void c_struct_with_array(StructWithArray x) { + assert_or_panic(x.a == 1); + assert_or_panic(x.b == 2); +} + +StructWithArray c_ret_struct_with_array() { + return (StructWithArray) { 4, {}, 155 }; +} diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 9344c2efaee49af836e887d0301dabbf174c451b..6f1b336b011d145e45e3df1de6ed014cafd8c57b 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -665,3 +665,19 @@ test "C ABI integer return types" { try expect(c_ret_i32() == -1); try expect(c_ret_i64() == -1); } + +const StructWithArray = extern struct { + a: i32, + padding: [4]u8, + b: i64, +}; +extern fn c_struct_with_array(StructWithArray) void; +extern fn c_ret_struct_with_array() StructWithArray; + +test "Struct with array as padding." { + c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 }); + + var x = c_ret_struct_with_array(); + try std.testing.expect(x.a == 4); + try std.testing.expect(x.b == 155); +} -- 2.54.0