C lang Bit Fields described by MemoryLayout API
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Nov 29 11:51:24 UTC 2023
Hi Daniel,
memory layouts cannot be used to describe sub-byte structure.
A bit-field is really a repetition of one or more "storage words" (where
the native compiler makes the decision of which storage word to use, and
how many of them). When you access a bitfield, the compiler gives you
access to the correct storage word containing the bitfield, and then it
applies some bit masking on top, to make it look like you are accessing
a single entity. In other words, a bit-field access operation is always
a memory access operation (involving a power-of-two storage word)
followed by some in-register computation. Memory layouts can capture the
former, but not the latter.
The best thing jextract could do to model bit-fields correctly would be
to obtain the storage info from clang, and at least generate a layout
which contains all the correct storage words. Then, on top, it would be
relatively trivial for jextract to generate simple getter/setters which
use some bit masking to do what you wanna do.
The main issue is that libclang, to date, does not expose decisions
regarding bitfield storage. We have tried several times (and failed) to
infer the correct storage words and number, but we always end up with
bugs and discrepancies. We just need access to the raw data that clang
is working with (we can see this data when libclang dumps the AST, but
the data is not made available through the libclang API).
For this reason, jextract support for bitfields is somewhat limited (but
we hope to be able to fix this, when better support for bitfields in
libclang becomes available!)
Maurizio
On 29/11/2023 02:09, Daniel Meier wrote:
> Hello,
>
> How can C language Bit Fields (https://learn.microsoft.com/en-us/cpp/c-language/c-bit-fields) be described using MemoryLayout API? I've tried to apply jextract at FSP_FSCTL_VOLUME_PARAMS struct (https://github.com/winfsp/winfsp/blob/master/inc/winfsp/fsctl.h) and it failed to generate all the structure fields. So I wonder how to describe memory layout of such structure manually.
>
> Thank you.
>
> Regards
> Daniel
More information about the panama-dev
mailing list