Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8260974

Jextract should generate accessors for bitfields

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: repo-panama
    • Fix Version/s: None
    • Component/s: tools

      Description

      Given a struct with bitfields:

      struct Foo {
         int i:4;
         int j:12;
      };

      jextract currently does not emit accessors for "i" and "j".

      Internally we have all we need to do this generation. Given the above struct, jextract generates the following internal representation:

      Scoped: STRUCT Foo layout = [b32[abi/kind=INT,contents=[b4(i)[layout/name=i]b12(j)[layout/name=j]x16]]](Foo)[layout/name=Foo]
          Scoped: BITFIELDS layout = [b4(i)[layout/name=i]b12(j)[layout/name=j]x16]
              Variable: BITFIELD i type = Int(layout = b32[abi/kind=INT]), layout = Optional[b4]
              Variable: BITFIELD j type = Int(layout = b32[abi/kind=INT]), layout = Optional[b12]


      From the above, we can derive a lot of info:

      1) there is one "BITFIELDS" scoped abstraction - its size is 4 + 12 + 16 = 32 - which means that the "container" size is 32 (an int)
      2) from the layout associated to the BITFIELDS abstraction, we can recover relative offsets (inside the container) and sizes for "i" and "j" - for instance, offset_i = 0, size_i = 4
      3) from relative offsets and sizes, we can generate bitfields accessors, using the scheme described below

      Note: there is probably something missing in the above, in that the group layout associated to the bitfield container doesn't have a name - this probably makes things hard, as elsewhere in jextract we use name-based resolution e.g. to find offsets of fields inside structs.

      In case of a getter we have to:

      1) get the value of the enclosing "container" (e.g. in case of "x" that is an int) - this can be done with a memory access VarHandle
      2) to some bit manipulation of the value obtained above (e.g. shift right to get rid of offset, and then mask so that all bits outside the field size are set to zero)

      In case of a setter we have to:

      1) get the value of the enclosing "container" (e.g. in case of "x" that is an int) - this can be done with a memory access VarHandle
      2) write the new bitfield value in the value obtained above (this will require again some bit-level manipulation to make sure the value is overwritten in the right bit position) - this generates a new "container value"
      3) set the new container value - again this can be done with a memory access VarHandle

      We could do all the above either by only generating the accessors for the bitfields, or by generating a low-level accessor for the container, and then emitting the bitfields accessor more simply, in Java.



         


        Attachments

          Activity

            People

            Assignee:
            sundar Sundararajan Athijegannathan
            Reporter:
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: