< prev index next >

src/java.base/share/classes/jdk/internal/foreign/abi/x64/windows/Windowsx64ABI.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.foreign.abi.x64.windows;
  26 
  27 import jdk.internal.foreign.ScopeImpl;
  28 import jdk.internal.foreign.Util;
  29 import jdk.internal.foreign.abi.*;
  30 import jdk.internal.foreign.memory.LayoutTypeImpl;
  31 import jdk.internal.foreign.memory.Types;
  32 
  33 import java.foreign.Library;
  34 import java.foreign.NativeMethodType;
  35 import java.foreign.NativeTypes;
  36 import java.foreign.Scope;
  37 import java.foreign.layout.Function;
  38 import java.foreign.layout.Layout;
  39 import java.foreign.layout.Value;
  40 import java.foreign.memory.LayoutType;
  41 import java.foreign.memory.Pointer;
  42 import java.foreign.memory.Struct;
  43 import java.lang.invoke.MethodHandle;
  44 import java.lang.invoke.WrongMethodTypeException;
  45 import java.util.Collection;
  46 import java.util.List;
  47 
  48 /**
  49  * ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
  50  */
  51 public class Windowsx64ABI implements SystemABI {
  52 







  53     private static Windowsx64ABI instance;
  54 
  55     public static Windowsx64ABI getInstance() {
  56         if (instance == null) {
  57             instance = new Windowsx64ABI();
  58         }
  59         return instance;
  60     }
  61 
  62     static CallingSequence arrangeCall(NativeMethodType nmt) {
  63         return arrangeCall(nmt, Integer.MAX_VALUE);
  64     }
  65 
  66     static CallingSequence arrangeCall(NativeMethodType nmt, int varArgsStart) {
  67         Function f = nmt.function();
  68         CallingSequenceBuilderImpl builder = new CallingSequenceBuilderImpl(
  69                 f.returnLayout().map(x -> new Argument(-1, x, "__retval")).orElse(null));
  70         for (int i = 0; i < f.argumentLayouts().size(); i++) {
  71             Layout type = f.argumentLayouts().get(i);
  72             builder.addArgument(type, i >= varArgsStart, "arg" + i);
  73         }
  74         if (f.isVariadic()) {
  75             builder.addArgument(Types.POINTER, false, null);
  76         }
  77 
  78         return builder.build();
  79     }
  80 
  81     @Override
  82     public MethodHandle downcallHandle(CallingConvention cc, Library.Symbol symbol, NativeMethodType nmt) {
  83         Util.checkNoArrays(nmt.methodType());
  84         if (nmt.isVarArgs()) {
  85             return VarargsInvokerImpl.make(symbol, nmt);
  86         }
  87 
  88         return UniversalNativeInvokerImpl.make(symbol, arrangeCall(nmt), nmt).getBoundMethodHandle();




  89     }
  90 
  91     @Override
  92     public Library.Symbol upcallStub(CallingConvention cc, MethodHandle target, NativeMethodType nmt) {
  93         Util.checkNoArrays(nmt.methodType());
  94         if (!target.type().equals(nmt.methodType())) {
  95             throw new WrongMethodTypeException("Native method type has wrong type: " + nmt.methodType());
  96         }
  97 
  98         return UpcallStubs.registerUpcallStub(new UniversalUpcallHandlerImpl(target, arrangeCall(nmt), nmt));
  99     }
 100 
 101     @Override
 102     public CallingConvention defaultCallingConvention() {
 103         return null;
 104     }
 105 
 106     @Override
 107     public CallingConvention namedCallingConvention(String name) throws IllegalArgumentException {
 108         return null;
 109     }
 110 
 111     @Override
 112     public Collection<CallingConvention> callingConventions() {
 113         return null;
 114     }
 115 
 116     static void unboxValue(Object o, LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> dstPtrFunc,


 117                            List<ArgumentBinding> bindings) throws Throwable {
 118         if (o instanceof Struct) {
 119             assert (bindings.size() == 1); // always for structs on windows
 120 
 121             Pointer<?> structPtr = ((Struct<?>) o).ptr();
 122             LayoutType<?> structType = structPtr.type();
 123             Pointer<Long> src = Util.unsafeCast(structPtr, NativeTypes.UINT64);
 124             ArgumentBinding binding = bindings.get(0);
 125 
 126             if (CallingSequenceBuilderImpl.isRegisterAggregate(binding.getMember().getType())) { // pass value
 127                 Pointer.copy(src, dstPtrFunc.apply(binding), structType.bytesSize());
 128             } else { // pass a pointer
 129                 /*
 130                  * Leak memory for now
 131                  */
 132                 Scope scope = Scope.globalScope().fork();
 133                 Pointer<?> copy = scope.allocate(structType);
 134                 Pointer.copy(src, copy, structType.bytesSize());
 135 
 136                 Pointer<?> dst = dstPtrFunc.apply(binding);
 137                 Util.unsafeCast(dst, NativeTypes.UINT64).type().setter().invoke(dst, copy.addr());
 138             }
 139         } else {
 140             assert bindings.size() <= 2;
 141             Pointer<?> dst = Util.unsafeCast(dstPtrFunc.apply(bindings.get(0)), type);
 142             dst.type().setter().invoke(dst, o);
 143         }
 144     }
 145 
 146     static Object boxValue(LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> srcPtrFunc,

 147                            List<ArgumentBinding> bindings) throws IllegalAccessException {
 148         assert (bindings.size() == 1); // always on windows
 149         ArgumentBinding binding = bindings.get(0);
 150         Class<?> carrier = ((LayoutTypeImpl<?>) type).carrier();
 151         if (Util.isCStruct(carrier)) {
 152 
 153             /*
 154              * Leak memory for now
 155              */
 156             Scope scope = Scope.globalScope().fork();
 157 
 158             @SuppressWarnings({"rawtypes", "unchecked"})
 159             Pointer<?> rtmp = ((ScopeImpl) scope).allocate(type, 8);
 160 
 161             if (CallingSequenceBuilderImpl.isRegisterAggregate(type.layout())) {
 162                 Pointer<Long> dst = Util.unsafeCast(rtmp, NativeTypes.UINT64).offset(binding.getOffset() / NativeTypes.UINT64.bytesSize());
 163                 Pointer.copy(srcPtrFunc.apply(binding), dst, binding.getStorage().getSize());
 164             } else {
 165                 Pointer<?> local = Util.unsafeCast(srcPtrFunc.apply(binding), type.pointer()).get();
 166                 // need defensive copy since Structs don't have value semantics on the Java side.
 167                 Pointer.copy(local, rtmp, type.bytesSize());
 168             }
 169 
 170             return rtmp.get();
 171         }
 172         return Util.unsafeCast(srcPtrFunc.apply(binding), type).get();
 173     }
 174 
 175 }


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.foreign.abi.x64.windows;
  26 
  27 import jdk.internal.foreign.ScopeImpl;
  28 import jdk.internal.foreign.Util;
  29 import jdk.internal.foreign.abi.*;
  30 import jdk.internal.foreign.memory.LayoutTypeImpl;

  31 
  32 import java.foreign.Library;
  33 import java.foreign.NativeMethodType;
  34 import java.foreign.NativeTypes;
  35 import java.foreign.Scope;
  36 import java.foreign.layout.Function;


  37 import java.foreign.memory.LayoutType;
  38 import java.foreign.memory.Pointer;
  39 import java.foreign.memory.Struct;
  40 import java.lang.invoke.MethodHandle;
  41 import java.lang.invoke.WrongMethodTypeException;
  42 import java.util.Collection;
  43 import java.util.List;
  44 
  45 /**
  46  * ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
  47  */
  48 public class Windowsx64ABI implements SystemABI {
  49 
  50     public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 4;
  51     public static final int MAX_INTEGER_RETURN_REGISTERS = 1;
  52     public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 4;
  53     public static final int MAX_VECTOR_RETURN_REGISTERS = 1;
  54     public static final int MAX_REGISTER_ARGUMENTS = 4;
  55     public static final int MAX_REGISTER_RETURNS = 1;
  56 
  57     private static Windowsx64ABI instance;
  58 
  59     public static Windowsx64ABI getInstance() {
  60         if (instance == null) {
  61             instance = new Windowsx64ABI();
  62         }
  63         return instance;
  64     }
  65 
  66     static CallingSequence arrangeCall(NativeMethodType nmt) {




  67         Function f = nmt.function();
  68         CallingSequenceBuilder builder = new CallingSequenceBuilderImpl(
  69                 f.returnLayout().orElse(null));
  70         f.argumentLayouts().forEach(builder::addArgument);







  71         return builder.build();
  72     }
  73 
  74     @Override
  75     public MethodHandle downcallHandle(CallingConvention cc, Library.Symbol symbol, NativeMethodType nmt) {
  76         Util.checkNoArrays(nmt.methodType());
  77         if (nmt.isVarArgs()) {
  78             return VarargsInvoker.make(symbol, nmt, CallingSequenceBuilderImpl::new, adapter);
  79         }
  80 
  81         try {
  82             return new UniversalNativeInvoker(symbol, arrangeCall(nmt), nmt, adapter).getBoundMethodHandle();
  83         } catch (IllegalAccessException ex) {
  84             throw new IllegalStateException(ex);
  85         }
  86     }
  87 
  88     @Override
  89     public Library.Symbol upcallStub(CallingConvention cc, MethodHandle target, NativeMethodType nmt) {
  90         Util.checkNoArrays(nmt.methodType());
  91         if (!target.type().equals(nmt.methodType())) {
  92             throw new WrongMethodTypeException("Native method type has wrong type: " + nmt.methodType());
  93         }
  94 
  95         return UpcallStubs.registerUpcallStub(new UniversalUpcallHandler(target, arrangeCall(nmt), nmt, adapter));
  96     }
  97 
  98     @Override
  99     public CallingConvention defaultCallingConvention() {
 100         return null;
 101     }
 102 
 103     @Override
 104     public CallingConvention namedCallingConvention(String name) throws IllegalArgumentException {
 105         return null;
 106     }
 107 
 108     @Override
 109     public Collection<CallingConvention> callingConventions() {
 110         return null;
 111     }
 112 
 113     UniversalAdapter adapter = new UniversalAdapter() {
 114         @Override
 115         public void unboxValue(Object o, LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> dstPtrFunc,
 116                            List<ArgumentBinding> bindings) throws Throwable {
 117             if (o instanceof Struct) {
 118                 assert (bindings.size() == 1); // always for structs on windows
 119 
 120                 Pointer<?> structPtr = ((Struct<?>) o).ptr();
 121                 LayoutType<?> structType = structPtr.type();
 122                 Pointer<Long> src = Util.unsafeCast(structPtr, NativeTypes.UINT64);
 123                 ArgumentBinding binding = bindings.get(0);
 124 
 125                 if (CallingSequenceBuilderImpl.isRegisterAggregate(binding.argument().layout())) { // pass value
 126                     Pointer.copy(src, dstPtrFunc.apply(binding), structType.bytesSize());
 127                 } else { // pass a pointer
 128                     /*
 129                      * Leak memory for now
 130                      */
 131                     Scope scope = Scope.globalScope().fork();
 132                     Pointer<?> copy = scope.allocate(structType);
 133                     Pointer.copy(src, copy, structType.bytesSize());
 134 
 135                     Pointer<?> dst = dstPtrFunc.apply(binding);
 136                     Util.unsafeCast(dst, NativeTypes.UINT64).type().setter().invoke(dst, copy.addr());
 137                 }
 138             } else {
 139                 assert bindings.size() <= 2;
 140                 Pointer<?> dst = Util.unsafeCast(dstPtrFunc.apply(bindings.get(0)), type);
 141                 dst.type().setter().invoke(dst, o);
 142             }
 143         }
 144 
 145         @Override
 146         public Object boxValue(LayoutType<?> type, java.util.function.Function<ArgumentBinding, Pointer<?>> srcPtrFunc,
 147                                List<ArgumentBinding> bindings) throws IllegalAccessException {
 148             assert (bindings.size() == 1); // always on windows
 149             ArgumentBinding binding = bindings.get(0);
 150             Class<?> carrier = ((LayoutTypeImpl<?>) type).carrier();
 151             if (Util.isCStruct(carrier)) {
 152 
 153                 /*
 154                  * Leak memory for now
 155                  */
 156                 Scope scope = Scope.globalScope().fork();
 157 
 158                 @SuppressWarnings({"rawtypes", "unchecked"})
 159                 Pointer<?> rtmp = ((ScopeImpl) scope).allocate(type, 8);
 160 
 161                 if (CallingSequenceBuilderImpl.isRegisterAggregate(type.layout())) {
 162                     Pointer<Long> dst = Util.unsafeCast(rtmp, NativeTypes.UINT64).offset(binding.offset() / NativeTypes.UINT64.bytesSize());
 163                     Pointer.copy(srcPtrFunc.apply(binding), dst, binding.storage().getSize());
 164                 } else {
 165                     Pointer<?> local = Util.unsafeCast(srcPtrFunc.apply(binding), type.pointer()).get();
 166                     // need defensive copy since Structs don't have value semantics on the Java side.
 167                     Pointer.copy(local, rtmp, type.bytesSize());
 168                 }
 169 
 170                 return rtmp.get();
 171             }
 172             return Util.unsafeCast(srcPtrFunc.apply(binding), type).get();
 173         }
 174     };
 175 }
< prev index next >