From 5b171f446f46c97de111bce0575452be0334a05e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 23 Dec 2021 23:57:02 -0700 Subject: stage2: initial implementation of packed structs Layout algorithm: all `align(0)` fields are squished together as if they were a single integer with a number of bits equal to `@bitSizeOf` each field added together. Then the natural ABI alignment of that integer is used for that pseudo-field. --- src/Module.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 4f9f8307db..25e262855c 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -849,6 +849,24 @@ pub const Struct = struct { /// undefined until `status` is `have_layout`. offset: u32, is_comptime: bool, + + /// Returns the field alignment, assuming the struct is packed. + pub fn packedAlignment(field: Field) u32 { + if (field.abi_align.tag() == .abi_align_default) { + return 0; + } else { + return @intCast(u32, field.abi_align.toUnsignedInt()); + } + } + + /// Returns the field alignment, assuming the struct is not packed. + pub fn normalAlignment(field: Field, target: Target) u32 { + if (field.abi_align.tag() == .abi_align_default) { + return field.ty.abiAlignment(target); + } else { + return @intCast(u32, field.abi_align.toUnsignedInt()); + } + } }; pub fn getFullyQualifiedName(s: *Struct, gpa: Allocator) ![:0]u8 { -- cgit v1.2.3