-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve packed
semantics
#19660
Comments
Whats the rationale that favors this over |
Here
The code instead shows @Snektron From what I understand it forces using |
I like 1-3, I don't see a need for 4, and 5 seems like a complex feature that combines unions and structs that implements #985 but only through a builtin, not syntax. |
For me, it's something that prevents accidential comparability and allows for types that must not have a defined value (thus, cannot be constructed from zig land). Use case here would be modelling microcontroller register values where some fields have the value "don't ever touch these bits, do not write anything else than what you've read"
Yeah, it is similar to
No, both are correct. The backing type of a
True. Will adapt the original post to respect that, it's reasonable.
I can see that, it's not something that is necessarily needed, but would help in some cases where fields might overlap. But after some considerations, this could also be fully solved by casting between different instances of types |
5 might actually be helpful for some osdev interop things where a packed struct field represents the upper bits of an address, aligned with the start of the struct but with lower bits used for other things |
That was just an example, |
not sure what this actually means, i meant point 5 of having something like structlayout.explicit but at bit resolution |
This is a plan for hammer out the semantics for bit packed types in the Zig language.
Let's start with a tiny syntactical change:
(1) Rename
packed
tobitpacked
This change will reflect the true meaning of both
packed struct
andpacked union
more thanpacked
and reduces confusion about the type of packing that is performed (as other languages usepacked
for byte packing).This should be trivial to implement and update via
zig fmt
.(2) Improve
bitpacked union
semanticsRight now, the semantics of packed unions are kinda … underdefined:
Let's change that definition to:
This also means that not all union members share the same address, which is true for
extern union
.(3) No changes to
bitpacked struct
Except for the rename of
packed
tobitpacked
, no changes are done here.(4) Introduce a new type
opaquebits(N)
This new type
opaquebits(N)
is an opaque bit bag that has no semantics defined except for "it takes N bits of space".This type can be used for storing fully opaque data and the only way to interpret its content is by using
@bitCast
from and to it.(5) Introduce a new type class
std.builtin.Type.BitPacked
(This is definitly a stretch goal, and might require further work)
This type can be the backing type for both
bitpacked union
andbitpacked struct
, which is similar to a regular struct/union type in that it has typed fields, but lacks most other properties.The
BitPacked
type is a mix of structure and union and inspired by C#StructLayout.Explicit
andFieldOffset
, which allow construction of both structures and unions of an arbitrary layout.This means that it's also legal to allow overlapping fields. For a union type, all
bit_offset
values are 0, for a structure, allbit_offset
fields are layed out such that all fields are back-to-back.Each field in a
BitPacked
type has an explicit bit offset and backing type:It is not legal to create a type with unnamed bits.
Each
BitPacked
type behaves in codegen and on ABI boundaries as if it would beBitPacked.backing_integer
.(6) Incorporate #19395
Use
bitpacked(T) struct
/bitpacked(T) union
instead ofbitpacked struct(T)
bitpacked union
as those are more uniform in what the(T)
actually modifies.Use Case
One example where i'd like to have seen some of these features would be the EDID Video Input Definition:
For additional type safety,
config
could've been done asopaquebits(7)
and afn unpack() union(enum){…}
function could be done that returns either/or contained structure definition.Some other structures in EDID contain even more nested bit field definitions on a singular byte or integer, so a more improved support for bitpacked data can help for sure.
Summary
For me, (1) to (4) are things that are definitly useful and would improve language semantics. (5) is definitly more of a stretch goal.
The text was updated successfully, but these errors were encountered: