[foreign-jextract] RFR: 8262198: Overhaul bitfield parsing logic

Maurizio Cimadamore mcimadamore at openjdk.java.net
Tue Feb 23 17:11:46 UTC 2021


On Tue, 23 Feb 2021 15:34:06 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> This parsing stratgey cannot (by definition) fail - so, moving forward there will be no more exceptions coming from this side of the code. Of course, since we no longer have container classification, when bitfield support is added to the backend, things might be a bit tricker; however I realized that, since the layout is well-formed, the backend can always use the layout API to query the offset and size of a given bitfield, and decide on the best strategy to get there (e.g. get the closed 8/16/32/64-bit word and apply some bitmasking). So, perhaps things don't change much in terms of code generation support.
>> 
>> This patch fixes a small bug in Parser, where we where creating the same tree twice (!!). There are some small tweaks to TreeMaker, to work with the new bitfield info coming out from RecordLayoutComputer, but overall nothing too difficult here.
>> 
>> Note that the `BadBitfieldTest`, which was previously asserting a jextract crash, has now turned into a positive test (and I've dropped the exclusion on Windows, since the test should now pass on all platforms).
>> 
>> [1] - https://github.com/rust-lang/rust-bindgen/issues/743
>
> src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/RecordLayoutComputer.java line 139:
> 
>> 137: 
>> 138:     MemoryLayout bitfield(List<MemoryLayout> sublayouts) {
>> 139:         return LayoutUtils.setBitfields(MemoryLayout.ofStruct(sublayouts.toArray(new MemoryLayout[0])));
> 
> Does this work correctly for unions? What if we have many bitfields that together end up being large than the union itself?
> 
> union Foo {
>     int x;
>     int bitfield1: 24;
>     int bitfield2: 24;
> };
> Doesn't this get laid out like:
> union Foo {
>     int x;
>     struct {
>         int bitfield1: 24;
>         int bitfield2: 24;
>     }
> };
> 
> With this patch? The layouts seem to differ between those two.

My understanding is that bitfields in union are just disjoint. If you want them to be adjacent, then you need to put them inside a struct.

https://godbolt.org/z/nGoe38

> src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/UnionLayoutComputer.java line 93:
> 
>> 91:         if (actualSize < expectedSize) {
>> 92:             // emit an extra padding of expected size to make sure union layout size is computed correctly
>> 93:             addFieldLayout(MemoryLayout.ofPaddingBits(expectedSize));
> 
> Maybe an assert could be added here to check that `actualSize` is exactly `expectedSize` at the end, to catch the case where `actualSize` is larger than `expectedSize`. I think we have a problem otherwise.

actual size can be less if there are bitfields - in which case we need to emit a padding word to make sure the union layout has right size.

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/459


More information about the panama-dev mailing list