From d657b6c0e2ab7c47f5416dc4df1abb2bfbecd4b6 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 1 Oct 2023 00:07:21 -0400 Subject: sema: support reinterpreting extern/packed unions at comptime via field access My previous change for reading / writing to unions at comptime did not handle union field read/writes correctly in all cases. Previously, if a field was written to a union, it would overwrite the entire value. This is problematic when a field of a larger size is subsequently read, because the value would not be long enough, causing a panic. Additionally, the writing behaviour itself was incorrect. Writing to a field of a packed or extern union should only overwrite the bits corresponding to that field, allowing for memory reintepretation via field writes / reads. I addressed these problems as follows: Add the concept of a "backing type" for extern / packed unions (`Type.unionBackingType`). For extern unions, this is a `u8` array, for packed unions it's an integer matching the `bitSize` of the union. Whenever union memory is read at comptime, it's read as this type. When union memory is written at comptime, the tag may still be known. If so, the memory is written using the tagged type. If the tag is unknown (because this union had previously been read from memory), it's simply written back out as the backing type. I added `write_packed` to the `reinterpret` field of `ComptimePtrMutationKit`. This causes writes of the operand to be packed - which is necessary when writing to a field of a packed union. Without this, writing a value to a `u1` field would overwrite the entire byte it occupied. The final case to address was reading a different (potentially larger) field from a union when it was written with a known tag. To handle this, a new kind of bitcast was introduced (`bitCastUnionFieldVal`) which supports reading a larger field by using a backing buffer that has the unwritten bits set to undefined. The reason to support this (vs always just writing the union as it's backing type), is that no reads to larger fields ever occur at comptime, it would be strictly worse to have spent time writing the full backing type. --- src/type.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index 79be8b4c5b..396d1496de 100644 --- a/src/type.zig +++ b/src/type.zig @@ -1954,6 +1954,16 @@ pub const Type = struct { return true; } + /// Returns the type used for backing storage of this union during comptime operations. + /// Asserts the type is either an extern or packed union. + pub fn unionBackingType(ty: Type, mod: *Module) !Type { + return switch (ty.containerLayout(mod)) { + .Extern => try mod.arrayType(.{ .len = ty.abiSize(mod), .child = .u8_type }), + .Packed => try mod.intType(.unsigned, @intCast(ty.bitSize(mod))), + .Auto => unreachable, + }; + } + pub fn unionGetLayout(ty: Type, mod: *Module) Module.UnionLayout { const ip = &mod.intern_pool; const union_type = ip.indexToKey(ty.toIntern()).union_type; -- cgit v1.2.3