RFR: 7903649: Field and global variables of array type should have indexed accessors

Jorn Vernee jvernee at openjdk.org
Mon Jan 29 23:00:40 UTC 2024


On Mon, 29 Jan 2024 22:39:01 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> This PR adds support for indexed accessors for struct fields and global variables whose type is an array type. These accessors feature a number of `long` access coordinates which has the same cardinality as that of the underlying array type.
>> 
>> For instance, consider the following global variable declaration:
>> 
>> 
>> int ints[2][3][4];
>> 
>> 
>> For this, jextract now emits:
>> 
>> 
>>     private static SequenceLayout ints$LAYOUT() {
>>         class Holder {
>>             static final SequenceLayout LAYOUT = MemoryLayout.sequenceLayout(2, MemoryLayout.sequenceLayout(3, MemoryLayout.sequenceLayout(4, foo_h.C_INT)));
>>         }
>>         return Holder.LAYOUT;
>>     }
>> 
>>     private static MemorySegment ints$SEGMENT() {
>>         class Holder {
>>             static final MemorySegment SEGMENT = foo_h.findOrThrow("ints")
>>                 .reinterpret(ints$LAYOUT().byteSize());
>>         }
>>         return Holder.SEGMENT;
>>     }
>> 
>>     /**
>>      * Getter for variable:
>>      * {@snippet lang=c :
>>      * int ints[2][3][4]
>>      * }
>>      */
>>     public static MemorySegment ints() {
>>         return ints$SEGMENT();
>>     }
>> 
>>     /**
>>      * Setter for variable:
>>      * {@snippet lang=c :
>>      * int ints[2][3][4]
>>      * }
>>      */
>>     public static void ints(MemorySegment varValue) {
>>         MemorySegment.copy(varValue, 0L, ints$SEGMENT(), 0L, ints$LAYOUT().byteSize());
>>     }
>> 
>>     private static VarHandle ints$ELEM_HANDLE() {
>>         class Holder {
>>             static final VarHandle HANDLE = ints$LAYOUT().varHandle(sequenceElement(), sequenceElement(), sequenceElement());
>>         }
>>         return Holder.HANDLE;
>>     }
>> 
>>     /**
>>      * Indexed getter for variable:
>>      * {@snippet lang=c :
>>      * int ints[2][3][4]
>>      * }
>>      */
>>     public static int ints(long index0, long index1, long index2) {
>>         return (int)ints$ELEM_HANDLE().get(ints$SEGMENT(), 0L, index0, index1, index2);
>>     }
>> 
>>     /**
>>      * Indexed setter for variable:
>>      * {@snippet lang=c :
>>      * int ints[2][3][4]
>>      * }
>>      */
>>     public static void ints(long index0, long index1, long index2, int varValue) {
>>         ints$ELEM_HANDLE().set(ints$SEGMENT(), 0L, index0, index1, index2, varValue);
>>     }
>> 
>> 
>> If the array element type is a struct, different code needs to be generated. Consider this global variable declaration:
>> 
>> 
>> struct Point { int x; int y; } points[2][3][4];
>> 
>> 
>> This generates the followi...
>
> test/jtreg/generator/arrayAccess/TestArrayAccess.java line 48:
> 
>> 46:         try (Arena arena = Arena.ofConfined()) {
>> 47:             MemorySegment foo = Foo.allocate(arena);
>> 48:             for (int i = 0 ; i < 2 ; i++) {
> 
> So, the array length is hard-coded here. What would it take to derive the length from the bindings?
> 
> I think in this case it would be:
> 
> 
> long count = Foo.layout().select(groupElement("ints1")).elementCount();
> 
> 
> ?

P.S. I guess what I'm implying here is: maybe we need a way to make getting the length of an array field easier as well

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

PR Review Comment: https://git.openjdk.org/jextract/pull/198#discussion_r1470336221


More information about the jextract-dev mailing list