diff options
| author | Xavier Bouchoux <xavierb@gmail.com> | 2023-07-29 20:08:08 +0200 |
|---|---|---|
| committer | Xavier Bouchoux <xavierb@gmail.com> | 2023-10-03 06:39:20 +0200 |
| commit | 62d178e91af57c19d0ac000fe6930039a23e53a3 (patch) | |
| tree | f849674dd8aa13839319e05585b8513a0192b7ba /src/type.zig | |
| parent | 412d863ba5801c1376af7ab8f04a71b839a820a6 (diff) | |
| download | zig-62d178e91af57c19d0ac000fe6930039a23e53a3.tar.gz zig-62d178e91af57c19d0ac000fe6930039a23e53a3.zip | |
codegen: fix field offsets in packed structs
* add nested packed struct/union behavior tests
* use ptr_info.packed_offset rather than trying to duplicate the logic from Sema.structFieldPtrByIndex()
* use the container_ptr_info.packed_offset to account for non-aligned nested structs.
* dedup type.packedStructFieldBitOffset() and module.structPackedFieldBitOffset()
Diffstat (limited to 'src/type.zig')
| -rw-r--r-- | src/type.zig | 18 |
1 files changed, 2 insertions, 16 deletions
diff --git a/src/type.zig b/src/type.zig index 79be8b4c5b..a7b760a482 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3028,24 +3028,10 @@ pub const Type = struct { }; } - pub fn packedStructFieldBitOffset(ty: Type, field_index: usize, mod: *Module) u32 { + pub fn packedStructFieldByteOffset(ty: Type, field_index: u32, mod: *Module) u32 { const ip = &mod.intern_pool; const struct_type = ip.indexToKey(ty.toIntern()).struct_type; - assert(struct_type.layout == .Packed); - comptime assert(Type.packed_struct_layout_version == 2); - - var running_bits: u32 = 0; - for (struct_type.field_types.get(ip), 0..) |field_ty, i| { - if (i == field_index) break; - if (!field_ty.toType().hasRuntimeBits(mod)) continue; - const field_bits: u32 = @intCast(field_ty.toType().bitSize(mod)); - running_bits += field_bits; - } - return running_bits; - } - - pub fn packedStructFieldByteOffset(ty: Type, field_index: usize, mod: *Module) u32 { - return packedStructFieldBitOffset(ty, field_index, mod) / 8; + return @divExact(mod.structPackedFieldBitOffset(struct_type, field_index), 8); } pub const FieldOffset = struct { |
