< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/aarch64/macos/MacOsAArch64Linker.java

Print this page
*** 23,82 ***
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package jdk.internal.foreign.abi.aarch64.macos;
  
! import jdk.incubator.foreign.Addressable;
  import jdk.incubator.foreign.FunctionDescriptor;
  import jdk.incubator.foreign.MemoryAddress;
- import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
! import jdk.internal.foreign.AbstractCLinker;
- import jdk.internal.foreign.ResourceScopeImpl;
  import jdk.internal.foreign.abi.SharedUtils;
- import jdk.internal.foreign.abi.UpcallStubs;
  import jdk.internal.foreign.abi.aarch64.CallArranger;
  
  import java.lang.invoke.MethodHandle;
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.Objects;
  import java.util.function.Consumer;
  
- import static jdk.internal.foreign.PlatformLayouts.*;
- 
  /**
   * ABI implementation for macOS on Apple silicon. Based on AAPCS with
   * changes to va_list and passing arguments on the stack.
   */
! public final class MacOsAArch64Linker extends AbstractCLinker {
      private static MacOsAArch64Linker instance;
  
      static final long ADDRESS_SIZE = 64; // bits
  
-     private static final MethodHandle MH_unboxVaList;
-     private static final MethodHandle MH_boxVaList;
- 
-     static {
-         try {
-             MethodHandles.Lookup lookup = MethodHandles.lookup();
-             MH_unboxVaList = lookup.findVirtual(VaList.class, "address",
-                 MethodType.methodType(MemoryAddress.class));
-             MH_boxVaList = MethodHandles.insertArguments(lookup.findStatic(MacOsAArch64Linker.class, "newVaListOfAddress",
-                 MethodType.methodType(VaList.class, MemoryAddress.class, ResourceScope.class)), 1, ResourceScope.globalScope());
-         } catch (ReflectiveOperationException e) {
-             throw new ExceptionInInitializerError(e);
-         }
-     }
- 
      public static MacOsAArch64Linker getInstance() {
          if (instance == null) {
              instance = new MacOsAArch64Linker();
          }
          return instance;
      }
  
      @Override
!     public final MethodHandle downcallHandle(MethodType type, FunctionDescriptor function) {
-         Objects.requireNonNull(type);
          Objects.requireNonNull(function);
!         MethodType llMt = SharedUtils.convertVaListCarriers(type, MacOsAArch64VaList.CARRIER);
!         MethodHandle handle = CallArranger.arrangeDowncall(llMt, function);
          if (!type.returnType().equals(MemorySegment.class)) {
              // not returning segment, just insert a throwing allocator
              handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
          }
!         handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
-         return handle;
      }
  
      @Override
!     public final MemoryAddress upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
          Objects.requireNonNull(scope);
          Objects.requireNonNull(target);
          Objects.requireNonNull(function);
!         target = SharedUtils.boxVaLists(target, MH_boxVaList);
!         return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function), (ResourceScopeImpl) scope);
      }
  
      public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
          MacOsAArch64VaList.Builder builder = MacOsAArch64VaList.builder(scope);
          actions.accept(builder);
--- 23,64 ---
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package jdk.internal.foreign.abi.aarch64.macos;
  
! import jdk.incubator.foreign.CLinker;
  import jdk.incubator.foreign.FunctionDescriptor;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemorySegment;
+ import jdk.incubator.foreign.NativeSymbol;
  import jdk.incubator.foreign.ResourceScope;
! import jdk.incubator.foreign.VaList;
  import jdk.internal.foreign.abi.SharedUtils;
  import jdk.internal.foreign.abi.aarch64.CallArranger;
  
  import java.lang.invoke.MethodHandle;
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.Objects;
  import java.util.function.Consumer;
  
  /**
   * ABI implementation for macOS on Apple silicon. Based on AAPCS with
   * changes to va_list and passing arguments on the stack.
   */
! public final class MacOsAArch64Linker implements CLinker {
      private static MacOsAArch64Linker instance;
  
      static final long ADDRESS_SIZE = 64; // bits
  
      public static MacOsAArch64Linker getInstance() {
          if (instance == null) {
              instance = new MacOsAArch64Linker();
          }
          return instance;
      }
  
      @Override
!     public final MethodHandle downcallHandle(FunctionDescriptor function) {
          Objects.requireNonNull(function);
!         MethodType type = SharedUtils.inferMethodType(function, false);
!         MethodHandle handle = CallArranger.MACOS.arrangeDowncall(type, function);
          if (!type.returnType().equals(MemorySegment.class)) {
              // not returning segment, just insert a throwing allocator
              handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
          }
!         return SharedUtils.wrapDowncall(handle, function);
      }
  
      @Override
!     public final NativeSymbol upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
          Objects.requireNonNull(scope);
          Objects.requireNonNull(target);
          Objects.requireNonNull(function);
!         MethodType type = SharedUtils.inferMethodType(function, true);
!         if (!type.equals(target.type())) {
+             throw new IllegalArgumentException("Wrong method handle type: " + target.type());
+         }
+         return CallArranger.MACOS.arrangeUpcall(target, target.type(), function, scope);
      }
  
      public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
          MacOsAArch64VaList.Builder builder = MacOsAArch64VaList.builder(scope);
          actions.accept(builder);
< prev index next >