use of jdk.incubator.foreign.ValueLayout in project openj9 by eclipse.
the class LayoutStrPreprocessor method preprocessLayout.
/* Preprocess the layout to generate a concise layout string with all kind symbols
* extracted from the layout to simplify parsing the layout string in native.
* e.g. a struct layout string with nested struct is as follows: (Only for Java <= 17)
* [
* [
* b32(elem1)[abi/kind=INT,layout/name=elem1]
* b32(elem2)[abi/kind=INT,layout/name=elem2]
* ](Struct1_II)[layout/name=Struct1_II]
* [
* b32(elem1)[abi/kind=INT,layout/name=elem1]
* b32(elem2)[abi/kind=INT,layout/name=elem2]
* ](Struct2_II)[layout/name=Struct2_II]
* ](nested_struct)[layout/name=nested_struct]
*
* which ends up with "16#2[#2[II]#2[II]]" by conversion as follows:
*
* 16#2[ (16 is the byte size of the layout and 2 is the count of the struct elements
* #2[ 2 is the count of the int elements
* I (INT)
* I (INT)
* ]
* #2[ 2 is the count of the int elements
* I (INT)
* I (INT)
* ]
* ]
* where "#" denotes the start of struct.
*
* Note:
* 1) the prefix "ByteSize#CountOfElmemnt" and "#CountOfElmemnt" are not required in
* the upcall given the converted layout stirngs are further parsed for the generated
* thunk in native, which is logically different from downcall.
* 2) the parsing of primitives in layouts in Java 18 is based on the MemoryLayout.carrier()
* rather than the CLinker.TypeKind which is entirely removed in OpenJDK.
*/
private static StringBuilder preprocessLayout(MemoryLayout targetLayout, boolean isDownCall) {
// $NON-NLS-1$
StringBuilder targetLayoutString = new StringBuilder("");
/* Directly obtain the kind symbol of the primitive layout */
if (targetLayout instanceof ValueLayout) {
targetLayoutString.append(getPrimitiveTypeSymbol((ValueLayout) targetLayout));
} else if (targetLayout instanceof SequenceLayout) {
// Intended for nested arrays
SequenceLayout arrayLayout = (SequenceLayout) targetLayout;
MemoryLayout elementLayout = arrayLayout.elementLayout();
long elementCount = arrayLayout.elementCount().getAsLong();
targetLayoutString.append(elementCount).append(':').append(preprocessLayout(elementLayout, isDownCall));
} else if (targetLayout instanceof GroupLayout) {
// Intended for the nested structs
GroupLayout structLayout = (GroupLayout) targetLayout;
List<MemoryLayout> elementLayoutList = structLayout.memberLayouts();
int structElementCount = elementLayoutList.size();
// $NON-NLS-1$
StringBuilder elementLayoutStrs = new StringBuilder("");
int paddingElements = 0;
for (int elemIndex = 0; elemIndex < structElementCount; elemIndex++) {
MemoryLayout structElement = elementLayoutList.get(elemIndex);
/* Ignore any padding element in the struct as it is handled by ffi_call by default */
if (structElement.isPadding()) {
paddingElements += 1;
/* The padding bytes is required in native during the upcall */
if (!isDownCall) {
elementLayoutStrs.append('(').append(structElement.byteSize()).append(')');
}
} else {
elementLayoutStrs.append(preprocessLayout(structElement, isDownCall));
}
}
/* Prefix "#" to denote the start of this layout string in the case of downcall */
if (isDownCall) {
targetLayoutString.append('#').append(structElementCount - paddingElements);
}
targetLayoutString.append('[').append(elementLayoutStrs).append(']');
}
return targetLayoutString;
}
Aggregations