Skip to content

Commit

Permalink
Merge pull request #17352 from ChengJin01/ffi_disable_code_for_compil…
Browse files Browse the repository at this point in the history
…ation_jdk21

Disable FFI specific code for compilation in JDK21
  • Loading branch information
keithc-ca committed May 30, 2023
2 parents 6aab183 + 7bca76c commit 3e340db
Show file tree
Hide file tree
Showing 22 changed files with 295 additions and 609 deletions.
@@ -1,4 +1,4 @@
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 19]*/
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 20]*/
/*******************************************************************************
* Copyright IBM Corp. and others 2021
*
Expand Down Expand Up @@ -26,17 +26,16 @@
import java.lang.invoke.MethodType;

import java.lang.foreign.FunctionDescriptor;
/*[IF JAVA_SPEC_VERSION >= 20]*/
import jdk.internal.foreign.abi.LinkerOptions;
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
/*[IF JAVA_SPEC_VERSION == 20]*/
import openj9.internal.foreign.abi.InternalDowncallHandler;
/*[ENDIF] JAVA_SPEC_VERSION == 20 */

/**
* The counterpart in OpenJDK is replaced with this class that wrap up a method handle
* enabling the native code to the ffi_call via the libffi interface at runtime.
*/
public class DowncallLinker {
/*[IF JAVA_SPEC_VERSION >= 20]*/
/**
* The method is ultimately invoked by Linker on the specific platforms to generate the requested
* method handle to the underlying C function.
Expand All @@ -46,20 +45,12 @@ public class DowncallLinker {
* @param options The linker options indicating additional linking requirements to the linker
* @return a method handle bound to the native method
*/
@SuppressWarnings("nls")
public static MethodHandle getBoundMethodHandle(MethodType functionMethodType, FunctionDescriptor funcDesc, LinkerOptions options) {
/*[IF JAVA_SPEC_VERSION >= 21]*/
throw new InternalError("Downcall is not yet implemented");
/*[ELSE] JAVA_SPEC_VERSION >= 21 */
return new InternalDowncallHandler(functionMethodType, funcDesc, options).getBoundMethodHandle();
/*[ENDIF] JAVA_SPEC_VERSION >= 21 */
}
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
/**
* The method is ultimately invoked by Linker on the specific platforms to generate the requested
* method handle to the underlying C function.
*
* @param functionMethodType The MethodType of the specified native function
* @param funcDesc The function descriptor of the specified native function
* @return a method handle bound to the native method
*/
public static MethodHandle getBoundMethodHandle(MethodType functionMethodType, FunctionDescriptor funcDesc) {
return new InternalDowncallHandler(functionMethodType, funcDesc).getBoundMethodHandle();
}
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
}
@@ -1,4 +1,4 @@
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 19]*/
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 20]*/
/*******************************************************************************
* Copyright IBM Corp. and others 2021
*
Expand Down Expand Up @@ -27,32 +27,28 @@

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemorySegment;
/*[IF JAVA_SPEC_VERSION >= 20]*/
import java.lang.foreign.SegmentScope;
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
import java.lang.foreign.MemorySession;
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
/*[IF JAVA_SPEC_VERSION >= 21]*/
import jdk.internal.foreign.abi.AbstractLinker.UpcallStubFactory;
/*[ENDIF] JAVA_SPEC_VERSION >= 21 */
import jdk.internal.foreign.abi.LinkerOptions;
/*[ELSE] JAVA_SPEC_VERSION >= 21 */
import java.lang.foreign.SegmentScope;
import jdk.internal.foreign.abi.LinkerOptions;
import openj9.internal.foreign.abi.InternalUpcallHandler;
/*[ENDIF] JAVA_SPEC_VERSION >= 21 */

/**
* The counterpart in OpenJDK is replaced with this class that wrap up
* an upcall handler enabling the native call to the java code at runtime.
*/
public final class UpcallLinker {

/*[IF JAVA_SPEC_VERSION == 20]*/
private final long thunkAddr;

/* The constructor creates an upcall handler specific to the requested java method
* by generating a native thunk in upcall on a given platform.
*/
/*[IF JAVA_SPEC_VERSION >= 20]*/
UpcallLinker(MethodHandle target, MethodType methodType, FunctionDescriptor descriptor, SegmentScope session)
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
UpcallLinker(MethodHandle target, MethodType methodType, FunctionDescriptor descriptor, MemorySession session)
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
{
InternalUpcallHandler internalUpcallHandler = new InternalUpcallHandler(target, methodType, descriptor, session);
/* The thunk address must be set given entryPoint() is used in OpenJDK. */
Expand All @@ -68,7 +64,6 @@ public long entryPoint() {
return thunkAddr;
}

/*[IF JAVA_SPEC_VERSION >= 20]*/
/**
* The method invoked via Clinker generates a native thunk to create
* a native symbol that holds an entry point to the native function
Expand All @@ -81,24 +76,11 @@ public long entryPoint() {
* @return the native symbol
*/
public static MemorySegment make(MethodHandle target, MethodType methodType, FunctionDescriptor descriptor, SegmentScope session)
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
/**
* The method invoked via Clinker generates a native thunk to create
* a native symbol that holds an entry point to the native function
* intended for the requested java method in upcall.
*
* @param target the upcall method handle to the requested java method
* @param methodType the MethodType of the upcall method handle
* @param descriptor the FunctionDescriptor of the upcall method handle
* @param session the MemorySession of the upcall method handle
* @return the native symbol
*/
public static MemorySegment make(MethodHandle target, MethodType methodType, FunctionDescriptor descriptor, MemorySession session)
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
{
UpcallLinker upcallLinker = new UpcallLinker(target, methodType, descriptor, session);
return UpcallStubs.makeUpcall(upcallLinker.entryPoint(), session);
}
/*[ENDIF] JAVA_SPEC_VERSION == 20 */

/*[IF JAVA_SPEC_VERSION >= 21]*/
/**
Expand Down Expand Up @@ -129,12 +111,16 @@ public static UpcallStubFactory makeFactory(MethodType methodType, ABIDescriptor
*
* @param methodType the MethodType of the upcall method handle
* @param descriptor the FunctionDescriptor of the upcall method handle
* @param options the LinkerOptions indicating additional linking requirements to the linker
* @return a factory instance that wraps up the upcall specific code
* @throws InternalError as the upcalll specific code is not yet implemented
*/
public static UpcallStubFactory makeFactory(MethodType methodType, FunctionDescriptor descriptor) {
return (target, session) -> {
return UpcallLinker.make(target, methodType, descriptor, session);
};
@SuppressWarnings("nls")
public static UpcallStubFactory makeFactory(MethodType methodType, FunctionDescriptor descriptor, LinkerOptions options) {
// return (target, arena) -> {
// return UpcallLinker.make(target, methodType, descriptor, arena, options);
// };
throw new InternalError("Upcall is not yet implemented");
}
/*[ENDIF] JAVA_SPEC_VERSION >= 21 */
}

0 comments on commit 3e340db

Please sign in to comment.