< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/x64/sysv/SysVx64Linker.java

Print this page
*** 28,10 ***
--- 28,11 ---
  import jdk.incubator.foreign.ForeignLinker;
  import jdk.incubator.foreign.FunctionDescriptor;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
+ import jdk.internal.foreign.abi.SharedUtils;
  import jdk.internal.foreign.abi.UpcallStubs;
  
  import java.lang.invoke.MethodHandle;
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;

*** 58,13 ***
      private static final MethodHandle MH_boxVaList;
  
      static {
          try {
              MethodHandles.Lookup lookup = MethodHandles.lookup();
!             MH_unboxVaList = lookup.findStatic(SysVx64Linker.class, "unboxVaList",
!                 MethodType.methodType(MemoryAddress.class, CSupport.VaList.class));
!             MH_boxVaList = lookup.findStatic(SysVx64Linker.class, "boxVaList",
                  MethodType.methodType(VaList.class, MemoryAddress.class));
          } catch (ReflectiveOperationException e) {
              throw new ExceptionInInitializerError(e);
          }
      }
--- 59,13 ---
      private static final MethodHandle MH_boxVaList;
  
      static {
          try {
              MethodHandles.Lookup lookup = MethodHandles.lookup();
!             MH_unboxVaList = lookup.findVirtual(CSupport.VaList.class, "address",
!                 MethodType.methodType(MemoryAddress.class));
!             MH_boxVaList = lookup.findStatic(SysVx64Linker.class, "newVaListOfAddress",
                  MethodType.methodType(VaList.class, MemoryAddress.class));
          } catch (ReflectiveOperationException e) {
              throw new ExceptionInInitializerError(e);
          }
      }

*** 80,49 ***
          SysVVaList.Builder builder = SysVVaList.builder();
          actions.accept(builder);
          return builder.build();
      }
  
-     private static MethodType convertVaListCarriers(MethodType mt) {
-         Class<?>[] params = new Class<?>[mt.parameterCount()];
-         for (int i = 0; i < params.length; i++) {
-             Class<?> pType = mt.parameterType(i);
-             params[i] = ((pType == CSupport.VaList.class) ? SysVVaList.CARRIER : pType);
-         }
-         return MethodType.methodType(mt.returnType(), params);
-     }
- 
-     private static MethodHandle unxboxVaLists(MethodType type, MethodHandle handle) {
-         for (int i = 0; i < type.parameterCount(); i++) {
-             if (type.parameterType(i) == VaList.class) {
-                handle = MethodHandles.filterArguments(handle, i, MH_unboxVaList);
-             }
-         }
-         return handle;
-     }
- 
      @Override
      public MethodHandle downcallHandle(MemoryAddress symbol, MethodType type, FunctionDescriptor function) {
!         MethodType llMt = convertVaListCarriers(type);
          MethodHandle handle = CallArranger.arrangeDowncall(symbol, llMt, function);
!         handle = unxboxVaLists(type, handle);
-         return handle;
-     }
- 
-     private static MethodHandle boxVaLists(MethodHandle handle) {
-         MethodType type = handle.type();
-         for (int i = 0; i < type.parameterCount(); i++) {
-             if (type.parameterType(i) == VaList.class) {
-                handle = MethodHandles.filterArguments(handle, i, MH_boxVaList);
-             }
-         }
          return handle;
      }
  
      @Override
      public MemorySegment upcallStub(MethodHandle target, FunctionDescriptor function) {
!         target = boxVaLists(target);
          return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function));
      }
  
      @Override
      public String name() {
--- 81,21 ---
          SysVVaList.Builder builder = SysVVaList.builder();
          actions.accept(builder);
          return builder.build();
      }
  
      @Override
      public MethodHandle downcallHandle(MemoryAddress symbol, MethodType type, FunctionDescriptor function) {
!         MethodType llMt = SharedUtils.convertVaListCarriers(type, SysVVaList.CARRIER);
          MethodHandle handle = CallArranger.arrangeDowncall(symbol, llMt, function);
!         handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
          return handle;
      }
  
      @Override
      public MemorySegment upcallStub(MethodHandle target, FunctionDescriptor function) {
!         target = SharedUtils.boxVaLists(target, MH_boxVaList);
          return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function));
      }
  
      @Override
      public String name() {

*** 141,17 ***
              case POINTER -> ArgumentClassImpl.POINTER;
              default -> null;
          });
      }
  
!     private static MemoryAddress unboxVaList(CSupport.VaList list) {
-         return ((SysVVaList) list).getSegment().baseAddress();
-     }
- 
-     private static CSupport.VaList boxVaList(MemoryAddress ma) {
          return SysVVaList.ofAddress(ma);
      }
  
!     public static VaList newVaListOfAddress(MemoryAddress ma) {
!         return SysVVaList.ofAddress(ma);
      }
  }
--- 114,13 ---
              case POINTER -> ArgumentClassImpl.POINTER;
              default -> null;
          });
      }
  
!     public static VaList newVaListOfAddress(MemoryAddress ma) {
          return SysVVaList.ofAddress(ma);
      }
  
!     public static VaList emptyVaList() {
!         return SysVVaList.empty();
      }
  }
< prev index next >