authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-26 23:13:01-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-27 00:12:56-05:00
log81318e870418d017244d6d133aabca19f2c63b58
treecf2798c2ada2b1901f6e2129ea45c6566c40febe
parent46b49a0a766a39ca37ba48f1a1c2ed28c260b08b

llvm: add asserts and behavior tests for #14063

Closes #14063

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

src/codegen/llvm.zig+4
...@@ -1928,6 +1928,7 @@ pub const Object = struct {...@@ -1928,6 +1928,7 @@ pub const Object = struct {
1928 if (ty.castTag(.@"struct")) |payload| {1928 if (ty.castTag(.@"struct")) |payload| {
1929 const struct_obj = payload.data;1929 const struct_obj = payload.data;
1930 if (struct_obj.layout == .Packed and struct_obj.haveFieldTypes()) {1930 if (struct_obj.layout == .Packed and struct_obj.haveFieldTypes()) {
1931 assert(struct_obj.haveLayout());
1931 const info = struct_obj.backing_int_ty.intInfo(target);1932 const info = struct_obj.backing_int_ty.intInfo(target);
1932 const dwarf_encoding: c_uint = switch (info.signedness) {1933 const dwarf_encoding: c_uint = switch (info.signedness) {
1933 .signed => DW.ATE.signed,1934 .signed => DW.ATE.signed,
...@@ -2931,6 +2932,7 @@ pub const DeclGen = struct {...@@ -2931,6 +2932,7 @@ pub const DeclGen = struct {
2931 const struct_obj = t.castTag(.@"struct").?.data;2932 const struct_obj = t.castTag(.@"struct").?.data;
29322933
2933 if (struct_obj.layout == .Packed) {2934 if (struct_obj.layout == .Packed) {
2935 assert(struct_obj.haveLayout());
2934 const int_llvm_ty = try dg.lowerType(struct_obj.backing_int_ty);2936 const int_llvm_ty = try dg.lowerType(struct_obj.backing_int_ty);
2935 gop.value_ptr.* = int_llvm_ty;2937 gop.value_ptr.* = int_llvm_ty;
2936 return int_llvm_ty;2938 return int_llvm_ty;
...@@ -3632,6 +3634,7 @@ pub const DeclGen = struct {...@@ -3632,6 +3634,7 @@ pub const DeclGen = struct {
3632 const struct_obj = tv.ty.castTag(.@"struct").?.data;3634 const struct_obj = tv.ty.castTag(.@"struct").?.data;
36333635
3634 if (struct_obj.layout == .Packed) {3636 if (struct_obj.layout == .Packed) {
3637 assert(struct_obj.haveLayout());
3635 const big_bits = struct_obj.backing_int_ty.bitSize(target);3638 const big_bits = struct_obj.backing_int_ty.bitSize(target);
3636 const int_llvm_ty = dg.context.intType(@intCast(c_uint, big_bits));3639 const int_llvm_ty = dg.context.intType(@intCast(c_uint, big_bits));
3637 const fields = struct_obj.fields.values();3640 const fields = struct_obj.fields.values();
...@@ -9152,6 +9155,7 @@ pub const FuncGen = struct {...@@ -9152,6 +9155,7 @@ pub const FuncGen = struct {
9152 .Struct => {9155 .Struct => {
9153 if (result_ty.containerLayout() == .Packed) {9156 if (result_ty.containerLayout() == .Packed) {
9154 const struct_obj = result_ty.castTag(.@"struct").?.data;9157 const struct_obj = result_ty.castTag(.@"struct").?.data;
9158 assert(struct_obj.haveLayout());
9155 const big_bits = struct_obj.backing_int_ty.bitSize(target);9159 const big_bits = struct_obj.backing_int_ty.bitSize(target);
9156 const int_llvm_ty = self.context.intType(@intCast(c_uint, big_bits));9160 const int_llvm_ty = self.context.intType(@intCast(c_uint, big_bits));
9157 const fields = struct_obj.fields.values();9161 const fields = struct_obj.fields.values();
test/behavior/struct.zig+14
...@@ -1430,6 +1430,12 @@ test "struct has only one reference" {...@@ -1430,6 +1430,12 @@ test "struct has only one reference" {
1430 fn errorUnionStructReturn() error{Foo}!struct { x: u8 } {1430 fn errorUnionStructReturn() error{Foo}!struct { x: u8 } {
1431 return error.Foo;1431 return error.Foo;
1432 }1432 }
1433
1434 fn pointerPackedStruct(_: *packed struct { x: u8 }) void {}
1435 fn nestedPointerPackedStruct(_: struct { x: *packed struct { x: u8 } }) void {}
1436 fn pointerNestedPackedStruct(_: *struct { x: packed struct { x: u8 } }) void {}
1437 fn pointerNestedPointerPackedStruct(_: *struct { x: *packed struct { x: u8 } }) void {}
1438
1433 fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int {1439 fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int {
1434 return x.?;1440 return x.?;
1435 }1441 }
...@@ -1446,6 +1452,14 @@ test "struct has only one reference" {...@@ -1446,6 +1452,14 @@ test "struct has only one reference" {
1446 const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn;1452 const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn;
1447 try expect(optional_struct_return != error_union_struct_return);1453 try expect(optional_struct_return != error_union_struct_return);
14481454
1455 const pointer_packed_struct: *const anyopaque = &S.pointerPackedStruct;
1456 const nested_pointer_packed_struct: *const anyopaque = &S.nestedPointerPackedStruct;
1457 try expect(pointer_packed_struct != nested_pointer_packed_struct);
1458
1459 const pointer_nested_packed_struct: *const anyopaque = &S.pointerNestedPackedStruct;
1460 const pointer_nested_pointer_packed_struct: *const anyopaque = &S.pointerNestedPointerPackedStruct;
1461 try expect(pointer_nested_packed_struct != pointer_nested_pointer_packed_struct);
1462
1449 try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {})));1463 try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {})));
1450 try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 })));1464 try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 })));
1451 try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 })));1465 try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 })));