Commit 4462d08224

Veikka Tuominen <git@vexu.eu>
2022-09-01 12:48:36
stage2 llvm: fix passing packed structs to callconv(.C) functions
Closes #12704
1 parent 2cd3989
Changed files (2)
src
codegen
test
src/codegen/llvm.zig
@@ -9853,6 +9853,8 @@ const ParamTypeIterator = struct {
                     .AnyFrame,
                     .Vector,
                     => true,
+                    .Struct => ty.containerLayout() == .Packed,
+                    .Union => ty.containerLayout() == .Packed,
 
                     else => false,
                 };
test/behavior/packed-struct.zig
@@ -579,3 +579,29 @@ test "runtime init of unnamed packed struct type" {
         }
     }{ .x = z }).m();
 }
+
+test "packed struct passed to callconv(.C) function" {
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+
+    const S = struct {
+        const Packed = packed struct {
+            a: u16,
+            b: bool = true,
+            c: bool = true,
+            d: u46 = 0,
+        };
+
+        fn foo(p: Packed, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) callconv(.C) bool {
+            return p.a == 12345 and p.b == true and p.c == true and p.d == 0 and a1 == 5 and a2 == 4 and a3 == 3 and a4 == 2 and a5 == 1;
+        }
+    };
+    const result = S.foo(S.Packed{
+        .a = 12345,
+        .b = true,
+        .c = true,
+    }, 5, 4, 3, 2, 1);
+    try expect(result);
+}