From 60a47cb071f48a44e95c3b4a6defcf477738db0d Mon Sep 17 00:00:00 2001 From: Jorn Vernee Date: Thu, 22 Sep 2022 22:25:48 +0000 Subject: [PATCH] 8292047: Consider ways to add linkage parameters to downcall handles Reviewed-by: mcimadamore --- doc/panama_ffi.md | 3 +- .../java/lang/foreign/FunctionDescriptor.java | 21 +---- .../classes/java/lang/foreign/Linker.java | 48 +++++++--- .../java/lang/foreign/SegmentAllocator.java | 2 +- .../java/lang/foreign/SymbolLookup.java | 2 +- .../classes/java/lang/foreign/VaList.java | 2 +- .../java/lang/foreign/package-info.java | 4 +- .../foreign/FunctionDescriptorImpl.java | 87 ++----------------- .../internal/foreign/abi/AbstractLinker.java | 16 ++-- .../internal/foreign/abi/LinkerOptions.java | 80 +++++++++++++++++ .../jdk/internal/foreign/abi/SharedUtils.java | 5 -- .../foreign/abi/aarch64/CallArranger.java | 11 ++- .../abi/aarch64/linux/LinuxAArch64Linker.java | 5 +- .../abi/aarch64/macos/MacOsAArch64Linker.java | 5 +- .../foreign/abi/x64/sysv/SysVx64Linker.java | 3 +- .../foreign/abi/x64/windows/CallArranger.java | 11 ++- .../abi/x64/windows/Windowsx64Linker.java | 5 +- test/jdk/java/foreign/NativeTestHelper.java | 1 - test/jdk/java/foreign/StdLibTest.java | 4 +- .../java/foreign/TestFunctionDescriptor.java | 4 - test/jdk/java/foreign/TestIntrinsics.java | 6 +- test/jdk/java/foreign/TestLinker.java | 50 +++++++++++ test/jdk/java/foreign/TestVarArgs.java | 7 +- .../callarranger/TestAarch64CallArranger.java | 11 ++- .../callarranger/TestWindowsCallArranger.java | 7 +- test/jdk/java/foreign/valist/VaListTest.java | 4 - .../java/lang/foreign/PointerInvoke.java | 2 - .../bench/java/lang/foreign/VaList.java | 3 +- 28 files changed, 239 insertions(+), 170 deletions(-) create mode 100644 src/java.base/share/classes/jdk/internal/foreign/abi/LinkerOptions.java create mode 100644 test/jdk/java/foreign/TestLinker.java diff --git a/doc/panama_ffi.md b/doc/panama_ffi.md index 1bae70a817e..731d715fe06 100644 --- a/doc/panama_ffi.md +++ b/doc/panama_ffi.md @@ -416,7 +416,8 @@ public class Examples { public static void printf() throws Throwable { MethodHandle printf = LINKER.downcallHandle( STDLIB.lookup("printf").get(), - FunctionDescriptor.of(JAVA_INT, ADDRESS).asVariadic(JAVA_INT, JAVA_INT, JAVA_INT) + FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT), + Linker.Option.firstVariadicArg(1) // first int is variadic ); try (MemorySession session = MemorySession.openConfined()) { MemorySegment s = session.allocateUtf8String("%d plus %d equals %d\n"); diff --git a/src/java.base/share/classes/java/lang/foreign/FunctionDescriptor.java b/src/java.base/share/classes/java/lang/foreign/FunctionDescriptor.java index e631a42fc0c..ddb4491be13 100644 --- a/src/java.base/share/classes/java/lang/foreign/FunctionDescriptor.java +++ b/src/java.base/share/classes/java/lang/foreign/FunctionDescriptor.java @@ -36,7 +36,7 @@ /** * A function descriptor is made up of zero or more argument layouts and zero or one return layout. A function descriptor * is used to model the signature of foreign functions when creating - * {@linkplain Linker#downcallHandle(MemorySegment, FunctionDescriptor) downcall method handles} or + * {@linkplain Linker#downcallHandle(MemorySegment, FunctionDescriptor, Linker.Option...) downcall method handles} or * {@linkplain Linker#upcallStub(MethodHandle, FunctionDescriptor, MemorySession) upcall stubs}. * * @implSpec @@ -58,13 +58,6 @@ public sealed interface FunctionDescriptor permits FunctionDescriptorImpl { */ List argumentLayouts(); - /** - * The index of the first variadic argument layout (where defined). - * @return The index of the first variadic argument layout, or {@code -1} if this is not a - * {@linkplain #asVariadic(MemoryLayout...) variadic} layout. - */ - int firstVariadicArgumentIndex(); - /** * Returns a function descriptor with the given argument layouts appended to the argument layout array * of this function descriptor. @@ -113,17 +106,6 @@ public sealed interface FunctionDescriptor permits FunctionDescriptorImpl { */ MethodType toMethodType(); - /** - * Creates a specialized variadic function descriptor, by appending given variadic layouts to this - * function descriptor argument layouts. The resulting function descriptor can report the position - * of the {@linkplain #firstVariadicArgumentIndex() first variadic argument}, and cannot be altered - * in any way: for instance, calling {@link #changeReturnLayout(MemoryLayout)} on the resulting descriptor - * will throw an {@link UnsupportedOperationException}. - * @param variadicLayouts the variadic argument layouts to be appended to this descriptor argument layouts. - * @return a variadic function descriptor, or this descriptor if {@code variadicLayouts.length == 0}. - */ - FunctionDescriptor asVariadic(MemoryLayout... variadicLayouts); - /** * Creates a function descriptor with the given return and argument layouts. * @param resLayout the return layout. @@ -145,5 +127,4 @@ static FunctionDescriptor ofVoid(MemoryLayout... argLayouts) { // Null checks are implicit in List.of(argLayouts) return FunctionDescriptorImpl.ofVoid(List.of(argLayouts)); } - } diff --git a/src/java.base/share/classes/java/lang/foreign/Linker.java b/src/java.base/share/classes/java/lang/foreign/Linker.java index b572cb7b0fd..6d347554124 100644 --- a/src/java.base/share/classes/java/lang/foreign/Linker.java +++ b/src/java.base/share/classes/java/lang/foreign/Linker.java @@ -26,6 +26,7 @@ package java.lang.foreign; import jdk.internal.foreign.abi.AbstractLinker; +import jdk.internal.foreign.abi.LinkerOptions; import jdk.internal.foreign.abi.SharedUtils; import jdk.internal.javac.PreviewFeature; import jdk.internal.reflect.CallerSensitive; @@ -46,7 +47,7 @@ * in the JVM and foreign functions in the library. In particular: * @@ -61,7 +62,7 @@ * *

Downcall method handles

* - * {@linkplain #downcallHandle(FunctionDescriptor) Linking a foreign function} is a process which requires a function descriptor, + * {@linkplain #downcallHandle(FunctionDescriptor, Option...) Linking a foreign function} is a process which requires a function descriptor, * a set of memory layouts which, together, specify the signature of the foreign function to be linked, and returns, * when complete, a downcall method handle, that is, a method handle that can be used to invoke the target foreign function. *

@@ -77,7 +78,7 @@ * The downcall method handle type, derived as above, might be decorated by additional leading parameters, * in the given order if both are present: *