authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-01 13:48:36+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-02 17:57:10+03:00
log4462d082240d71e671a8d3d5fb3da81e70c4760e
tree197bd8c2b1e7d79ce4ddcbb6a94a9a1196e63f78
parent2cd3989cb32d97c8a60d9cf4154e049815259b46

stage2 llvm: fix passing packed structs to callconv(.C) functions

Closes #12704

2 files changed, 28 insertions(+), 0 deletions(-)

src/codegen/llvm.zig+2
...@@ -9853,6 +9853,8 @@ const ParamTypeIterator = struct {...@@ -9853,6 +9853,8 @@ const ParamTypeIterator = struct {
9853 .AnyFrame,9853 .AnyFrame,
9854 .Vector,9854 .Vector,
9855 => true,9855 => true,
9856 .Struct => ty.containerLayout() == .Packed,
9857 .Union => ty.containerLayout() == .Packed,
98569858
9857 else => false,9859 else => false,
9858 };9860 };
test/behavior/packed-struct.zig+26
...@@ -579,3 +579,29 @@ test "runtime init of unnamed packed struct type" {...@@ -579,3 +579,29 @@ test "runtime init of unnamed packed struct type" {
579 }579 }
580 }{ .x = z }).m();580 }{ .x = z }).m();
581}581}
582
583test "packed struct passed to callconv(.C) function" {
584 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
585 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
586 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
587 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
588
589 const S = struct {
590 const Packed = packed struct {
591 a: u16,
592 b: bool = true,
593 c: bool = true,
594 d: u46 = 0,
595 };
596
597 fn foo(p: Packed, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) callconv(.C) bool {
598 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;
599 }
600 };
601 const result = S.foo(S.Packed{
602 .a = 12345,
603 .b = true,
604 .c = true,
605 }, 5, 4, 3, 2, 1);
606 try expect(result);
607}