1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   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 }