authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-28 12:14:41+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-28 12:14:41+01:00
loga61ac9ecbf42a97a4f56f7384e5b862dcb169f4a
tree73413ea4f4ac89038dc2410d0c39cfe3f8ee9f85
parent57a823582cfb86a7c85c4598f5580325fb674ec4

x64: fix store with ABI size > 8 on stack; pass union tests


2 files changed, 57 insertions(+), 18 deletions(-)

src/arch/x86_64/CodeGen.zig+9-2
......@@ -2484,8 +2484,15 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
24842484 });
24852485 },
24862486 .stack_offset => {
2487 const tmp_reg = try self.copyToTmpRegister(value_ty, value);
2488 return self.store(ptr, .{ .register = tmp_reg }, ptr_ty, value_ty);
2487 if (abi_size <= 8) {
2488 const tmp_reg = try self.copyToTmpRegister(value_ty, value);
2489 return self.store(ptr, .{ .register = tmp_reg }, ptr_ty, value_ty);
2490 }
2491
2492 try self.genInlineMemcpy(0, value_ty, value, .{
2493 .source_stack_base = .rbp,
2494 .dest_stack_base = reg.to64(),
2495 });
24892496 },
24902497 else => |other| {
24912498 return self.fail("TODO implement set pointee with {}", .{other});
test/behavior/union.zig+48-16
......@@ -4,70 +4,102 @@ const expect = std.testing.expect;
44const expectEqual = std.testing.expectEqual;
55const Tag = std.meta.Tag;
66
7const Foo = union {
7const FooWithFloats = union {
88 float: f64,
99 int: i32,
1010};
1111
12test "basic unions" {
12test "basic unions with floats" {
1313 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
1414 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
1515 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1616
17 var foo = Foo{ .int = 1 };
17 var foo = FooWithFloats{ .int = 1 };
1818 try expect(foo.int == 1);
19 foo = Foo{ .float = 12.34 };
19 foo = FooWithFloats{ .float = 12.34 };
2020 try expect(foo.float == 12.34);
2121}
2222
23test "init union with runtime value" {
23fn setFloat(foo: *FooWithFloats, x: f64) void {
24 foo.* = FooWithFloats{ .float = x };
25}
26
27test "init union with runtime value - floats" {
2428 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
2529 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
2630 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
2731
28 var foo: Foo = undefined;
32 var foo: FooWithFloats = undefined;
2933
3034 setFloat(&foo, 12.34);
3135 try expect(foo.float == 12.34);
36}
37
38test "basic unions" {
39 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
40 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
41
42 var foo = Foo{ .int = 1 };
43 try expect(foo.int == 1);
44 foo = Foo{ .str = .{ .slice = "Hello!" } };
45 try expect(std.mem.eql(u8, foo.str.slice, "Hello!"));
46}
47
48const Foo = union {
49 int: i32,
50 str: struct {
51 slice: []const u8,
52 },
53};
54
55test "init union with runtime value" {
56 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
57 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
58
59 var foo: Foo = undefined;
3260
3361 setInt(&foo, 42);
3462 try expect(foo.int == 42);
35}
3663
37fn setFloat(foo: *Foo, x: f64) void {
38 foo.* = Foo{ .float = x };
64 setStr(&foo, "Hello!");
65 try expect(std.mem.eql(u8, foo.str.slice, "Hello!"));
3966}
4067
4168fn setInt(foo: *Foo, x: i32) void {
4269 foo.* = Foo{ .int = x };
4370}
4471
72fn setStr(foo: *Foo, slice: []const u8) void {
73 foo.* = Foo{ .str = .{ .slice = slice } };
74}
75
4576test "comptime union field access" {
4677 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
4778
4879 comptime {
49 var foo = Foo{ .int = 0 };
80 var foo = FooWithFloats{ .int = 0 };
5081 try expect(foo.int == 0);
5182
52 foo = Foo{ .float = 42.42 };
53 try expect(foo.float == 42.42);
83 foo = FooWithFloats{ .float = 12.34 };
84 try expect(foo.float == 12.34);
5485 }
5586}
5687
5788const FooExtern = extern union {
58 float: f64,
5989 int: i32,
90 str: struct {
91 slice: []const u8,
92 },
6093};
6194
6295test "basic extern unions" {
63 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
6496 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
6597 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
6698
6799 var foo = FooExtern{ .int = 1 };
68100 try expect(foo.int == 1);
69 foo.float = 12.34;
70 try expect(foo.float == 12.34);
101 foo.str.slice = "Well";
102 try expect(std.mem.eql(u8, foo.str.slice, "Well"));
71103}
72104
73105const ExternPtrOrInt = extern union {