Commit 51491186cb

Veikka Tuominen <git@vexu.eu>
2022-10-20 09:37:05
stage2: fix x86_64 C ABI of struct with array member
Closes #12185
1 parent ce95cc1
Changed files (3)
src
arch
x86_64
test
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,
     }
 }
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 };
+}
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);
+}