authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-10 15:04:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-10 15:04:39-07:00
log3c3bc5af29a5fcd1daaf8d8d39625c7b505e80bf
tree265b5cfeb0847a23439a58e8c54e31bf4bb2a615
parent58bc562cb45d5d27fed2e69ee289a4b5c9199cff

Sema: introduce bitSizeAdvanced to recursively resolve types

Same pattern as abiSizeAdvanced. Fixes compiler crash for nested packed structs.

3 files changed, 63 insertions(+), 57 deletions(-)

src/Sema.zig+3-3
......@@ -11747,11 +11747,11 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1174711747
1174811748fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1174911749 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
11750 const src = inst_data.src();
1175011751 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
11751 const unresolved_operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
11752 const operand_ty = try sema.resolveTypeFields(block, operand_src, unresolved_operand_ty);
11752 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
1175311753 const target = sema.mod.getTarget();
11754 const bit_size = operand_ty.bitSize(target);
11754 const bit_size = try operand_ty.bitSizeAdvanced(target, sema.kit(block, src));
1175511755 return sema.addIntUnsigned(Type.comptime_int, bit_size);
1175611756}
1175711757
src/type.zig+52-51
......@@ -3542,9 +3542,19 @@ pub const Type = extern union {
35423542 );
35433543 }
35443544
3545 /// Asserts the type has the bit size already resolved.
35463545 pub fn bitSize(ty: Type, target: Target) u64 {
3547 return switch (ty.tag()) {
3546 return bitSizeAdvanced(ty, target, null) catch unreachable;
3547 }
3548
3549 /// If you pass `sema_kit`, any recursive type resolutions will happen if
3550 /// necessary, possibly returning a CompileError. Passing `null` instead asserts
3551 /// the type is fully resolved, and there will be no error, guaranteed.
3552 pub fn bitSizeAdvanced(
3553 ty: Type,
3554 target: Target,
3555 sema_kit: ?Module.WipAnalysis,
3556 ) Module.CompileError!u64 {
3557 switch (ty.tag()) {
35483558 .fn_noreturn_no_args => unreachable, // represents machine code; not a pointer
35493559 .fn_void_no_args => unreachable, // represents machine code; not a pointer
35503560 .fn_naked_noreturn_no_args => unreachable, // represents machine code; not a pointer
......@@ -3568,40 +3578,30 @@ pub const Type = extern union {
35683578 .generic_poison => unreachable,
35693579 .bound_fn => unreachable,
35703580
3571 .void => 0,
3572 .bool, .u1 => 1,
3573 .u8, .i8 => 8,
3574 .i16, .u16, .f16 => 16,
3575 .u29 => 29,
3576 .i32, .u32, .f32 => 32,
3577 .i64, .u64, .f64 => 64,
3578 .f80 => 80,
3579 .u128, .i128, .f128 => 128,
3581 .void => return 0,
3582 .bool, .u1 => return 1,
3583 .u8, .i8 => return 8,
3584 .i16, .u16, .f16 => return 16,
3585 .u29 => return 29,
3586 .i32, .u32, .f32 => return 32,
3587 .i64, .u64, .f64 => return 64,
3588 .f80 => return 80,
3589 .u128, .i128, .f128 => return 128,
35803590
35813591 .@"struct" => {
3582 const field_count = ty.structFieldCount();
3583 if (field_count == 0) return 0;
3584
3585 const struct_obj = ty.castTag(.@"struct").?.data;
3586 assert(struct_obj.haveFieldTypes());
3587
3588 switch (struct_obj.layout) {
3589 .Auto, .Extern => {
3590 var total: u64 = 0;
3591 for (struct_obj.fields.values()) |field| {
3592 total += field.ty.bitSize(target);
3593 }
3594 return total;
3595 },
3596 .Packed => return struct_obj.packedIntegerBits(target),
3592 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
3593 var total: u64 = 0;
3594 for (ty.structFields().values()) |field| {
3595 total += try bitSizeAdvanced(field.ty, target, sema_kit);
35973596 }
3597 return total;
35983598 },
35993599
36003600 .tuple, .anon_struct => {
3601 const tuple = ty.tupleFields();
3601 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
36023602 var total: u64 = 0;
3603 for (tuple.types) |field_ty| {
3604 total += field_ty.bitSize(target);
3603 for (ty.tupleFields().types) |field_ty| {
3604 total += try bitSizeAdvanced(field_ty, target, sema_kit);
36053605 }
36063606 return total;
36073607 },
......@@ -3609,37 +3609,35 @@ pub const Type = extern union {
36093609 .enum_simple, .enum_full, .enum_nonexhaustive, .enum_numbered => {
36103610 var buffer: Payload.Bits = undefined;
36113611 const int_tag_ty = ty.intTagType(&buffer);
3612 return int_tag_ty.bitSize(target);
3612 return try bitSizeAdvanced(int_tag_ty, target, sema_kit);
36133613 },
36143614
36153615 .@"union", .union_tagged => {
3616 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
36163617 const union_obj = ty.cast(Payload.Union).?.data;
3617
3618 const fields = union_obj.fields;
3619 if (fields.count() == 0) return 0;
3620
36213618 assert(union_obj.haveFieldTypes());
36223619
36233620 var size: u64 = 0;
3624 for (fields.values()) |field| {
3625 size = @maximum(size, field.ty.bitSize(target));
3621 for (union_obj.fields.values()) |field| {
3622 size = @maximum(size, try bitSizeAdvanced(field.ty, target, sema_kit));
36263623 }
36273624 return size;
36283625 },
36293626
36303627 .vector => {
36313628 const payload = ty.castTag(.vector).?.data;
3632 const elem_bit_size = payload.elem_type.bitSize(target);
3629 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
36333630 return elem_bit_size * payload.len;
36343631 },
3635 .array_u8 => 8 * ty.castTag(.array_u8).?.data,
3636 .array_u8_sentinel_0 => 8 * (ty.castTag(.array_u8_sentinel_0).?.data + 1),
3632 .array_u8 => return 8 * ty.castTag(.array_u8).?.data,
3633 .array_u8_sentinel_0 => return 8 * (ty.castTag(.array_u8_sentinel_0).?.data + 1),
36373634 .array => {
36383635 const payload = ty.castTag(.array).?.data;
36393636 const elem_size = std.math.max(payload.elem_type.abiAlignment(target), payload.elem_type.abiSize(target));
36403637 if (elem_size == 0 or payload.len == 0)
3641 return 0;
3642 return (payload.len - 1) * 8 * elem_size + payload.elem_type.bitSize(target);
3638 return @as(u64, 0);
3639 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
3640 return (payload.len - 1) * 8 * elem_size + elem_bit_size;
36433641 },
36443642 .array_sentinel => {
36453643 const payload = ty.castTag(.array_sentinel).?.data;
......@@ -3647,14 +3645,15 @@ pub const Type = extern union {
36473645 payload.elem_type.abiAlignment(target),
36483646 payload.elem_type.abiSize(target),
36493647 );
3650 return payload.len * 8 * elem_size + payload.elem_type.bitSize(target);
3648 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
3649 return payload.len * 8 * elem_size + elem_bit_size;
36513650 },
36523651
36533652 .isize,
36543653 .usize,
36553654 .@"anyframe",
36563655 .anyframe_T,
3657 => target.cpu.arch.ptrBitWidth(),
3656 => return target.cpu.arch.ptrBitWidth(),
36583657
36593658 .const_slice,
36603659 .mut_slice,
......@@ -3662,7 +3661,7 @@ pub const Type = extern union {
36623661
36633662 .const_slice_u8,
36643663 .const_slice_u8_sentinel_0,
3665 => target.cpu.arch.ptrBitWidth() * 2,
3664 => return target.cpu.arch.ptrBitWidth() * 2,
36663665
36673666 .optional_single_const_pointer,
36683667 .optional_single_mut_pointer,
......@@ -3681,8 +3680,8 @@ pub const Type = extern union {
36813680 },
36823681
36833682 .pointer => switch (ty.castTag(.pointer).?.data.size) {
3684 .Slice => target.cpu.arch.ptrBitWidth() * 2,
3685 else => target.cpu.arch.ptrBitWidth(),
3683 .Slice => return target.cpu.arch.ptrBitWidth() * 2,
3684 else => return target.cpu.arch.ptrBitWidth(),
36863685 },
36873686
36883687 .manyptr_u8,
......@@ -3708,7 +3707,7 @@ pub const Type = extern union {
37083707 .error_set_merged,
37093708 => return 16, // TODO revisit this when we have the concept of the error tag type
37103709
3711 .int_signed, .int_unsigned => ty.cast(Payload.Bits).?.data,
3710 .int_signed, .int_unsigned => return ty.cast(Payload.Bits).?.data,
37123711
37133712 .optional => {
37143713 var buf: Payload.ElemType = undefined;
......@@ -3722,7 +3721,8 @@ pub const Type = extern union {
37223721 // field and a boolean as the second. Since the child type's abi alignment is
37233722 // guaranteed to be >= that of bool's (1 byte) the added size is exactly equal
37243723 // to the child type's ABI alignment.
3725 return child_type.bitSize(target) + 1;
3724 const child_bit_size = try bitSizeAdvanced(child_type, target, sema_kit);
3725 return child_bit_size + 1;
37263726 },
37273727
37283728 .error_union => {
......@@ -3730,9 +3730,9 @@ pub const Type = extern union {
37303730 if (!payload.error_set.hasRuntimeBits() and !payload.payload.hasRuntimeBits()) {
37313731 return 0;
37323732 } else if (!payload.error_set.hasRuntimeBits()) {
3733 return payload.payload.bitSize(target);
3733 return payload.payload.bitSizeAdvanced(target, sema_kit);
37343734 } else if (!payload.payload.hasRuntimeBits()) {
3735 return payload.error_set.bitSize(target);
3735 return payload.error_set.bitSizeAdvanced(target, sema_kit);
37363736 }
37373737 @panic("TODO bitSize error union");
37383738 },
......@@ -3749,7 +3749,7 @@ pub const Type = extern union {
37493749 .extern_options,
37503750 .type_info,
37513751 => @panic("TODO at some point we gotta resolve builtin types"),
3752 };
3752 }
37533753 }
37543754
37553755 pub fn isSinglePointer(self: Type) bool {
......@@ -5506,6 +5506,7 @@ pub const Type = extern union {
55065506 switch (ty.tag()) {
55075507 .@"struct" => {
55085508 const struct_obj = ty.castTag(.@"struct").?.data;
5509 assert(struct_obj.haveFieldTypes());
55095510 return struct_obj.fields.count();
55105511 },
55115512 .empty_struct, .empty_struct_literal => return 0,
test/behavior/packed-struct.zig+8-3
......@@ -243,7 +243,12 @@ test "correct sizeOf and offsets in packed structs" {
243243}
244244
245245test "nested packed structs" {
246 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
246 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
247 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
248 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
249 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
250 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
251 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
247252
248253 const S1 = packed struct { a: u8, b: u8, c: u8 };
249254
......@@ -253,7 +258,7 @@ test "nested packed structs" {
253258 const S3Padded = packed struct { s3: S3, pad: u16 };
254259
255260 try expectEqual(48, @bitSizeOf(S3));
256 try expectEqual(6, @sizeOf(S3));
261 try expectEqual(@sizeOf(u48), @sizeOf(S3));
257262
258263 try expectEqual(3, @offsetOf(S3, "y"));
259264 try expectEqual(24, @bitOffsetOf(S3, "y"));
......@@ -273,7 +278,7 @@ test "nested packed structs" {
273278 const S6 = packed struct { a: i32, b: S4, c: i8 };
274279
275280 const expectedBitSize = 80;
276 const expectedByteSize = expectedBitSize / 8;
281 const expectedByteSize = @sizeOf(u80);
277282 try expectEqual(expectedBitSize, @bitSizeOf(S5));
278283 try expectEqual(expectedByteSize, @sizeOf(S5));
279284 try expectEqual(expectedBitSize, @bitSizeOf(S6));