jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
author vlivanov
Wed, 10 Sep 2014 19:19:52 +0400
changeset 26481 c5b74a88a3c0
parent 26480 92d431f1ec8d
child 26482 cea1ab1c3ac7
permissions -rw-r--r--
8057042: LambdaFormEditor: derive new LFs from a base LF Reviewed-by: vlivanov, psandoz Contributed-by: john.r.rose@oracle.com
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;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    30
import java.util.ArrayList;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    31
import java.util.Arrays;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    32
import java.util.Collections;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    33
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    34
import sun.invoke.empty.Empty;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    35
import sun.invoke.util.ValueConversions;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    36
import sun.invoke.util.VerifyType;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    37
import sun.invoke.util.Wrapper;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    38
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    39
import sun.reflect.Reflection;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    40
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
    41
import static java.lang.invoke.MethodHandleStatics.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    42
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
    43
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    45
 * Trusted implementation code for MethodHandle.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    46
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    48
/*non-public*/ abstract class MethodHandleImpl {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    49
    // Do not adjust this except for special platforms:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    50
    private static final int MAX_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    51
    static {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    52
        final Object[] values = { 255 };
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    53
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    54
            @Override
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    55
            public Void run() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    56
                values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    57
                return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    58
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    59
        });
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    60
        MAX_ARITY = (Integer) values[0];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    61
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    62
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
    /// Factory methods to create method handles:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    64
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    65
    static void initStatics() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    66
        // Trigger selected static initializations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    67
        MemberName.Factory.INSTANCE.getClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    68
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    69
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    70
    static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    71
        if (arrayClass == Object[].class)
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    72
            return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    73
        if (!arrayClass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    74
            throw newIllegalArgumentException("not an array: "+arrayClass);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    75
        MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    76
        int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    77
        MethodHandle mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    78
        if (mh != null)  return mh;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    79
        mh = ArrayAccessor.getAccessor(arrayClass, isSetter);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    80
        MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    81
        if (mh.type() != correctType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    82
            assert(mh.type().parameterType(0) == Object[].class);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    83
            assert((isSetter ? mh.type().parameterType(2) : mh.type().returnType()) == Object.class);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    84
            assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType());
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    85
            // safe to view non-strictly, because element type follows from array type
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
    86
            mh = mh.viewAsType(correctType, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    87
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
    88
        mh = makeIntrinsic(mh, (isSetter ? Intrinsic.ARRAY_STORE : Intrinsic.ARRAY_LOAD));
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    89
        // Atomically update accessor cache.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    90
        synchronized(cache) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    91
            if (cache[cacheIndex] == null) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    92
                cache[cacheIndex] = mh;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    93
            } else {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    94
                // Throw away newly constructed accessor and use cached version.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    95
                mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    96
            }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    97
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    98
        return mh;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    99
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   100
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   101
    static final class ArrayAccessor {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   102
        /// Support for array element access
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   103
        static final int GETTER_INDEX = 0, SETTER_INDEX = 1, INDEX_LIMIT = 2;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   104
        static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   105
                = new ClassValue<MethodHandle[]>() {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   106
                    @Override
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   107
                    protected MethodHandle[] computeValue(Class<?> type) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   108
                        return new MethodHandle[INDEX_LIMIT];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   109
                    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   110
                };
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   111
        static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   112
        static {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   113
            MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   114
            cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, false), Intrinsic.ARRAY_LOAD);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   115
            cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, true),  Intrinsic.ARRAY_STORE);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   116
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   117
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   118
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   119
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   120
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   121
        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
   122
        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
   123
        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
   124
        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
   125
        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
   126
        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
   127
        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
   128
        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
   129
        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
   130
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   131
        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
   132
        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
   133
        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
   134
        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
   135
        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
   136
        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
   137
        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
   138
        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
   139
        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
   140
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   141
        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
   142
            Class<?> elemClass = arrayClass.getComponentType();
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   143
            if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   144
            return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   145
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   146
        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
   147
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   148
            Class<?> arrayArgClass = arrayClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   149
            if (!elemClass.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   150
                arrayArgClass = Object[].class;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   151
                elemClass = Object.class;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   152
            }
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   153
            return !isSetter ?
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   154
                    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
   155
                    MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   156
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   157
        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
   158
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   159
            return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   160
                    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
   161
                    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
   162
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   163
        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
   164
            String     name = name(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   165
            MethodType type = type(arrayClass, isSetter);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   166
            try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   167
                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
   168
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   169
                throw uncaughtException(ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   170
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   171
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   172
    }
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
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   175
     * 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
   176
     * 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
   177
     * 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
   178
     * 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
   179
     * 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
   180
     * @param srcType required call type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   181
     * @param target original method handle
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   182
     * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   183
     * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   184
     * @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
   185
     *          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
   186
     *          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
   187
     */
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   188
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   189
                                            boolean strict, boolean monobox) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   190
        MethodType dstType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   191
        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
   192
        if (srcType == dstType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   193
            return target;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   194
        return makePairwiseConvertIndirect(target, srcType, strict, monobox);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   195
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   196
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   197
    private static int countNonNull(Object[] array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   198
        int count = 0;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   199
        for (Object x : array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   200
            if (x != null)  ++count;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   201
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   202
        return count;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   203
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   204
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   205
    static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   206
                                                    boolean strict, boolean monobox) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   207
        // Calculate extra arguments (temporaries) required in the names array.
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   208
        Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   209
        final int INARG_COUNT = srcType.parameterCount();
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   210
        int convCount = countNonNull(convSpecs);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   211
        boolean retConv = (convSpecs[INARG_COUNT] != null);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   212
        boolean retVoid = srcType.returnType() == void.class;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   213
        if (retConv && retVoid) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   214
            convCount -= 1;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   215
            retConv = false;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   216
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   217
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   218
        final int IN_MH         = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   219
        final int INARG_BASE    = 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   220
        final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   221
        final int NAME_LIMIT    = INARG_LIMIT + convCount + 1;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   222
        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
   223
        final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   224
        final int RESULT        = (retVoid ? -1 : NAME_LIMIT - 1);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   225
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   226
        // Now build a LambdaForm.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   227
        MethodType lambdaType = srcType.basicType().invokerType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   228
        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
   229
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   230
        // 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
   231
        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
   232
        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
   233
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   234
        int nameCursor = INARG_LIMIT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   235
        for (int i = 0; i < INARG_COUNT; i++) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   236
            Object convSpec = convSpecs[i];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   237
            if (convSpec == null) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   238
                // do nothing: difference is trivial
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   239
                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
   240
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   241
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   242
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   243
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   244
            if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   245
                Class<?> convClass = (Class<?>) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   246
                conv = new Name(Lazy.MH_castReference, convClass, names[INARG_BASE + i]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   247
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   248
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   249
                conv = new Name(fn, names[INARG_BASE + i]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   250
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   251
            assert(names[nameCursor] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   252
            names[nameCursor++] = conv;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   253
            assert(outArgs[OUTARG_BASE + i] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   254
            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
   255
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   256
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   257
        // Build argument array for the call.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   258
        assert(nameCursor == OUT_CALL);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   259
        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
   260
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   261
        Object convSpec = convSpecs[INARG_COUNT];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   262
        if (!retConv) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   263
            assert(OUT_CALL == names.length-1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   264
        } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   265
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   266
            if (convSpec == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   267
                conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType())));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   268
            } else if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   269
                Class<?> convClass = (Class<?>) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   270
                conv = new Name(Lazy.MH_castReference, convClass, names[OUT_CALL]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   271
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   272
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   273
                if (fn.type().parameterCount() == 0)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   274
                    conv = new Name(fn);  // don't pass retval to void conversion
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   275
                else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   276
                    conv = new Name(fn, names[OUT_CALL]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   277
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   278
            assert(names[RETURN_CONV] == null);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   279
            names[RETURN_CONV] = conv;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   280
            assert(RETURN_CONV == names.length-1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   281
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   282
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   283
        LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names, RESULT);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   284
        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
   285
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   286
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   287
    /**
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   288
     * 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
   289
     * @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
   290
     * @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
   291
     * @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
   292
     */
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   293
    @ForceInline
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   294
    @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
   295
    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
   296
        // 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
   297
        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
   298
            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
   299
        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
   300
    }
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
    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
   303
        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
   304
    }
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   305
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   306
    static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   307
                                            boolean strict, boolean monobox) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   308
        final int INARG_COUNT = srcType.parameterCount();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   309
        Object[] convSpecs = new Object[INARG_COUNT+1];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   310
        for (int i = 0; i <= INARG_COUNT; i++) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   311
            boolean isRet = (i == INARG_COUNT);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   312
            Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   313
            Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   314
            if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   315
                convSpecs[i] = valueConversion(src, dst, strict, monobox);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   316
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   317
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   318
        return convSpecs;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   319
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   320
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   321
                                            boolean strict) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   322
        return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   323
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   324
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   325
    /**
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   326
     * Find a conversion function from the given source to the given destination.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   327
     * This conversion function will be used as a LF NamedFunction.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   328
     * Return a Class object if a simple cast is needed.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   329
     * Return void.class if void is involved.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   330
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   331
    static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   332
        assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   333
        if (dst == void.class)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   334
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   335
        MethodHandle fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   336
        if (src.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   337
            if (src == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   338
                return void.class;  // caller must recognize this specially
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   339
            } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   340
                // Examples: int->byte, byte->int, boolean->int (!strict)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   341
                fn = ValueConversions.convertPrimitive(src, dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   342
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   343
                // Examples: int->Integer, boolean->Object, float->Number
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   344
                Wrapper wsrc = Wrapper.forPrimitiveType(src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   345
                fn = ValueConversions.boxExact(wsrc);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   346
                assert(fn.type().parameterType(0) == wsrc.primitiveType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   347
                assert(fn.type().returnType() == wsrc.wrapperType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   348
                if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   349
                    // Corner case, such as int->Long, which will probably fail.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   350
                    MethodType mt = MethodType.methodType(dst, src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   351
                    if (strict)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   352
                        fn = fn.asType(mt);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   353
                    else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   354
                        fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   355
                }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   356
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   357
        } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   358
            Wrapper wdst = Wrapper.forPrimitiveType(dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   359
            if (monobox || src == wdst.wrapperType()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   360
                // Use a strongly-typed unboxer, if possible.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   361
                fn = ValueConversions.unboxExact(wdst, strict);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   362
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   363
                // Examples:  Object->int, Number->int, Comparable->int, Byte->int
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   364
                // must include additional conversions
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   365
                // src must be examined at runtime, to detect Byte, Character, etc.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   366
                fn = (strict
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   367
                        ? ValueConversions.unboxWiden(wdst)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   368
                        : ValueConversions.unboxCast(wdst));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   369
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   370
        } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   371
            // Simple reference conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   372
            // Note:  Do not check for a class hierarchy relation
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   373
            // between src and dst.  In all cases a 'null' argument
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   374
            // will pass the cast conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   375
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   376
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   377
        assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   378
        return fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   379
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   380
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   381
    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
   382
        MethodType type = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   383
        int last = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   384
        if (type.parameterType(last) != arrayType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   385
            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
   386
        target = target.asFixedArity();  // make sure this attribute is turned off
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   387
        return new AsVarargsCollector(target, arrayType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   388
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   389
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   390
    private static final class AsVarargsCollector extends DelegatingMethodHandle {
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   391
        private final MethodHandle target;
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   392
        private final Class<?> arrayType;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   393
        private @Stable MethodHandle asCollectorCache;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   394
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   395
        AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   396
            this(target.type(), target, arrayType);
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   397
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   398
        AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   399
            super(type, target);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   400
            this.target = target;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   401
            this.arrayType = arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   402
            this.asCollectorCache = target.asCollector(arrayType, 0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   403
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   404
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   405
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   406
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   407
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   408
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   409
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   410
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   411
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   412
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   413
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   414
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   415
        @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   416
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   417
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   418
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   419
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   420
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   421
        MethodHandle setVarargs(MemberName member) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   422
            if (member.isVarargs())  return this;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   423
            return asFixedArity();
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   424
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   425
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   426
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   427
        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
   428
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   429
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   430
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   431
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   432
                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
   433
                // 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
   434
                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
   435
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   436
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   437
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   438
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   439
                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
   440
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   441
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   442
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   443
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   444
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   445
                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
   446
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   447
                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
   448
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   449
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   450
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   451
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   452
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   453
        @Override
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   454
        boolean viewAsTypeChecks(MethodType newType, boolean strict) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   455
            super.viewAsTypeChecks(newType, true);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   456
            if (strict) return true;
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   457
            // extra assertion for non-strict checks:
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   458
            assert (type().lastParameterType().getComponentType()
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   459
                    .isAssignableFrom(
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   460
                            newType.lastParameterType().getComponentType()))
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   461
                    : Arrays.asList(this, newType);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   462
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   463
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   464
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   465
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   466
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   467
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   468
                                            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
   469
        MethodType targetType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   470
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   471
        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
   472
            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
   473
            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
   474
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   475
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   476
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   477
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   478
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   479
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   480
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   481
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   482
        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
   483
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   484
        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
   485
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   486
        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
   487
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   488
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   489
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   490
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   491
                Name array = names[argIndex];
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   492
                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
   493
                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
   494
                    indexes[i] = nameCursor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   495
                    names[nameCursor++] = new Name(aload, array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   496
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   497
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   498
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   499
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   500
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   501
        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
   502
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   503
        // 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
   504
        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
   505
        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
   506
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   507
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   508
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   509
        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
   510
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   511
        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
   512
        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
   513
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   514
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   515
    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
   516
        if (av == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   517
            if (n == 0)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   518
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   519
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   520
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   521
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   522
            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
   523
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   524
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   525
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   526
        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
   527
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   528
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   529
    /**
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   530
     * Pre-initialized NamedFunctions for bootstrapping purposes.
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   531
     * 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
   532
     */
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents: 26480
diff changeset
   533
    static class Lazy {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   534
        private static final Class<?> MHI = MethodHandleImpl.class;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   535
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   536
        static final NamedFunction NF_checkSpreadArgument;
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   537
        static final NamedFunction NF_guardWithCatch;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   538
        static final NamedFunction NF_throwException;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   539
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   540
        static final MethodHandle MH_castReference;
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   541
        static final MethodHandle MH_selectAlternative;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   542
        static final MethodHandle MH_copyAsPrimitiveArray;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   543
        static final MethodHandle MH_fillNewTypedArray;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   544
        static final MethodHandle MH_fillNewArray;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   545
        static final MethodHandle MH_arrayIdentity;
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
   546
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   547
        static {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   548
            try {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   549
                NF_checkSpreadArgument = new NamedFunction(MHI.getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   550
                NF_guardWithCatch      = new NamedFunction(MHI.getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   551
                                                                                 MethodHandle.class, Object[].class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   552
                NF_throwException      = new NamedFunction(MHI.getDeclaredMethod("throwException", Throwable.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   553
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   554
                NF_checkSpreadArgument.resolve();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   555
                NF_guardWithCatch.resolve();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   556
                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
   557
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   558
                MH_castReference        = IMPL_LOOKUP.findStatic(MHI, "castReference",
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   559
                                            MethodType.methodType(Object.class, Class.class, Object.class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   560
                MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray",
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   561
                                            MethodType.methodType(Object.class, Wrapper.class, Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   562
                MH_arrayIdentity        = IMPL_LOOKUP.findStatic(MHI, "identity",
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   563
                                            MethodType.methodType(Object[].class, Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   564
                MH_fillNewArray         = IMPL_LOOKUP.findStatic(MHI, "fillNewArray",
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   565
                                            MethodType.methodType(Object[].class, Integer.class, Object[].class));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   566
                MH_fillNewTypedArray    = IMPL_LOOKUP.findStatic(MHI, "fillNewTypedArray",
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   567
                                            MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   568
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   569
                MH_selectAlternative    = makeIntrinsic(
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   570
                        IMPL_LOOKUP.findStatic(MHI, "selectAlternative",
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   571
                                MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)),
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   572
                        Intrinsic.SELECT_ALTERNATIVE);
20495
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   573
            } catch (ReflectiveOperationException ex) {
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   574
                throw newInternalError(ex);
6586c2f78d57 8024616: JSR292: lazily initialize core NamedFunctions used for bootstrapping
vlivanov
parents: 19804
diff changeset
   575
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   576
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   577
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   578
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   579
    /** 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
   580
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   581
                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
   582
        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
   583
        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
   584
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   585
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   586
        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
   587
        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
   588
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   589
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   590
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   591
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   592
        // 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
   593
        // 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
   594
        // 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
   595
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   596
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   597
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   598
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   599
        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
   600
        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
   601
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   602
        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
   603
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   604
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   605
        // 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
   606
        // 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
   607
        // 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
   608
        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
   609
        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
   610
        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
   611
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   612
        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
   613
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   614
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   615
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   616
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   617
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   618
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   619
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   620
            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
   621
            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
   622
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   623
        inputArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   624
        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
   625
        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
   626
        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
   627
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   628
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   629
        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
   630
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   631
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   632
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   633
    @LambdaForm.Hidden
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   634
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   635
    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
   636
        return testResult ? target : fallback;
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   637
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   638
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   639
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   640
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   641
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   642
                                   MethodHandle fallback) {
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   643
        MethodType type = target.type();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   644
        assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   645
        MethodType basicType = type.basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   646
        LambdaForm form = makeGuardWithTestForm(basicType);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   647
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   648
        BoundMethodHandle mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   649
        try {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   650
            mh = (BoundMethodHandle)
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   651
                    data.constructor().invokeBasic(type, form,
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   652
                        (Object) test, (Object) target, (Object) fallback);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   653
        } catch (Throwable ex) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   654
            throw uncaughtException(ex);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   655
        }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   656
        assert(mh.type() == type);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   657
        return mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   658
    }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   659
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   660
    static
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   661
    LambdaForm makeGuardWithTestForm(MethodType basicType) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   662
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   663
        if (lform != null)  return lform;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   664
        final int THIS_MH      = 0;  // the BMH_LLL
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   665
        final int ARG_BASE     = 1;  // start of incoming arguments
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   666
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   667
        int nameCursor = ARG_LIMIT;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   668
        final int GET_TEST     = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   669
        final int GET_TARGET   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   670
        final int GET_FALLBACK = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   671
        final int CALL_TEST    = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   672
        final int SELECT_ALT   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   673
        final int CALL_TARGET  = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   674
        assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   675
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   676
        MethodType lambdaType = basicType.invokerType();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   677
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   678
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   679
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   680
        names[THIS_MH] = names[THIS_MH].withConstraint(data);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   681
        names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   682
        names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   683
        names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   684
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   685
        Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   686
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   687
        // call test
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   688
        MethodType testType = basicType.changeReturnType(boolean.class).basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   689
        invokeArgs[0] = names[GET_TEST];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   690
        names[CALL_TEST] = new Name(testType, invokeArgs);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   691
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   692
        // call selectAlternative
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   693
        names[SELECT_ALT] = new Name(Lazy.MH_selectAlternative, names[CALL_TEST],
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   694
                                     names[GET_TARGET], names[GET_FALLBACK]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   695
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   696
        // call target or fallback
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   697
        invokeArgs[0] = names[SELECT_ALT];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   698
        names[CALL_TARGET] = new Name(basicType, invokeArgs);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   699
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   700
        lform = new LambdaForm("guard", lambdaType.parameterCount(), names);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   701
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   702
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   703
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   704
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   705
    /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
   706
     * The LambdaForm shape for catchException combinator is the following:
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   707
     * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   708
     *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   709
     *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   710
     *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   711
     *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   712
     *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   713
     *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   714
     *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   715
     *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   716
     *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   717
     * }</pre></blockquote>
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
     * 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
   720
     * 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
   721
     * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   722
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   723
     * 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
   724
     * among catchException combinators with the same basic type.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   725
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   726
    private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   727
        MethodType lambdaType = basicType.invokerType();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   728
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   729
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   730
        if (lform != null) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   731
            return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   732
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   733
        final int THIS_MH      = 0;  // the BMH_LLLLL
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   734
        final int ARG_BASE     = 1;  // start of incoming arguments
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   735
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   736
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   737
        int nameCursor = ARG_LIMIT;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   738
        final int GET_TARGET       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   739
        final int GET_CLASS        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   740
        final int GET_CATCHER      = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   741
        final int GET_COLLECT_ARGS = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   742
        final int GET_UNBOX_RESULT = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   743
        final int BOXED_ARGS       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   744
        final int TRY_CATCH        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   745
        final int UNBOX_RESULT     = nameCursor++;
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
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   748
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   749
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
26479
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26476
diff changeset
   750
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   751
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   752
        names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   753
        names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   754
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   755
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   756
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   757
        // FIXME: rework argument boxing/result unboxing logic for LF interpretation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   758
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   759
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   760
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   761
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   762
        Object[] args = new Object[invokeBasic.type().parameterCount()];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   763
        args[0] = names[GET_COLLECT_ARGS];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   764
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   765
        names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   766
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   767
        // 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
   768
        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
   769
        names[TRY_CATCH] = new Name(Lazy.NF_guardWithCatch, gwcArgs);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   770
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   771
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   772
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   773
        Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   774
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   775
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   776
        lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   777
24696
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 24572
diff changeset
   778
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   779
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   780
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   781
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   782
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   783
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   784
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   785
        MethodType type = target.type();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   786
        LambdaForm form = makeGuardWithCatchForm(type.basicType());
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   787
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
   788
        // Prepare auxiliary method handles used during LambdaForm interpretation.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   789
        // Box arguments and wrap them into Object[]: ValueConversions.array().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   790
        MethodType varargsType = type.changeReturnType(Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
   791
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   792
        // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   793
        MethodHandle unboxResult;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   794
        Class<?> rtype = type.returnType();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   795
        if (rtype.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   796
            if (rtype == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   797
                unboxResult = ValueConversions.ignore();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   798
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   799
                Wrapper w = Wrapper.forPrimitiveType(type.returnType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   800
                unboxResult = ValueConversions.unboxExact(w);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   801
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   802
        } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   803
            unboxResult = MethodHandles.identity(Object.class);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   804
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   805
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   806
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   807
        BoundMethodHandle mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   808
        try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   809
            mh = (BoundMethodHandle)
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   810
                    data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   811
                                                   (Object) collectArgs, (Object) unboxResult);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   812
        } catch (Throwable ex) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   813
            throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   814
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   815
        assert(mh.type() == type);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   816
        return mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   817
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   818
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   819
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   820
     * Intrinsified during LambdaForm compilation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   821
     * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   822
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   823
    @LambdaForm.Hidden
23059
4aca6147df88 8036732: Fix raw warning in java.lang.invoke.MethodHandleImpl.java
darcy
parents: 23039
diff changeset
   824
    static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   825
                                 Object... av) throws Throwable {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   826
        // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   827
        try {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   828
            return target.asFixedArity().invokeWithArguments(av);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   829
        } catch (Throwable t) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   830
            if (!exType.isInstance(t)) throw t;
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   831
            return catcher.asFixedArity().invokeWithArguments(prepend(t, av));
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   832
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   833
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   834
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   835
    /** Prepend an element {@code elem} to an {@code array}. */
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   836
    @LambdaForm.Hidden
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   837
    private static Object[] prepend(Object elem, Object[] array) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   838
        Object[] newArray = new Object[array.length+1];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   839
        newArray[0] = elem;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   840
        System.arraycopy(array, 0, newArray, 1, array.length);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   841
        return newArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   842
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   843
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   844
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   845
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   846
        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
   847
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   848
        if (arity > 1) {
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26467
diff changeset
   849
            MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26467
diff changeset
   850
            mh = MethodHandles.dropArguments(mh, 1, type.parameterList().subList(1, arity));
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26467
diff changeset
   851
            return mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   852
        }
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   853
        return makePairwiseConvert(Lazy.NF_throwException.resolvedHandle(), type, false, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   854
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   855
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   856
    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
   857
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   858
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   859
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   860
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   861
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   862
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   863
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   864
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   865
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   866
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   867
        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
   868
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   869
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   870
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   871
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   872
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   873
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   874
            throw new InternalError(method.toString());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   875
        mh = mh.withInternalMemberName(method, false);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   876
        mh = mh.asVarargsCollector(Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   877
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   878
        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
   879
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   880
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   881
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   882
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   883
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   884
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   885
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   886
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   887
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   888
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   889
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   890
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   891
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   892
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   893
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   894
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   895
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   896
    // 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
   897
    private static class BindCaller {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   898
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   899
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   900
            // Do not use this function to inject calls into system classes.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   901
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   902
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   903
                       hostClass.isPrimitive() ||
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   904
                       hostClass.getName().startsWith("java.") ||
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
   905
                       hostClass.getName().startsWith("sun."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   906
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   907
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   908
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   909
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   910
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   911
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   912
            return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   913
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   914
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   915
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   916
            Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   917
            if (hostClass.getClassLoader() != bcc.getClassLoader())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   918
                throw new InternalError(hostClass.getName()+" (CL)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   919
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   920
                if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   921
                    throw new InternalError(hostClass.getName()+" (PD)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   922
            } catch (SecurityException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   923
                // Self-check was blocked by security manager.  This is OK.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   924
                // In fact the whole try body could be turned into an assertion.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   925
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   926
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   927
                MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   928
                init.invokeExact();  // force initialization of the class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   929
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   930
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   931
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   932
            MethodHandle bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   933
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   934
                MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   935
                bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   936
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   937
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   938
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   939
            // 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
   940
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   941
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   942
                Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   943
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   944
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   945
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   946
            return bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   947
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   948
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   949
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   950
                return makeInjectedInvoker(hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   951
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   952
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   953
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   954
        // 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
   955
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   956
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   957
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   958
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   959
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   960
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   961
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   962
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   963
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   964
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   965
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   966
        // Undo the adapter effect of prepareForInvoker:
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   967
        private static MethodHandle restoreToType(MethodHandle vamh,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   968
                                                  MethodHandle original,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
   969
                                                  Class<?> hostClass) {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   970
            MethodType type = original.type();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   971
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   972
            MemberName member = original.internalMemberName();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   973
            mh = mh.asType(type);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
   974
            mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
   975
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   976
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   977
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   978
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   979
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   980
            final Class<?> THIS_CLASS = BindCaller.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   981
            assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   982
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   983
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   984
                    .findStatic(THIS_CLASS, "checkCallerClass",
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   985
                                MethodType.methodType(boolean.class, Class.class, Class.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   986
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   987
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   988
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   989
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   990
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   991
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   992
        @CallerSensitive
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   993
        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
   994
            // 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
   995
            // correct to ask for the immediate caller here.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
   996
            Class<?> actual = Reflection.getCallerClass();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   997
            if (actual != expected && actual != expected2)
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   998
                throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   999
                                        +(expected == expected2 ? "" : ", or else "+expected2.getName()));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1000
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1001
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1002
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1003
        private static final byte[] T_BYTES;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1004
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1005
            final Object[] values = {null};
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1006
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1007
                    public Void run() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1008
                        try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1009
                            Class<T> tClass = T.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1010
                            String tName = tClass.getName();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1011
                            String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1012
                            java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1013
                            int len = uconn.getContentLength();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1014
                            byte[] bytes = new byte[len];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1015
                            try (java.io.InputStream str = uconn.getInputStream()) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1016
                                int nr = str.read(bytes);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1017
                                if (nr != len)  throw new java.io.IOException(tResource);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1018
                            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1019
                            values[0] = bytes;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1020
                        } catch (java.io.IOException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1021
                            throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1022
                        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1023
                        return null;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1024
                    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1025
                });
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1026
            T_BYTES = (byte[]) values[0];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1027
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1028
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1029
        // The following class is used as a template for Unsafe.defineAnonymousClass:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1030
        private static class T {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1031
            static void init() { }  // side effect: initializes this class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1032
            static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1033
                return vamh.invokeExact(args);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1034
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1035
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1036
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1037
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1038
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1039
    /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1040
    private static final class WrappedMember extends DelegatingMethodHandle {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1041
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1042
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1043
        private final Class<?> callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1044
        private final boolean isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1045
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1046
        private WrappedMember(MethodHandle target, MethodType type,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1047
                              MemberName member, boolean isInvokeSpecial,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1048
                              Class<?> callerClass) {
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1049
            super(type, target);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1050
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1051
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1052
            this.callerClass = callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1053
            this.isInvokeSpecial = isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1054
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1055
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1056
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1057
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1058
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1059
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1060
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1061
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1062
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1063
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1064
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1065
        boolean isInvokeSpecial() {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1066
            return isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1067
        }
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1068
        @Override
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1069
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1070
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1071
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1072
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1073
        public MethodHandle asTypeUncached(MethodType newType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1074
            // This MH is an alias for target, except for the MemberName
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1075
            // Drop the MemberName if there is any conversion.
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1076
            return asTypeCache = target.asType(newType);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1077
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1078
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1079
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1080
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1081
        if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1082
            return target;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1083
        return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1084
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1085
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1086
    /** Intrinsic IDs */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1087
    /*non-public*/
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1088
    enum Intrinsic {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1089
        SELECT_ALTERNATIVE,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1090
        GUARD_WITH_CATCH,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1091
        NEW_ARRAY,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1092
        ARRAY_LOAD,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1093
        ARRAY_STORE,
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1094
        IDENTITY,
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  1095
        ZERO,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1096
        NONE // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1097
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1098
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1099
    /** Mark arbitrary method handle as intrinsic.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1100
     * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1101
    private static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1102
        private final MethodHandle target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1103
        private final Intrinsic intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1104
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1105
        IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1106
            super(target.type(), target);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1107
            this.target = target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1108
            this.intrinsicName = intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1109
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1110
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1111
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1112
        protected MethodHandle getTarget() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1113
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1114
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1115
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1116
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1117
        Intrinsic intrinsicName() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1118
            return intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1119
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1120
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1121
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1122
        public MethodHandle asTypeUncached(MethodType newType) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1123
            // This MH is an alias for target, except for the intrinsic name
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1124
            // Drop the name if there is any conversion.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1125
            return asTypeCache = target.asType(newType);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1126
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1127
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1128
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1129
        String internalProperties() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1130
            return super.internalProperties() +
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1131
                    "\n& Intrinsic="+intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1132
        }
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1133
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1134
        @Override
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1135
        public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1136
            if (intrinsicName == Intrinsic.IDENTITY) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1137
                MethodType resultType = type().asCollectorType(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1138
                MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1139
                return newArray.asType(resultType);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1140
            }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1141
            return super.asCollector(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1142
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1143
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1144
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1145
    static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1146
        if (intrinsicName == target.intrinsicName())
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1147
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1148
        return new IntrinsicMethodHandle(target, intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1149
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1150
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1151
    static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1152
        return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1153
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1154
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1155
    /// Collection of multiple arguments.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1156
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1157
    private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1158
        MethodType type = MethodType.genericMethodType(nargs)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1159
                .changeReturnType(rtype)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1160
                .insertParameterTypes(0, ptypes);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1161
        try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1162
            return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1163
        } catch (ReflectiveOperationException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1164
            return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1165
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1166
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1167
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1168
    private static final Object[] NO_ARGS_ARRAY = {};
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1169
    private static Object[] makeArray(Object... args) { return args; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1170
    private static Object[] array() { return NO_ARGS_ARRAY; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1171
    private static Object[] array(Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1172
                { return makeArray(a0); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1173
    private static Object[] array(Object a0, Object a1)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1174
                { return makeArray(a0, a1); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1175
    private static Object[] array(Object a0, Object a1, Object a2)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1176
                { return makeArray(a0, a1, a2); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1177
    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1178
                { return makeArray(a0, a1, a2, a3); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1179
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1180
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1181
                { return makeArray(a0, a1, a2, a3, a4); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1182
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1183
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1184
                { return makeArray(a0, a1, a2, a3, a4, a5); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1185
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1186
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1187
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1188
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1189
                                  Object a4, Object a5, Object a6, Object a7)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1190
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1191
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1192
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1193
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1194
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1195
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1196
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1197
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1198
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1199
    private static MethodHandle[] makeArrays() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1200
        ArrayList<MethodHandle> mhs = new ArrayList<>();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1201
        for (;;) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1202
            MethodHandle mh = findCollector("array", mhs.size(), Object[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1203
            if (mh == null)  break;
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1204
            mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1205
            mhs.add(mh);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1206
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1207
        assert(mhs.size() == 11);  // current number of methods
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1208
        return mhs.toArray(new MethodHandle[MAX_ARITY+1]);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1209
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1210
    private static final MethodHandle[] ARRAYS = makeArrays();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1211
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1212
    // filling versions of the above:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1213
    // using Integer len instead of int len and no varargs to avoid bootstrapping problems
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1214
    private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1215
        Object[] a = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1216
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1217
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1218
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1219
    private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1220
        Object[] a = Arrays.copyOf(example, len);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1221
        assert(a.getClass() != Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1222
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1223
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1224
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1225
    private static void fillWithArguments(Object[] a, int pos, Object... args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1226
        System.arraycopy(args, 0, a, pos, args.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1227
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1228
    // using Integer pos instead of int pos to avoid bootstrapping problems
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1229
    private static Object[] fillArray(Integer pos, Object[] a, Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1230
                { fillWithArguments(a, pos, a0); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1231
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1232
                { fillWithArguments(a, pos, a0, a1); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1233
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1234
                { fillWithArguments(a, pos, a0, a1, a2); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1235
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1236
                { fillWithArguments(a, pos, a0, a1, a2, a3); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1237
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1238
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1239
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1240
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1241
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1242
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1243
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1244
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1245
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1246
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1247
                                  Object a4, Object a5, Object a6, Object a7)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1248
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1249
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1250
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1251
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1252
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1253
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1254
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1255
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1256
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1257
    private static MethodHandle[] makeFillArrays() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1258
        ArrayList<MethodHandle> mhs = new ArrayList<>();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1259
        mhs.add(null);  // there is no empty fill; at least a0 is required
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1260
        for (;;) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1261
            MethodHandle mh = findCollector("fillArray", mhs.size(), Object[].class, Integer.class, Object[].class);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1262
            if (mh == null)  break;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1263
            mhs.add(mh);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1264
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1265
        assert(mhs.size() == 11);  // current number of methods
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1266
        return mhs.toArray(new MethodHandle[0]);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1267
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1268
    private static final MethodHandle[] FILL_ARRAYS = makeFillArrays();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1269
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1270
    private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1271
        Object a = w.makeArray(boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1272
        w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1273
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1274
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1275
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1276
    /** Return a method handle that takes the indicated number of Object
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1277
     *  arguments and returns an Object array of them, as if for varargs.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1278
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1279
    static MethodHandle varargsArray(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1280
        MethodHandle mh = ARRAYS[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1281
        if (mh != null)  return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1282
        mh = findCollector("array", nargs, Object[].class);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1283
        if (mh != null)  mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1284
        if (mh != null)  return ARRAYS[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1285
        mh = buildVarargsArray(Lazy.MH_fillNewArray, Lazy.MH_arrayIdentity, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1286
        assert(assertCorrectArity(mh, nargs));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1287
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1288
        return ARRAYS[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1289
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1290
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1291
    private static boolean assertCorrectArity(MethodHandle mh, int arity) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1292
        assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1293
        return true;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1294
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1295
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1296
    // Array identity function (used as Lazy.MH_arrayIdentity).
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1297
    static <T> T[] identity(T[] x) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1298
        return x;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1299
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1300
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1301
    private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1302
        // Build up the result mh as a sequence of fills like this:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1303
        //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1304
        // The various fill(_,10*I,___*[J]) are reusable.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1305
        int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1306
        int rightLen = nargs - leftLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1307
        MethodHandle leftCollector = newArray.bindTo(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1308
        leftCollector = leftCollector.asCollector(Object[].class, leftLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1309
        MethodHandle mh = finisher;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1310
        if (rightLen > 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1311
            MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1312
            if (mh == Lazy.MH_arrayIdentity)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1313
                mh = rightFiller;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1314
            else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1315
                mh = MethodHandles.collectArguments(mh, 0, rightFiller);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1316
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1317
        if (mh == Lazy.MH_arrayIdentity)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1318
            mh = leftCollector;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1319
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1320
            mh = MethodHandles.collectArguments(mh, 0, leftCollector);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1321
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1322
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1323
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1324
    private static final int LEFT_ARGS = (FILL_ARRAYS.length - 1);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1325
    private static final MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1326
    /** fill_array_to_right(N).invoke(a, argL..arg[N-1])
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1327
     *  fills a[L]..a[N-1] with corresponding arguments,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1328
     *  and then returns a.  The value L is a global constant (LEFT_ARGS).
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1329
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1330
    private static MethodHandle fillToRight(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1331
        MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1332
        if (filler != null)  return filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1333
        filler = buildFiller(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1334
        assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1335
        return FILL_ARRAY_TO_RIGHT[nargs] = filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1336
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1337
    private static MethodHandle buildFiller(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1338
        if (nargs <= LEFT_ARGS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1339
            return Lazy.MH_arrayIdentity;  // no args to fill; return the array unchanged
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1340
        // we need room for both mh and a in mh.invoke(a, arg*[nargs])
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1341
        final int CHUNK = LEFT_ARGS;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1342
        int rightLen = nargs % CHUNK;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1343
        int midLen = nargs - rightLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1344
        if (rightLen == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1345
            midLen = nargs - (rightLen = CHUNK);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1346
            if (FILL_ARRAY_TO_RIGHT[midLen] == null) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1347
                // build some precursors from left to right
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1348
                for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1349
                    if (j > LEFT_ARGS)  fillToRight(j);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1350
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1351
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1352
        if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1353
        assert(rightLen > 0);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1354
        MethodHandle midFill = fillToRight(midLen);  // recursive fill
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1355
        MethodHandle rightFill = FILL_ARRAYS[rightLen].bindTo(midLen);  // [midLen..nargs-1]
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1356
        assert(midFill.type().parameterCount()   == 1 + midLen - LEFT_ARGS);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1357
        assert(rightFill.type().parameterCount() == 1 + rightLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1358
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1359
        // Combine the two fills:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1360
        //   right(mid(a, x10..x19), x20..x23)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1361
        // The final product will look like this:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1362
        //   right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1363
        if (midLen == LEFT_ARGS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1364
            return rightFill;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1365
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1366
            return MethodHandles.collectArguments(rightFill, 0, midFill);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1367
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1368
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1369
    // Type-polymorphic version of varargs maker.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1370
    private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1371
        = new ClassValue<MethodHandle[]>() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1372
            @Override
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1373
            protected MethodHandle[] computeValue(Class<?> type) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1374
                return new MethodHandle[256];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1375
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1376
    };
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1377
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1378
    static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1379
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1380
    /** Return a method handle that takes the indicated number of
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1381
     *  typed arguments and returns an array of them.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1382
     *  The type argument is the array type.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1383
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1384
    static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1385
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1386
        if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1387
        // FIXME: Need more special casing and caching here.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1388
        if (nargs >= MAX_JVM_ARITY/2 - 1) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1389
            int slots = nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1390
            final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1391
            if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1392
                slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1393
            if (slots > MAX_ARRAY_SLOTS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1394
                throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1395
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1396
        if (elemType == Object.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1397
            return varargsArray(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1398
        // other cases:  primitive arrays, subtypes of Object[]
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1399
        MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1400
        MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1401
        if (mh != null)  return mh;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1402
        if (nargs == 0) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1403
            Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1404
            mh = MethodHandles.constant(arrayType, example);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1405
        } else if (elemType.isPrimitive()) {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1406
            MethodHandle builder = Lazy.MH_fillNewArray;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1407
            MethodHandle producer = buildArrayProducer(arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1408
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1409
        } else {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1410
            Class<? extends Object[]> objArrayType = arrayType.asSubclass(Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1411
            Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1412
            MethodHandle builder = Lazy.MH_fillNewTypedArray.bindTo(example);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1413
            MethodHandle producer = Lazy.MH_arrayIdentity; // must be weakly typed
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1414
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1415
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1416
        mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1417
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1418
        assert(assertCorrectArity(mh, nargs));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1419
        if (nargs < cache.length)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1420
            cache[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1421
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1422
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1423
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1424
    private static MethodHandle buildArrayProducer(Class<?> arrayType) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1425
        Class<?> elemType = arrayType.getComponentType();
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1426
        assert(elemType.isPrimitive());
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1427
        return Lazy.MH_copyAsPrimitiveArray.bindTo(Wrapper.forPrimitiveType(elemType));
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1428
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1429
}