< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/ProgrammableUpcallHandler.java

Print this page
*** 24,16 ***
   */
  
  package jdk.internal.foreign.abi;
  
  import jdk.incubator.foreign.MemoryAddress;
- import jdk.incubator.foreign.MemoryLayouts;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
! import jdk.incubator.foreign.SegmentAllocator;
- import jdk.internal.access.JavaLangInvokeAccess;
- import jdk.internal.access.SharedSecrets;
  import jdk.internal.foreign.MemoryAddressImpl;
  import sun.security.action.GetPropertyAction;
  
  import java.lang.invoke.MethodHandle;
  import java.lang.invoke.MethodHandles;
--- 24,14 ---
   */
  
  package jdk.internal.foreign.abi;
  
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemorySegment;
+ import jdk.incubator.foreign.NativeSymbol;
  import jdk.incubator.foreign.ResourceScope;
! import jdk.incubator.foreign.ValueLayout;
  import jdk.internal.foreign.MemoryAddressImpl;
  import sun.security.action.GetPropertyAction;
  
  import java.lang.invoke.MethodHandle;
  import java.lang.invoke.MethodHandles;

*** 44,10 ***
--- 42,11 ---
  import java.util.Map;
  import java.util.Objects;
  import java.util.stream.Stream;
  
  import static java.lang.invoke.MethodHandles.dropArguments;
+ import static java.lang.invoke.MethodHandles.exactInvoker;
  import static java.lang.invoke.MethodHandles.filterReturnValue;
  import static java.lang.invoke.MethodHandles.identity;
  import static java.lang.invoke.MethodHandles.insertArguments;
  import static java.lang.invoke.MethodHandles.lookup;
  import static java.lang.invoke.MethodType.methodType;

*** 65,13 ***
      private static final boolean USE_SPEC = Boolean.parseBoolean(
          GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.ProgrammableUpcallHandler.USE_SPEC", "true"));
      private static final boolean USE_INTRINSICS = Boolean.parseBoolean(
          GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.ProgrammableUpcallHandler.USE_INTRINSICS", "true"));
  
!     private static final JavaLangInvokeAccess JLI = SharedSecrets.getJavaLangInvokeAccess();
- 
-     private static final VarHandle VH_LONG = MemoryLayouts.JAVA_LONG.varHandle(long.class);
  
      private static final MethodHandle MH_invokeMoves;
      private static final MethodHandle MH_invokeInterpBindings;
  
      static {
--- 64,11 ---
      private static final boolean USE_SPEC = Boolean.parseBoolean(
          GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.ProgrammableUpcallHandler.USE_SPEC", "true"));
      private static final boolean USE_INTRINSICS = Boolean.parseBoolean(
          GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.ProgrammableUpcallHandler.USE_INTRINSICS", "true"));
  
!     private static final VarHandle VH_LONG = ValueLayout.JAVA_LONG.varHandle();
  
      private static final MethodHandle MH_invokeMoves;
      private static final MethodHandle MH_invokeInterpBindings;
  
      static {

*** 86,11 ***
          } catch (ReflectiveOperationException e) {
              throw new InternalError(e);
          }
      }
  
!     public static UpcallHandler make(ABIDescriptor abi, MethodHandle target, CallingSequence callingSequence) {
          Binding.VMLoad[] argMoves = argMoveBindings(callingSequence);
          Binding.VMStore[] retMoves = retMoveBindings(callingSequence);
  
          boolean isSimple = !(retMoves.length > 1);
  
--- 83,11 ---
          } catch (ReflectiveOperationException e) {
              throw new InternalError(e);
          }
      }
  
!     public static NativeSymbol make(ABIDescriptor abi, MethodHandle target, CallingSequence callingSequence, ResourceScope scope) {
          Binding.VMLoad[] argMoves = argMoveBindings(callingSequence);
          Binding.VMStore[] retMoves = retMoveBindings(callingSequence);
  
          boolean isSimple = !(retMoves.length > 1);
  

*** 116,27 ***
              doBindings = doBindings.asCollector(Object[].class, llType.parameterCount());
              doBindings = doBindings.asType(llType);
          }
  
          long entryPoint;
!         boolean usesStackArgs = argMoveBindingsStream(callingSequence)
-                 .map(Binding.VMLoad::storage)
-                 .anyMatch(s -> abi.arch.isStackType(s.type()));
-         if (USE_INTRINSICS && isSimple && !usesStackArgs && supportsOptimizedUpcalls()) {
              checkPrimitive(doBindings.type());
!             JLI.ensureCustomized(doBindings);
              VMStorage[] args = Arrays.stream(argMoves).map(Binding.Move::storage).toArray(VMStorage[]::new);
              VMStorage[] rets = Arrays.stream(retMoves).map(Binding.Move::storage).toArray(VMStorage[]::new);
              CallRegs conv = new CallRegs(args, rets);
              entryPoint = allocateOptimizedUpcallStub(doBindings, abi, conv);
          } else {
              BufferLayout layout = BufferLayout.of(abi);
              MethodHandle doBindingsErased = doBindings.asSpreader(Object[].class, doBindings.type().parameterCount());
              MethodHandle invokeMoves = insertArguments(MH_invokeMoves, 1, doBindingsErased, argMoves, retMoves, abi, layout);
              entryPoint = allocateUpcallStub(invokeMoves, abi, layout);
          }
!         return () -> entryPoint;
      }
  
      private static void checkPrimitive(MethodType type) {
          if (!type.returnType().isPrimitive()
                  || type.parameterList().stream().anyMatch(p -> !p.isPrimitive()))
--- 113,24 ---
              doBindings = doBindings.asCollector(Object[].class, llType.parameterCount());
              doBindings = doBindings.asType(llType);
          }
  
          long entryPoint;
!         if (USE_INTRINSICS && isSimple && supportsOptimizedUpcalls()) {
              checkPrimitive(doBindings.type());
!             doBindings = insertArguments(exactInvoker(doBindings.type()), 0, doBindings);
              VMStorage[] args = Arrays.stream(argMoves).map(Binding.Move::storage).toArray(VMStorage[]::new);
              VMStorage[] rets = Arrays.stream(retMoves).map(Binding.Move::storage).toArray(VMStorage[]::new);
              CallRegs conv = new CallRegs(args, rets);
              entryPoint = allocateOptimizedUpcallStub(doBindings, abi, conv);
          } else {
              BufferLayout layout = BufferLayout.of(abi);
              MethodHandle doBindingsErased = doBindings.asSpreader(Object[].class, doBindings.type().parameterCount());
              MethodHandle invokeMoves = insertArguments(MH_invokeMoves, 1, doBindingsErased, argMoves, retMoves, abi, layout);
              entryPoint = allocateUpcallStub(invokeMoves, abi, layout);
          }
!         return UpcallStubs.makeUpcall(entryPoint, scope);
      }
  
      private static void checkPrimitive(MethodType type) {
          if (!type.returnType().isPrimitive()
                  || type.parameterList().stream().anyMatch(p -> !p.isPrimitive()))
< prev index next >