< prev index next >

src/java.base/share/classes/jdk/internal/foreign/abi/VarargsInvoker.java

Print this page




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

  28 import java.foreign.memory.LayoutType;
  29 import java.foreign.memory.Pointer;
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.invoke.MethodType;


  33 import jdk.internal.foreign.Util;

  34 
  35 public abstract class VarargsInvoker {
  36 
  37     protected static final MethodHandle INVOKE_MH;
  38     protected final NativeMethodType nativeMethodType;
  39     protected final Library.Symbol symbol;


  40 
  41     protected VarargsInvoker(Library.Symbol symbol, NativeMethodType nativeMethodType) {

  42         this.symbol = symbol;
  43         this.nativeMethodType = nativeMethodType;


  44     }
  45 
  46     static {
  47         try {
  48             INVOKE_MH = MethodHandles.lookup().findVirtual(VarargsInvoker.class, "invoke", MethodType.methodType(Object.class, Object[].class));
  49         } catch (ReflectiveOperationException e) {
  50             throw new RuntimeException(e);
  51         }
  52     }
  53 








  54     private Object invoke(Object[] args) throws Throwable {
  55         // one trailing Object[]
  56         int nNamedArgs = nativeMethodType.parameterArray().length;
  57         assert(args.length == nNamedArgs + 1);
  58         // The last argument is the array of vararg collector
  59         Object[] unnamedArgs = (Object[]) args[args.length - 1];
  60 





  61         LayoutType<?> retLayoutType = nativeMethodType.returnType();
  62         LayoutType<?>[] argLayoutTypes = new LayoutType<?>[nNamedArgs + unnamedArgs.length];
  63         System.arraycopy(nativeMethodType.parameterArray(), 0, argLayoutTypes, 0, nNamedArgs);



  64         int pos = nNamedArgs;
  65         for (Object o: unnamedArgs) {
  66             Class<?> type = o.getClass();
  67             argLayoutTypes[pos++] = Util.makeType(computeClass(type), Util.variadicLayout(type));


  68         }
  69         MethodHandle delegate = specialize(NativeMethodType.of(retLayoutType, argLayoutTypes));





  70 
  71         // flatten argument list so that it can be passed to an asSpreader MH
  72         Object[] allArgs = new Object[nNamedArgs + unnamedArgs.length];
  73         System.arraycopy(args, 0, allArgs, 0, nNamedArgs);
  74         System.arraycopy(unnamedArgs, 0, allArgs, nNamedArgs, unnamedArgs.length);
  75 
  76         return delegate.invokeWithArguments(allArgs);
  77     }
  78 
  79     protected abstract MethodHandle specialize(NativeMethodType nmt);
  80 
  81     private Class<?> computeClass(Class<?> c) {
  82         if (c.isPrimitive()) {
  83             throw new IllegalArgumentException("Not expecting primitive type " + c.getName());
  84         }
  85 
  86         if (c == Byte.class || c == Short.class || c == Character.class || c == Integer.class || c == Long.class) {
  87             return long.class;
  88         } else if (c == Float.class || c == Double.class) {
  89             return double.class;
  90         } else if (Pointer.class.isAssignableFrom(c)) {
  91             return Pointer.class;
  92         } else {
  93             throw new UnsupportedOperationException("Type unhandled: " + c.getName());



















  94         }
  95     }
  96 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.internal.foreign.abi;
  25 
  26 import java.foreign.Library;
  27 import java.foreign.NativeMethodType;
  28 import java.foreign.layout.Layout;
  29 import java.foreign.memory.LayoutType;
  30 import java.foreign.memory.Pointer;
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.invoke.MethodType;
  34 import java.util.function.Function;
  35 
  36 import jdk.internal.foreign.Util;
  37 import jdk.internal.foreign.memory.Types;
  38 
  39 public class VarargsInvoker {
  40 
  41     private static final MethodHandle INVOKE_MH;
  42     private final NativeMethodType nativeMethodType;
  43     private final Library.Symbol symbol;
  44     private final Function<Layout, CallingSequenceBuilder> seqBuilderFactory;
  45     private final UniversalAdapter adapter;
  46 
  47     private VarargsInvoker(Library.Symbol symbol, NativeMethodType nativeMethodType,
  48                            Function<Layout, CallingSequenceBuilder> seqBuilderFactory, UniversalAdapter adapter) {
  49         this.symbol = symbol;
  50         this.nativeMethodType = nativeMethodType;
  51         this.seqBuilderFactory = seqBuilderFactory;
  52         this.adapter = adapter;
  53     }
  54 
  55     static {
  56         try {
  57             INVOKE_MH = MethodHandles.lookup().findVirtual(VarargsInvoker.class, "invoke", MethodType.methodType(Object.class, Object[].class));
  58         } catch (ReflectiveOperationException e) {
  59             throw new RuntimeException(e);
  60         }
  61     }
  62 
  63     public static MethodHandle make(Library.Symbol symbol, NativeMethodType nativeMethodType,
  64                                     Function<Layout, CallingSequenceBuilder> seqBuilder, UniversalAdapter adapter) {
  65         VarargsInvoker invoker = new VarargsInvoker(symbol, nativeMethodType, seqBuilder, adapter);
  66         MethodType methodType = nativeMethodType.methodType();
  67         return INVOKE_MH.bindTo(invoker).asCollector(Object[].class, methodType.parameterCount())
  68                 .asType(methodType);
  69     }
  70 
  71     private Object invoke(Object[] args) throws Throwable {
  72         // one trailing Object[]
  73         int nNamedArgs = nativeMethodType.parameterArray().length;
  74         assert(args.length == nNamedArgs + 1);
  75         // The last argument is the array of vararg collector
  76         Object[] unnamedArgs = (Object[]) args[args.length - 1];
  77 
  78         CallingSequenceBuilder seqBuilder = seqBuilderFactory.apply(
  79                 nativeMethodType.function()
  80                         .returnLayout()
  81                         .orElse(null));
  82 
  83         LayoutType<?> retLayoutType = nativeMethodType.returnType();
  84         LayoutType<?>[] argLayoutTypes = new LayoutType<?>[nNamedArgs + unnamedArgs.length];
  85         System.arraycopy(nativeMethodType.parameterArray(), 0, argLayoutTypes, 0, nNamedArgs);
  86 
  87         nativeMethodType.function().argumentLayouts().forEach(seqBuilder::addArgument);
  88 
  89         int pos = nNamedArgs;
  90         for (Object o: unnamedArgs) {
  91             Class<?> type = o.getClass();
  92             Layout layout = variadicLayout(type);
  93             argLayoutTypes[pos++] = Util.makeType(computeClass(type), layout);
  94             seqBuilder.addArgument(layout, true);
  95         }
  96 
  97         //build universal invoker used to dispatch the call
  98         UniversalNativeInvoker delegate = new UniversalNativeInvoker(symbol,
  99                 seqBuilder.build(),
 100                 NativeMethodType.of(retLayoutType, argLayoutTypes),
 101                 adapter);
 102 
 103         // flatten argument list so that it can be passed to an asSpreader MH
 104         Object[] allArgs = new Object[nNamedArgs + unnamedArgs.length];
 105         System.arraycopy(args, 0, allArgs, 0, nNamedArgs);
 106         System.arraycopy(unnamedArgs, 0, allArgs, nNamedArgs, unnamedArgs.length);
 107 
 108         return delegate.invoke(allArgs);
 109     }
 110 


 111     private Class<?> computeClass(Class<?> c) {
 112         if (c.isPrimitive()) {
 113             throw new IllegalArgumentException("Not expecting primitive type " + c.getName());
 114         }
 115 
 116         if (c == Byte.class || c == Short.class || c == Character.class || c == Integer.class || c == Long.class) {
 117             return long.class;
 118         } else if (c == Float.class || c == Double.class) {
 119             return double.class;
 120         } else if (Pointer.class.isAssignableFrom(c)) {
 121             return Pointer.class;
 122         } else {
 123             throw new UnsupportedOperationException("Type unhandled: " + c.getName());
 124         }
 125     }
 126 
 127     private Layout variadicLayout(Class<?> c) {
 128         c = (Class<?>)Util.unboxIfNeeded(c);
 129         if (c == char.class || c == byte.class || c == short.class || c == int.class || c == long.class) {
 130             //it is ok to approximate with a machine word here; numerics arguments in a prototype-less
 131             //function call are always rounded up to a register size anyway.
 132             return Types.INT64;
 133         } else if (c == float.class || c == double.class) {
 134             return Types.DOUBLE;
 135         } else if (Pointer.class.isAssignableFrom(c)) {
 136             return Types.POINTER;
 137         } else if (Util.isCallback(c)) {
 138             return Types.POINTER;
 139         } else if (Util.isCStruct(c)) {
 140             return Util.layoutof(c);
 141         } else {
 142             throw new IllegalArgumentException("Unhandled variadic argument class: " + c);
 143         }
 144     }
 145 }
< prev index next >