aboutsummaryrefslogtreecommitdiff
path: root/src/Compilation.zig
diff options
context:
space:
mode:
authorkcbanner <kcbanner@gmail.com>2023-10-01 00:07:21 -0400
committerkcbanner <kcbanner@gmail.com>2023-10-02 13:15:28 -0400
commitd657b6c0e2ab7c47f5416dc4df1abb2bfbecd4b6 (patch)
tree1c28a341c2b8094468bac5121a3dc955f16f8582 /src/Compilation.zig
parent53775b0999527ced0550f3abb32c4a4a715af74e (diff)
downloadzig-d657b6c0e2ab7c47f5416dc4df1abb2bfbecd4b6.tar.gz
zig-d657b6c0e2ab7c47f5416dc4df1abb2bfbecd4b6.zip
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.
Diffstat (limited to 'src/Compilation.zig')
0 files changed, 0 insertions, 0 deletions