Skip to content

Commit

Permalink
Replace reinterpret with withTargetLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 5, 2024
1 parent 3f9d43d commit 4adca0d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions demo/src/main/java/overrun/marshal/test/CDowncallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ interface CDowncallTest {
void testCriticalFalse();

@Entrypoint("testReturnSizedArr")
@SizedSeg(4 * Integer.BYTES)
MemorySegment ntestReturnSizedArr();

@Overload("ntestReturnSizedArr")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package overrun.marshal.test;

import overrun.marshal.SizedSeg;
import overrun.marshal.Upcall;

import java.lang.foreign.Arena;
Expand All @@ -34,8 +35,8 @@ public interface GLFWErrorCallback extends Upcall {
void invoke(int error, String description);

@Stub
default void invoke(int error, MemorySegment description) {
invoke(error, description.reinterpret(Long.MAX_VALUE).getString(0));
default void invoke(int error, @SizedSeg(Long.MAX_VALUE) MemorySegment description) {
invoke(error, description.getString(0));
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/overrun/marshal/Downcall.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* Methods without {@link Overload @Overload} marked
* converts parameters of {@link String} and array types into {@link java.lang.foreign.MemorySegment MemorySegment}.
* <h3>Parameter Annotations</h3>
* See {@link Sized @Sized} and {@link Ref @Ref}.
* See {@link Sized @Sized}, {@link SizedSeg @SizedSeg} and {@link Ref @Ref}.
* <h2>Example</h2>
* <pre>{@code
* @Downcall(libname = "libGL.so", name = "GL")
Expand All @@ -59,6 +59,7 @@
* @see Entrypoint
* @see Overload
* @see Sized
* @see SizedSeg
* @see Ref
* @since 0.1.0
*/
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/overrun/marshal/DowncallProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,7 @@ private void writeFile(
.map(p -> Spec.literal(p.getSimpleName().toString()))
.collect(Collectors.toList()));
if (notVoid) {
final Spec castSpec = Spec.cast(javaReturnType, invokeExact);
if (eIsStruct) {
targetStatement.addStatement(Spec.returnStatement(new InvokeSpec(Spec.parentheses(castSpec), "reinterpret")
.addArgument(new InvokeSpec(Spec.accessSpec(eStructRef.value(), "LAYOUT"), "byteSize"))));
} else if (eSizedSeg != null || eSized != null) {
targetStatement.addStatement(Spec.returnStatement(new InvokeSpec(Spec.parentheses(castSpec), "reinterpret")
.addArgument(getConstExp(eSizedSeg != null ? eSizedSeg.value() : eSized.value()))));
} else {
targetStatement.addStatement(Spec.returnStatement(castSpec));
}
targetStatement.addStatement(Spec.returnStatement(Spec.cast(javaReturnType, invokeExact)));
} else {
targetStatement.addStatement(Spec.statement(invokeExact));
}
Expand Down Expand Up @@ -447,12 +438,6 @@ private void writeFile(
.addArgument(finalInvocation);
} else if (isArray(returnType)) {
final TypeMirror arrayComponentType = getArrayComponentType(returnType);
if (eSized != null) {
finalInvocation = new InvokeSpec(finalInvocation, "reinterpret")
.addArgument(Spec.operatorSpec("*",
Spec.literal(getConstExp(eSized.value())),
new InvokeSpec(toValueLayoutStr(arrayComponentType), "byteSize")));
}
if (isBooleanArray(returnType)) {
finalInvocation = new InvokeSpec(BoolHelper.class, "toArray")
.addArgument(finalInvocation);
Expand Down Expand Up @@ -620,7 +605,27 @@ private void addMethodHandles(TypeElement type, List<ExecutableElement> methods,
.addArgument(new InvokeSpec(FunctionDescriptor.class,
returnType.getKind() == TypeKind.VOID ? "ofVoid" : "of").also(invokeSpec -> {
if (returnType.getKind() != TypeKind.VOID) {
invokeSpec.addArgument(toValueLayoutStr(returnType));
final StructRef structRef = v.getAnnotation(StructRef.class);
final boolean isStruct = structRef != null;
final String valueLayoutStr = isStruct ? "ValueLayout.ADDRESS" : toValueLayoutStr(returnType);
final SizedSeg sizedSeg = v.getAnnotation(SizedSeg.class);
final Sized sized = v.getAnnotation(Sized.class);
if (isStruct) {
invokeSpec.addArgument(new InvokeSpec(valueLayoutStr, "withTargetLayout")
.addArgument(Spec.accessSpec(structRef.value(), "LAYOUT")));
} else if (sizedSeg != null && isMemorySegment(returnType)) {
invokeSpec.addArgument(new InvokeSpec(valueLayoutStr, "withTargetLayout")
.addArgument(new InvokeSpec(MemoryLayout.class, "sequenceLayout")
.addArgument(getConstExp(sizedSeg.value()))
.addArgument(Spec.accessSpec(ValueLayout.class, "JAVA_BYTE"))));
} else if (sized != null && isArray(returnType)) {
invokeSpec.addArgument(new InvokeSpec(valueLayoutStr, "withTargetLayout")
.addArgument(new InvokeSpec(MemoryLayout.class, "sequenceLayout")
.addArgument(getConstExp(sized.value()))
.addArgument(Spec.accessSpec(ValueLayout.class, toValueLayoutStr(getArrayComponentType(returnType))))));
} else {
invokeSpec.addArgument(valueLayoutStr);
}
}
v.getParameters().forEach(e -> invokeSpec.addArgument(toValueLayoutStr(e.asType())));
})).also(invokeSpec -> {
Expand Down

0 comments on commit 4adca0d

Please sign in to comment.