jdk/src/share/classes/java/lang/invoke/MethodHandleImpl.java
author vlivanov
Sat, 01 Mar 2014 02:07:30 +0400
changeset 23039 6ee70bc18809
parent 23038 bb8b3b23af3a
child 23059 4aca6147df88
permissions -rw-r--r--
8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke Reviewed-by: twisti, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang.invoke;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    28
import java.security.AccessController;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    29
import java.security.PrivilegedAction;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    30
import java.util.Arrays;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    31
import java.util.HashMap;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    32
import sun.invoke.empty.Empty;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    33
import sun.invoke.util.ValueConversions;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    34
import sun.invoke.util.VerifyType;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    35
import sun.invoke.util.Wrapper;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    36
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    37
import sun.reflect.Reflection;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    38
import static java.lang.invoke.LambdaForm.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    39
import static java.lang.invoke.MethodHandleStatics.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    40
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    41
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    43
 * Trusted implementation code for MethodHandle.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    45
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    46
/*non-public*/ abstract class MethodHandleImpl {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
    /// Factory methods to create method handles:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    49
    static void initStatics() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    50
        // Trigger selected static initializations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    51
        MemberName.Factory.INSTANCE.getClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    52
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    53
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    54
    static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    55
        if (!arrayClass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    56
            throw newIllegalArgumentException("not an array: "+arrayClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    57
        MethodHandle accessor = ArrayAccessor.getAccessor(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    58
        MethodType srcType = accessor.type().erase();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    59
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    60
        Name[] names = arguments(1, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    61
        Name[] args  = Arrays.copyOfRange(names, 1, 1 + srcType.parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    62
        names[names.length - 1] = new Name(accessor.asType(srcType), (Object[]) args);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    63
        LambdaForm form = new LambdaForm("getElement", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
    64
        MethodHandle mh = SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    65
        if (ArrayAccessor.needCast(arrayClass)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    66
            mh = mh.bindTo(arrayClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    67
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    68
        mh = mh.asType(ArrayAccessor.correctType(arrayClass, isSetter));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    69
        return mh;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    70
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    71
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    72
    static final class ArrayAccessor {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    73
        /// Support for array element access
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    74
        static final HashMap<Class<?>, MethodHandle> GETTER_CACHE = new HashMap<>();  // TODO use it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    75
        static final HashMap<Class<?>, MethodHandle> SETTER_CACHE = new HashMap<>();  // TODO use it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    76
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    77
        static int     getElementI(int[]     a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    78
        static long    getElementJ(long[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    79
        static float   getElementF(float[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    80
        static double  getElementD(double[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    81
        static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    82
        static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    83
        static short   getElementS(short[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    84
        static char    getElementC(char[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    85
        static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    86
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    87
        static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    88
        static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    89
        static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    90
        static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    91
        static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    92
        static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    93
        static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    94
        static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    95
        static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    96
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    97
        static Object  getElementL(Class<?> arrayClass, Object[] a, int i)           { arrayClass.cast(a); return a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    98
        static void    setElementL(Class<?> arrayClass, Object[] a, int i, Object x) { arrayClass.cast(a); a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    99
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   100
        // Weakly typed wrappers of Object[] accessors:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   101
        static Object  getElementL(Object    a, int i)            { return getElementL((Object[])a, i); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   102
        static void    setElementL(Object    a, int i, Object  x) {        setElementL((Object[]) a, i, x); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   103
        static Object  getElementL(Object   arrayClass, Object a, int i)             { return getElementL((Class<?>) arrayClass, (Object[])a, i); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   104
        static void    setElementL(Object   arrayClass, Object a, int i, Object x)   {        setElementL((Class<?>) arrayClass, (Object[])a, i, x); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   105
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   106
        static boolean needCast(Class<?> arrayClass) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   107
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   108
            return !elemClass.isPrimitive() && elemClass != Object.class;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   109
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   110
        static String name(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   111
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   112
            if (elemClass == null)  throw new IllegalArgumentException();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   113
            return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   114
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   115
        static final boolean USE_WEAKLY_TYPED_ARRAY_ACCESSORS = false;  // FIXME: decide
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   116
        static MethodType type(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   117
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   118
            Class<?> arrayArgClass = arrayClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   119
            if (!elemClass.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   120
                arrayArgClass = Object[].class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   121
                if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   122
                    arrayArgClass = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   123
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   124
            if (!needCast(arrayClass)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   125
                return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   126
                    MethodType.methodType(elemClass,  arrayArgClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   127
                    MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   128
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   129
                Class<?> classArgClass = Class.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   130
                if (USE_WEAKLY_TYPED_ARRAY_ACCESSORS)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   131
                    classArgClass = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   132
                return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   133
                    MethodType.methodType(Object.class, classArgClass, arrayArgClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   134
                    MethodType.methodType(void.class,   classArgClass, arrayArgClass, int.class, Object.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   135
            }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   136
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   137
        static MethodType correctType(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   138
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   139
            return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   140
                    MethodType.methodType(elemClass,  arrayClass, int.class) :
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   141
                    MethodType.methodType(void.class, arrayClass, int.class, elemClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   142
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   143
        static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   144
            String     name = name(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   145
            MethodType type = type(arrayClass, isSetter);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   146
            try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   147
                return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   148
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   149
                throw uncaughtException(ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   150
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   151
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   152
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   153
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   154
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   155
     * Create a JVM-level adapter method handle to conform the given method
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   156
     * handle to the similar newType, using only pairwise argument conversions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   157
     * For each argument, convert incoming argument to the exact type needed.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   158
     * The argument conversions allowed are casting, boxing and unboxing,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   159
     * integral widening or narrowing, and floating point widening or narrowing.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   160
     * @param srcType required call type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   161
     * @param target original method handle
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   162
     * @param level which strength of conversion is allowed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   163
     * @return an adapter to the original handle with the desired new type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   164
     *          or the original target if the types are already identical
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   165
     *          or null if the adaptation cannot be made
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   166
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   167
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, int level) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   168
        assert(level >= 0 && level <= 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   169
        MethodType dstType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   170
        assert(dstType.parameterCount() == target.type().parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   171
        if (srcType == dstType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   172
            return target;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   173
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   174
        // Calculate extra arguments (temporaries) required in the names array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   175
        // FIXME: Use an ArrayList<Name>.  Some arguments require more than one conversion step.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   176
        final int INARG_COUNT = srcType.parameterCount();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   177
        int conversions = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   178
        boolean[] needConv = new boolean[1+INARG_COUNT];
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   179
        for (int i = 0; i <= INARG_COUNT; i++) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   180
            Class<?> src = (i == INARG_COUNT) ? dstType.returnType() : srcType.parameterType(i);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   181
            Class<?> dst = (i == INARG_COUNT) ? srcType.returnType() : dstType.parameterType(i);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   182
            if (!VerifyType.isNullConversion(src, dst) ||
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   183
                level <= 1 && dst.isInterface() && !dst.isAssignableFrom(src)) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   184
                needConv[i] = true;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   185
                conversions++;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   186
            }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   187
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   188
        boolean retConv = needConv[INARG_COUNT];
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   189
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   190
        final int IN_MH         = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   191
        final int INARG_BASE    = 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   192
        final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   193
        final int NAME_LIMIT    = INARG_LIMIT + conversions + 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   194
        final int RETURN_CONV   = (!retConv ? -1         : NAME_LIMIT - 1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   195
        final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   196
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   197
        // Now build a LambdaForm.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   198
        MethodType lambdaType = srcType.basicType().invokerType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   199
        Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   200
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   201
        // Collect the arguments to the outgoing call, maybe with conversions:
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   202
        final int OUTARG_BASE = 0;  // target MH is Name.function, name Name.arguments[0]
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   203
        Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   204
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   205
        int nameCursor = INARG_LIMIT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   206
        for (int i = 0; i < INARG_COUNT; i++) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   207
            Class<?> src = srcType.parameterType(i);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   208
            Class<?> dst = dstType.parameterType(i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   209
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   210
            if (!needConv[i]) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   211
                // do nothing: difference is trivial
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   212
                outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   213
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   214
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   215
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   216
            // Tricky case analysis follows.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   217
            MethodHandle fn = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   218
            if (src.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   219
                if (dst.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   220
                    fn = ValueConversions.convertPrimitive(src, dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   221
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   222
                    Wrapper w = Wrapper.forPrimitiveType(src);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   223
                    MethodHandle boxMethod = ValueConversions.box(w);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   224
                    if (dst == w.wrapperType())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   225
                        fn = boxMethod;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   226
                    else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   227
                        fn = boxMethod.asType(MethodType.methodType(dst, src));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   228
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   229
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   230
                if (dst.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   231
                    // Caller has boxed a primitive.  Unbox it for the target.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   232
                    Wrapper w = Wrapper.forPrimitiveType(dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   233
                    if (level == 0 || VerifyType.isNullConversion(src, w.wrapperType())) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   234
                        fn = ValueConversions.unbox(dst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   235
                    } else if (src == Object.class || !Wrapper.isWrapperType(src)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   236
                        // Examples:  Object->int, Number->int, Comparable->int; Byte->int, Character->int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   237
                        // must include additional conversions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   238
                        // src must be examined at runtime, to detect Byte, Character, etc.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   239
                        MethodHandle unboxMethod = (level == 1
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   240
                                                    ? ValueConversions.unbox(dst)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   241
                                                    : ValueConversions.unboxCast(dst));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   242
                        fn = unboxMethod;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   243
                    } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   244
                        // Example: Byte->int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   245
                        // Do this by reformulating the problem to Byte->byte.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   246
                        Class<?> srcPrim = Wrapper.forWrapperType(src).primitiveType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   247
                        MethodHandle unbox = ValueConversions.unbox(srcPrim);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   248
                        // Compose the two conversions.  FIXME:  should make two Names for this job
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   249
                        fn = unbox.asType(MethodType.methodType(dst, src));
9780
6fc3b49cfee4 7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents: 9752
diff changeset
   250
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   251
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   252
                    // Simple reference conversion.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   253
                    // Note:  Do not check for a class hierarchy relation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   254
                    // between src and dst.  In all cases a 'null' argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   255
                    // will pass the cast conversion.
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   256
                    fn = ValueConversions.cast(dst, Lazy.MH_castReference);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   257
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   258
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   259
            Name conv = new Name(fn, names[INARG_BASE + i]);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   260
            assert(names[nameCursor] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   261
            names[nameCursor++] = conv;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   262
            assert(outArgs[OUTARG_BASE + i] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   263
            outArgs[OUTARG_BASE + i] = conv;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   264
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   265
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   266
        // Build argument array for the call.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   267
        assert(nameCursor == OUT_CALL);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   268
        names[OUT_CALL] = new Name(target, outArgs);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   269
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   270
        if (RETURN_CONV < 0) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   271
            assert(OUT_CALL == names.length-1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   272
        } else {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   273
            Class<?> needReturn = srcType.returnType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   274
            Class<?> haveReturn = dstType.returnType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   275
            MethodHandle fn;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   276
            Object[] arg = { names[OUT_CALL] };
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   277
            if (haveReturn == void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   278
                // synthesize a zero value for the given void
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   279
                Object zero = Wrapper.forBasicType(needReturn).zero();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   280
                fn = MethodHandles.constant(needReturn, zero);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   281
                arg = new Object[0];  // don't pass names[OUT_CALL] to conversion
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   282
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   283
                MethodHandle identity = MethodHandles.identity(needReturn);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   284
                MethodType needConversion = identity.type().changeParameterType(0, haveReturn);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   285
                fn = makePairwiseConvert(identity, needConversion, level);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   286
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   287
            assert(names[RETURN_CONV] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   288
            names[RETURN_CONV] = new Name(fn, arg);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   289
            assert(RETURN_CONV == names.length-1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   290
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   291
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   292
        LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   293
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   294
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   295
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   296
    /**
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   297
     * Identity function, with reference cast.
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   298
     * @param t an arbitrary reference type
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   299
     * @param x an arbitrary reference value
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   300
     * @return the same value x
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   301
     */
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   302
    @ForceInline
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   303
    @SuppressWarnings("unchecked")
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   304
    static <T,U> T castReference(Class<? extends T> t, U x) {
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   305
        // inlined Class.cast because we can't ForceInline it
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   306
        if (x != null && !t.isInstance(x))
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   307
            throw newClassCastException(t, x);
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   308
        return (T) x;
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   309
    }
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   310
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   311
    private static ClassCastException newClassCastException(Class<?> t, Object obj) {
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   312
        return new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + t.getName());
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   313
    }
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   314
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   315
    static MethodHandle makeReferenceIdentity(Class<?> refType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   316
        MethodType lambdaType = MethodType.genericMethodType(1).invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   317
        Name[] names = arguments(1, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   318
        names[names.length - 1] = new Name(ValueConversions.identity(), names[1]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   319
        LambdaForm form = new LambdaForm("identity", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   320
        return SimpleMethodHandle.make(MethodType.methodType(refType, refType), form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   321
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   322
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   323
    static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   324
        MethodType type = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   325
        int last = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   326
        if (type.parameterType(last) != arrayType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   327
            target = target.asType(type.changeParameterType(last, arrayType));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   328
        target = target.asFixedArity();  // make sure this attribute is turned off
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   329
        return new AsVarargsCollector(target, target.type(), arrayType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   330
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   331
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   332
    static class AsVarargsCollector extends MethodHandle {
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   333
        private final MethodHandle target;
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   334
        private final Class<?> arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   335
        private /*@Stable*/ MethodHandle asCollectorCache;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   336
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   337
        AsVarargsCollector(MethodHandle target, MethodType type, Class<?> arrayType) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   338
            super(type, reinvokerForm(target));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   339
            this.target = target;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   340
            this.arrayType = arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   341
            this.asCollectorCache = target.asCollector(arrayType, 0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   342
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   343
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   344
        @Override MethodHandle reinvokerTarget() { return target; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   345
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   346
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   347
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   348
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   349
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   350
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   351
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   352
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   353
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   354
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   355
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   356
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   357
        public MethodHandle asTypeUncached(MethodType newType) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   358
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   359
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   360
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   361
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   362
                type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   363
                // if arity and trailing parameter are compatible, do normal thing
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   364
                return asTypeCache = asFixedArity().asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   365
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   366
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   367
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   368
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   369
                return asTypeCache = acc.asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   370
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   371
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   372
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   373
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   374
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   375
                assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   376
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   377
                throw new WrongMethodTypeException("cannot build collector", ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   378
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   379
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   380
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   381
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   382
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   383
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   384
        MethodHandle setVarargs(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   385
            if (member.isVarargs())  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   386
            return asFixedArity();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   387
        }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   388
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   389
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   390
        MethodHandle viewAsType(MethodType newType) {
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   391
            if (newType.lastParameterType() != type().lastParameterType())
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   392
                throw new InternalError();
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   393
            MethodHandle newTarget = asFixedArity().viewAsType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   394
            // put back the varargs bit:
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   395
            return new AsVarargsCollector(newTarget, newType, arrayType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   396
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   397
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   398
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   399
        MemberName internalMemberName() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   400
            return asFixedArity().internalMemberName();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   401
        }
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   402
        @Override
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   403
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   404
            return asFixedArity().internalCallerClass();
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   405
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   406
16030
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   407
        /*non-public*/
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   408
        @Override
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   409
        boolean isInvokeSpecial() {
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   410
            return asFixedArity().isInvokeSpecial();
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   411
        }
265a0d86c9ff 7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents: 14852
diff changeset
   412
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   413
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   414
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   415
        MethodHandle bindArgument(int pos, char basicType, Object value) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   416
            return asFixedArity().bindArgument(pos, basicType, value);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   417
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   418
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   419
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   420
        MethodHandle bindReceiver(Object receiver) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   421
            return asFixedArity().bindReceiver(receiver);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   422
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   424
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   425
        MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   426
            return asFixedArity().dropArguments(srcType, pos, drops);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   427
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   428
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   429
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   430
        MethodHandle permuteArguments(MethodType newType, int[] reorder) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   431
            return asFixedArity().permuteArguments(newType, reorder);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   432
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   433
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   434
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   435
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   436
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   437
                                            Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   438
        MethodType targetType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   439
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   440
        for (int i = 0; i < spreadArgCount; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   441
            Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   442
            if (arg == null)  arg = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   443
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   444
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   445
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   446
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   447
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   448
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   449
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   450
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   451
        Name[] names = arguments(spreadArgCount + 2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   452
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   453
        int[] indexes = new int[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   454
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   455
        for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   456
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   457
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   458
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   459
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   460
                Name array = names[argIndex];
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   461
                names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   462
                for (int j = 0; j < spreadArgCount; i++, j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   463
                    indexes[i] = nameCursor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   464
                    names[nameCursor++] = new Name(aload, array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   465
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   466
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   467
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   468
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   469
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   470
        assert(nameCursor == names.length-1);  // leave room for the final call
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   471
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   472
        // Build argument array for the call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   473
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   474
        for (int i = 0; i < targetType.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   475
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   476
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   477
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   478
        names[names.length - 1] = new Name(target, (Object[]) targetArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   479
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   480
        LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   481
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   482
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   483
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   484
    static void checkSpreadArgument(Object av, int n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   485
        if (av == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   486
            if (n == 0)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   487
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   488
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   489
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   490
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   491
            int len = java.lang.reflect.Array.getLength(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   492
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   493
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   494
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   495
        throw newIllegalArgumentException("array is not of length "+n);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   496
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   497
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   498
    /**
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   499
     * Pre-initialized NamedFunctions for bootstrapping purposes.
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   500
     * Factored in an inner class to delay initialization until first usage.
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   501
     */
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   502
    private static class Lazy {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   503
        private static final Class<?> MHI = MethodHandleImpl.class;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   504
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   505
        static final NamedFunction NF_checkSpreadArgument;
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   506
        static final NamedFunction NF_guardWithCatch;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   507
        static final NamedFunction NF_selectAlternative;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   508
        static final NamedFunction NF_throwException;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   509
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   510
        static final MethodHandle MH_castReference;
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   511
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   512
        static {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   513
            try {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   514
                NF_checkSpreadArgument = new NamedFunction(MHI.getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   515
                NF_guardWithCatch      = new NamedFunction(MHI.getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   516
                                                                                 MethodHandle.class, Object[].class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   517
                NF_selectAlternative   = new NamedFunction(MHI.getDeclaredMethod("selectAlternative", boolean.class, MethodHandle.class,
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   518
                                                                                 MethodHandle.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   519
                NF_throwException      = new NamedFunction(MHI.getDeclaredMethod("throwException", Throwable.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   520
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   521
                NF_checkSpreadArgument.resolve();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   522
                NF_guardWithCatch.resolve();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   523
                NF_selectAlternative.resolve();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   524
                NF_throwException.resolve();
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   525
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   526
                MethodType mt = MethodType.methodType(Object.class, Class.class, Object.class);
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   527
                MH_castReference = IMPL_LOOKUP.findStatic(MHI, "castReference", mt);
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   528
            } catch (ReflectiveOperationException ex) {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   529
                throw newInternalError(ex);
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   530
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   531
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   532
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   533
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   534
    /** Factory method:  Collect or filter selected argument(s). */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   535
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   536
                MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   537
        MethodType targetType = target.type();          // (a..., c, [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   538
        MethodType collectorType = collector.type();    // (b...)=>c
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   539
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   540
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
        int collectValCount = (collectValType == void.class ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   542
        MethodType srcType = targetType                 // (a..., [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   543
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   544
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   545
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   546
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   547
        // in  arglist: [0: ...keep1 | cpos: collect...  | cpos+cacount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   548
        // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   549
        // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   550
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   551
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   552
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   553
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   554
        final int collectNamePos = names.length - 2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   555
        final int targetNamePos  = names.length - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   556
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   557
        Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   558
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   559
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   560
        // Build argument array for the target.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   561
        // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   562
        // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   563
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   564
        int inputArgPos  = 1;  // incoming LF args to copy to target
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   565
        int targetArgPos = 0;  // fill pointer for targetArgs
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   566
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   567
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   568
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   569
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   570
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   571
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   572
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   573
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   574
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   575
            System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   576
            targetArgPos += chunk;   // optionally pass on the collected chunk
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   577
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   578
        inputArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   579
        chunk = targetArgs.length - targetArgPos;  // all the rest
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   580
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   581
        assert(inputArgPos + chunk == collectNamePos);  // use of rest of input args also
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   582
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   583
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   584
        LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   585
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   586
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   587
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   588
    @LambdaForm.Hidden
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   589
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   590
    MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   591
        return testResult ? target : fallback;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   592
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   593
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   594
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   595
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   596
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   597
                                   MethodHandle fallback) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   598
        MethodType basicType = target.type().basicType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   599
        MethodHandle invokeBasic = MethodHandles.basicInvoker(basicType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   600
        int arity = basicType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   601
        int extraNames = 3;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   602
        MethodType lambdaType = basicType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   603
        Name[] names = arguments(extraNames, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   604
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   605
        Object[] testArgs   = Arrays.copyOfRange(names, 1, 1 + arity, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   606
        Object[] targetArgs = Arrays.copyOfRange(names, 0, 1 + arity, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   607
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   608
        // call test
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   609
        names[arity + 1] = new Name(test, testArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   610
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   611
        // call selectAlternative
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   612
        Object[] selectArgs = { names[arity + 1], target, fallback };
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   613
        names[arity + 2] = new Name(Lazy.NF_selectAlternative, selectArgs);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   614
        targetArgs[0] = names[arity + 2];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   615
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   616
        // call target or fallback
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   617
        names[arity + 3] = new Name(new NamedFunction(invokeBasic), targetArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   618
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   619
        LambdaForm form = new LambdaForm("guard", lambdaType.parameterCount(), names);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   620
        return SimpleMethodHandle.make(target.type(), form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   621
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   622
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   623
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   624
     * The LambaForm shape for catchException combinator is the following:
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   625
     * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   626
     *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   627
     *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   628
     *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   629
     *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   630
     *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   631
     *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   632
     *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   633
     *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   634
     *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   635
     * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   636
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   637
     * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   638
     * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   639
     * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   640
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   641
     * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   642
     * among catchException combinators with the same basic type.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   643
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   644
    private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   645
        MethodType lambdaType = basicType.invokerType();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   646
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   647
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   648
        if (lform != null) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   649
            return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   650
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   651
        final int THIS_MH      = 0;  // the BMH_LLLLL
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   652
        final int ARG_BASE     = 1;  // start of incoming arguments
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   653
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   654
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   655
        int nameCursor = ARG_LIMIT;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   656
        final int GET_TARGET       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   657
        final int GET_CLASS        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   658
        final int GET_CATCHER      = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   659
        final int GET_COLLECT_ARGS = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   660
        final int GET_UNBOX_RESULT = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   661
        final int BOXED_ARGS       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   662
        final int TRY_CATCH        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   663
        final int UNBOX_RESULT     = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   664
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   665
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   666
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   667
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   668
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   669
        names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   670
        names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   671
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   672
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   673
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   674
        // FIXME: rework argument boxing/result unboxing logic for LF interpretation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   675
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   676
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   677
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   678
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   679
        Object[] args = new Object[invokeBasic.type().parameterCount()];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   680
        args[0] = names[GET_COLLECT_ARGS];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   681
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   682
        names[BOXED_ARGS] = new Name(new NamedFunction(invokeBasic), args);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   683
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   684
        // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   685
        Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   686
        names[TRY_CATCH] = new Name(Lazy.NF_guardWithCatch, gwcArgs);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   687
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   688
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   689
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   690
        Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   691
        names[UNBOX_RESULT] = new Name(new NamedFunction(invokeBasicUnbox), unboxArgs);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   692
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   693
        lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   694
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   695
        basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   696
        return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   697
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   698
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   699
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   700
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   701
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   702
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   703
        MethodType type = target.type();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   704
        LambdaForm form = makeGuardWithCatchForm(type.basicType());
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   705
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   706
        // Prepare auxiliary method handles used during LambdaForm interpreation.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   707
        // Box arguments and wrap them into Object[]: ValueConversions.array().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   708
        MethodType varargsType = type.changeReturnType(Object[].class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   709
        MethodHandle collectArgs = ValueConversions.varargsArray(type.parameterCount())
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   710
                                                   .asType(varargsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   711
        // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   712
        MethodHandle unboxResult;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   713
        if (type.returnType().isPrimitive()) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   714
            unboxResult = ValueConversions.unbox(type.returnType());
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   715
        } else {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   716
            unboxResult = ValueConversions.identity();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   717
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   718
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   719
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   720
        BoundMethodHandle mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   721
        try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   722
            mh = (BoundMethodHandle)
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   723
                    data.constructor[0].invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   724
                                                    (Object) collectArgs, (Object) unboxResult);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   725
        } catch (Throwable ex) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   726
            throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   727
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   728
        assert(mh.type() == type);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   729
        return mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   730
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   731
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   732
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   733
     * Intrinsified during LambdaForm compilation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   734
     * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   735
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   736
    @LambdaForm.Hidden
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   737
    static Object guardWithCatch(MethodHandle target, Class exType, MethodHandle catcher,
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   738
                                 Object... av) throws Throwable {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   739
        try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   740
            return target.invokeWithArguments(av);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   741
        } catch (Throwable t) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   742
            if (!exType.isInstance(t)) throw t;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   743
            Object[] args = prepend(t, av);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   744
            return catcher.invokeWithArguments(args);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   745
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   746
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   747
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   748
    /** Prepend an element {@code elem} to an {@code array}. */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   749
    private static Object[] prepend(Object elem, Object[] array) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   750
        Object[] newArray = new Object[array.length+1];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   751
        newArray[0] = elem;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   752
        System.arraycopy(array, 0, newArray, 1, array.length);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   753
        return newArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   754
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   755
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   756
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   757
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   758
        assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   759
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   760
        if (arity > 1) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   761
            return throwException(type.dropParameterTypes(1, arity)).dropArguments(type, 1, arity-1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   762
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   763
        return makePairwiseConvert(Lazy.NF_throwException.resolvedHandle(), type, 2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   764
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   765
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   766
    static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   767
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   768
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   769
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   770
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   771
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   772
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   773
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   774
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   775
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   776
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   777
        MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   778
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   779
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   780
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   781
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   782
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   783
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   784
            throw new InternalError(method.toString());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   785
        mh = mh.withInternalMemberName(method);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   786
        mh = mh.asVarargsCollector(Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   787
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   788
        FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   789
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   790
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   791
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   792
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   793
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   794
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   795
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   796
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   797
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   798
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   799
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   800
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   801
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   802
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   803
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   804
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   805
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   806
    // That way we can lazily load the code and set up the constants.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   807
    private static class BindCaller {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   808
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   809
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   810
            // Do not use this function to inject calls into system classes.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   811
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   812
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   813
                       hostClass.isPrimitive() ||
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   814
                       hostClass.getName().startsWith("java.") ||
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   815
                       hostClass.getName().startsWith("sun."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   816
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   817
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   818
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   819
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   820
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   821
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   822
            return restoreToType(bccInvoker.bindTo(vamh), mh.type(), mh.internalMemberName(), hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   823
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   824
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   825
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   826
            Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   827
            if (hostClass.getClassLoader() != bcc.getClassLoader())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   828
                throw new InternalError(hostClass.getName()+" (CL)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   829
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   830
                if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   831
                    throw new InternalError(hostClass.getName()+" (PD)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   832
            } catch (SecurityException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   833
                // Self-check was blocked by security manager.  This is OK.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   834
                // In fact the whole try body could be turned into an assertion.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   835
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   836
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   837
                MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   838
                init.invokeExact();  // force initialization of the class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   839
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   840
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   841
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   842
            MethodHandle bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   843
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   844
                MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   845
                bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   846
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   847
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   848
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   849
            // Test the invoker, to ensure that it really injects into the right place.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   850
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   851
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   852
                Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   853
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   854
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   855
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   856
            return bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   857
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   858
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   859
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   860
                return makeInjectedInvoker(hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   861
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   862
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   863
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   864
        // Adapt mh so that it can be called directly from an injected invoker:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   865
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   866
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   867
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   868
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   869
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   870
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   871
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   872
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   873
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   874
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   875
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   876
        // Undo the adapter effect of prepareForInvoker:
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   877
        private static MethodHandle restoreToType(MethodHandle vamh, MethodType type,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   878
                                                  MemberName member,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   879
                                                  Class<?> hostClass) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   880
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   881
            mh = mh.asType(type);
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   882
            mh = new WrappedMember(mh, type, member, hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   883
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   884
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   885
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   886
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   887
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   888
            final Class<?> THIS_CLASS = BindCaller.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   889
            assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   890
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   891
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   892
                    .findStatic(THIS_CLASS, "checkCallerClass",
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   893
                                MethodType.methodType(boolean.class, Class.class, Class.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   894
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   895
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   896
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   897
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   898
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   899
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   900
        @CallerSensitive
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   901
        private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   902
            // This method is called via MH_checkCallerClass and so it's
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   903
            // correct to ask for the immediate caller here.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   904
            Class<?> actual = Reflection.getCallerClass();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   905
            if (actual != expected && actual != expected2)
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   906
                throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   907
                                        +(expected == expected2 ? "" : ", or else "+expected2.getName()));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   908
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   909
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   910
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   911
        private static final byte[] T_BYTES;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   912
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   913
            final Object[] values = {null};
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   914
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   915
                    public Void run() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   916
                        try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   917
                            Class<T> tClass = T.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   918
                            String tName = tClass.getName();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   919
                            String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   920
                            java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   921
                            int len = uconn.getContentLength();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   922
                            byte[] bytes = new byte[len];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   923
                            try (java.io.InputStream str = uconn.getInputStream()) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   924
                                int nr = str.read(bytes);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   925
                                if (nr != len)  throw new java.io.IOException(tResource);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   926
                            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   927
                            values[0] = bytes;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   928
                        } catch (java.io.IOException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   929
                            throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   930
                        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   931
                        return null;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   932
                    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   933
                });
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   934
            T_BYTES = (byte[]) values[0];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   935
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   936
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   937
        // The following class is used as a template for Unsafe.defineAnonymousClass:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   938
        private static class T {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   939
            static void init() { }  // side effect: initializes this class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   940
            static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   941
                return vamh.invokeExact(args);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   942
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   943
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   944
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   945
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   946
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   947
    /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   948
    static class WrappedMember extends MethodHandle {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   949
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   950
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   951
        private final Class<?> callerClass;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   952
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   953
        private WrappedMember(MethodHandle target, MethodType type, MemberName member, Class<?> callerClass) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   954
            super(type, reinvokerForm(target));
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   955
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   956
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   957
            this.callerClass = callerClass;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   958
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   959
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   960
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   961
        MethodHandle reinvokerTarget() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   962
            return target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   963
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   964
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   965
        public MethodHandle asTypeUncached(MethodType newType) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   966
            // This MH is an alias for target, except for the MemberName
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   967
            // Drop the MemberName if there is any conversion.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   968
            return asTypeCache = target.asType(newType);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   969
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   970
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   971
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   972
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   973
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   974
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   975
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   976
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   977
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   978
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   979
        boolean isInvokeSpecial() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   980
            return target.isInvokeSpecial();
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   981
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   982
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   983
        MethodHandle viewAsType(MethodType newType) {
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   984
            return new WrappedMember(target, newType, member, callerClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   985
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   986
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   987
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   988
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   989
        if (member.equals(target.internalMemberName()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   990
            return target;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   991
        return new WrappedMember(target, target.type(), member, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   992
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   993
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   994
}