jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
author redestad
Tue, 17 Nov 2015 15:29:21 +0100
changeset 33846 049daf20246e
parent 33841 1d1d1ea6a5f2
child 33874 46651fd30c0b
permissions -rw-r--r--
8143142: AssertionError in MethodHandleImpl Reviewed-by: psandoz, vlivanov
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
/*
31802
cb6542e7d329 8062543: Replace uses of MethodHandleImpl.castReference with Class.cast
mhaupt
parents: 31671
diff changeset
     2
 * Copyright (c) 2008, 2015, 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;
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
    33
import java.util.function.Function;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    34
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    35
import sun.invoke.empty.Empty;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    36
import sun.invoke.util.ValueConversions;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
    37
import sun.invoke.util.VerifyType;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    38
import sun.invoke.util.Wrapper;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
    39
import jdk.internal.HotSpotIntrinsicCandidate;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    40
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
    41
import sun.reflect.Reflection;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    42
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
    43
import static java.lang.invoke.MethodHandleStatics.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    44
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
    45
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    46
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    47
 * Trusted implementation code for MethodHandle.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    49
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    50
/*non-public*/ abstract class MethodHandleImpl {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    51
    // 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
    52
    private static final int MAX_ARITY;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    53
    static {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    54
        final Object[] values = { 255 };
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29022
diff changeset
    55
        AccessController.doPrivileged(new PrivilegedAction<>() {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    56
            @Override
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    57
            public Void run() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    58
                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
    59
                return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    60
            }
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
        MAX_ARITY = (Integer) values[0];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    63
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    64
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    65
    /// Factory methods to create method handles:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    66
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    67
    static void initStatics() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    68
        // Trigger selected static initializations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    69
        MemberName.Factory.INSTANCE.getClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    70
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    71
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    72
    static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    73
        if (arrayClass == Object[].class)
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    74
            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
    75
        if (!arrayClass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    76
            throw newIllegalArgumentException("not an array: "+arrayClass);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    77
        MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    78
        int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    79
        MethodHandle mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    80
        if (mh != null)  return mh;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    81
        mh = ArrayAccessor.getAccessor(arrayClass, isSetter);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    82
        MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    83
        if (mh.type() != correctType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    84
            assert(mh.type().parameterType(0) == Object[].class);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    85
            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
    86
            assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType());
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    87
            // 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
    88
            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
    89
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
    90
        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
    91
        // Atomically update accessor cache.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    92
        synchronized(cache) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    93
            if (cache[cacheIndex] == null) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    94
                cache[cacheIndex] = mh;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    95
            } else {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    96
                // Throw away newly constructed accessor and use cached version.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    97
                mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    98
            }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    99
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   100
        return mh;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   101
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   103
    static final class ArrayAccessor {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   104
        /// Support for array element access
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   105
        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
   106
        static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   107
                = new ClassValue<MethodHandle[]>() {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   108
                    @Override
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   109
                    protected MethodHandle[] computeValue(Class<?> type) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   110
                        return new MethodHandle[INDEX_LIMIT];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   111
                    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   112
                };
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   113
        static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   114
        static {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   115
            MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
   116
            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
   117
            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
   118
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   119
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   120
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   121
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   122
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   123
        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
   124
        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
   125
        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
   126
        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
   127
        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
   128
        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
   129
        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
   130
        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
   131
        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
   132
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   133
        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
   134
        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
   135
        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
   136
        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
   137
        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
   138
        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
   139
        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
   140
        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
   141
        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
   142
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   143
        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
   144
            Class<?> elemClass = arrayClass.getComponentType();
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   145
            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
   146
            return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   147
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   148
        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
   149
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   150
            Class<?> arrayArgClass = arrayClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   151
            if (!elemClass.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   152
                arrayArgClass = Object[].class;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   153
                elemClass = Object.class;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   154
            }
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   155
            return !isSetter ?
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   156
                    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
   157
                    MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   158
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   159
        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
   160
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   161
            return !isSetter ?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   162
                    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
   163
                    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
   164
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   165
        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
   166
            String     name = name(arrayClass, isSetter);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   167
            MethodType type = type(arrayClass, isSetter);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   168
            try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   169
                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
   170
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   171
                throw uncaughtException(ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   172
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   173
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   174
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   175
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   176
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   177
     * 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
   178
     * 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
   179
     * 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
   180
     * 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
   181
     * 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
   182
     * @param srcType required call type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   183
     * @param target original method handle
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   184
     * @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
   185
     * @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
   186
     * @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
   187
     *          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
   188
     *          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
   189
     */
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   190
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   191
                                            boolean strict, boolean monobox) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   192
        MethodType dstType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   193
        if (srcType == dstType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   194
            return target;
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 27350
diff changeset
   195
        return makePairwiseConvertByEditor(target, srcType, strict, monobox);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   196
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   197
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   198
    private static int countNonNull(Object[] array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   199
        int count = 0;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   200
        for (Object x : array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   201
            if (x != null)  ++count;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   202
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   203
        return count;
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
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   206
    static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   207
                                                    boolean strict, boolean monobox) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   208
        Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   209
        int convCount = countNonNull(convSpecs);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   210
        if (convCount == 0)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   211
            return target.viewAsType(srcType, strict);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   212
        MethodType basicSrcType = srcType.basicType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   213
        MethodType midType = target.type().basicType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   214
        BoundMethodHandle mh = target.rebind();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   215
        // FIXME: Reduce number of bindings when there is more than one Class conversion.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   216
        // FIXME: Reduce number of bindings when there are repeated conversions.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   217
        for (int i = 0; i < convSpecs.length-1; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   218
            Object convSpec = convSpecs[i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   219
            if (convSpec == null)  continue;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   220
            MethodHandle fn;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   221
            if (convSpec instanceof Class) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   222
                fn = getConstantHandle(MH_cast).bindTo(convSpec);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   223
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   224
                fn = (MethodHandle) convSpec;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   225
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   226
            Class<?> newType = basicSrcType.parameterType(i);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   227
            if (--convCount == 0)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   228
                midType = srcType;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   229
            else
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   230
                midType = midType.changeParameterType(i, newType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   231
            LambdaForm form2 = mh.editor().filterArgumentForm(1+i, BasicType.basicType(newType));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   232
            mh = mh.copyWithExtendL(midType, form2, fn);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   233
            mh = mh.rebind();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   234
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   235
        Object convSpec = convSpecs[convSpecs.length-1];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   236
        if (convSpec != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   237
            MethodHandle fn;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   238
            if (convSpec instanceof Class) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   239
                if (convSpec == void.class)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   240
                    fn = null;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   241
                else
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   242
                    fn = getConstantHandle(MH_cast).bindTo(convSpec);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   243
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   244
                fn = (MethodHandle) convSpec;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   245
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   246
            Class<?> newType = basicSrcType.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   247
            assert(--convCount == 0);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   248
            midType = srcType;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   249
            if (fn != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   250
                mh = mh.rebind();  // rebind if too complex
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   251
                LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   252
                mh = mh.copyWithExtendL(midType, form2, fn);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   253
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   254
                LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   255
                mh = mh.copyWith(midType, form2);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   256
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   257
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   258
        assert(convCount == 0);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   259
        assert(mh.type().equals(srcType));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   260
        return mh;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   261
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   262
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   263
    static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   264
                                                    boolean strict, boolean monobox) {
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
   265
        assert(target.type().parameterCount() == srcType.parameterCount());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   266
        // 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
   267
        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
   268
        final int INARG_COUNT = srcType.parameterCount();
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   269
        int convCount = countNonNull(convSpecs);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   270
        boolean retConv = (convSpecs[INARG_COUNT] != null);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   271
        boolean retVoid = srcType.returnType() == void.class;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   272
        if (retConv && retVoid) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   273
            convCount -= 1;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   274
            retConv = false;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   275
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   276
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   277
        final int IN_MH         = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   278
        final int INARG_BASE    = 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   279
        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
   280
        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
   281
        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
   282
        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
   283
        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
   284
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   285
        // Now build a LambdaForm.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   286
        MethodType lambdaType = srcType.basicType().invokerType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   287
        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
   288
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   289
        // 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
   290
        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
   291
        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
   292
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   293
        int nameCursor = INARG_LIMIT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   294
        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
   295
            Object convSpec = convSpecs[i];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   296
            if (convSpec == null) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   297
                // do nothing: difference is trivial
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   298
                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
   299
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   300
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   301
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   302
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   303
            if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   304
                Class<?> convClass = (Class<?>) convSpec;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   305
                conv = new Name(getConstantHandle(MH_cast), 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
   306
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   307
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   308
                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
   309
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   310
            assert(names[nameCursor] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   311
            names[nameCursor++] = conv;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   312
            assert(outArgs[OUTARG_BASE + i] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   313
            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
   314
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   315
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   316
        // Build argument array for the call.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   317
        assert(nameCursor == OUT_CALL);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   318
        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
   319
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   320
        Object convSpec = convSpecs[INARG_COUNT];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   321
        if (!retConv) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   322
            assert(OUT_CALL == names.length-1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   323
        } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   324
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   325
            if (convSpec == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   326
                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
   327
            } else if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   328
                Class<?> convClass = (Class<?>) convSpec;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   329
                conv = new Name(getConstantHandle(MH_cast), convClass, names[OUT_CALL]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   330
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   331
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   332
                if (fn.type().parameterCount() == 0)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   333
                    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
   334
                else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   335
                    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
   336
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   337
            assert(names[RETURN_CONV] == null);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   338
            names[RETURN_CONV] = conv;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   339
            assert(RETURN_CONV == names.length-1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   340
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   341
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   342
        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
   343
        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
   344
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   345
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   346
    static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   347
                                            boolean strict, boolean monobox) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   348
        final int INARG_COUNT = srcType.parameterCount();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   349
        Object[] convSpecs = new Object[INARG_COUNT+1];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   350
        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
   351
            boolean isRet = (i == INARG_COUNT);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   352
            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
   353
            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
   354
            if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   355
                convSpecs[i] = valueConversion(src, dst, strict, monobox);
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
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   358
        return convSpecs;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   359
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   360
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   361
                                            boolean strict) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   362
        return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   363
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   364
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   365
    /**
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   366
     * 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
   367
     * 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
   368
     * 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
   369
     * Return void.class if void is involved.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   370
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   371
    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
   372
        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
   373
        if (dst == void.class)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   374
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   375
        MethodHandle fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   376
        if (src.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   377
            if (src == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   378
                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
   379
            } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   380
                // 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
   381
                fn = ValueConversions.convertPrimitive(src, dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   382
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   383
                // Examples: int->Integer, boolean->Object, float->Number
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   384
                Wrapper wsrc = Wrapper.forPrimitiveType(src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   385
                fn = ValueConversions.boxExact(wsrc);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   386
                assert(fn.type().parameterType(0) == wsrc.primitiveType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   387
                assert(fn.type().returnType() == wsrc.wrapperType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   388
                if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   389
                    // 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
   390
                    MethodType mt = MethodType.methodType(dst, src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   391
                    if (strict)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   392
                        fn = fn.asType(mt);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   393
                    else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   394
                        fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   395
                }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   396
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   397
        } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   398
            Wrapper wdst = Wrapper.forPrimitiveType(dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   399
            if (monobox || src == wdst.wrapperType()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   400
                // Use a strongly-typed unboxer, if possible.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   401
                fn = ValueConversions.unboxExact(wdst, strict);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   402
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   403
                // 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
   404
                // must include additional conversions
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   405
                // 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
   406
                fn = (strict
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   407
                        ? ValueConversions.unboxWiden(wdst)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   408
                        : ValueConversions.unboxCast(wdst));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   409
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   410
        } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   411
            // Simple reference conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   412
            // 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
   413
            // 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
   414
            // will pass the cast conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   415
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   416
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   417
        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
   418
        return fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   419
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   420
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   421
    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
   422
        MethodType type = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   423
        int last = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   424
        if (type.parameterType(last) != arrayType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   425
            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
   426
        target = target.asFixedArity();  // make sure this attribute is turned off
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   427
        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
   428
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   429
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   430
    private static final class AsVarargsCollector extends DelegatingMethodHandle {
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   431
        private final MethodHandle target;
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   432
        private final Class<?> arrayType;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   433
        private @Stable MethodHandle asCollectorCache;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   434
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   435
        AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   436
            this(target.type(), target, arrayType);
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   437
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   438
        AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   439
            super(type, target);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   440
            this.target = target;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   441
            this.arrayType = arrayType;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   442
            this.asCollectorCache = target.asCollector(arrayType, 0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   443
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   444
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   445
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   446
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   447
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   448
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   449
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   450
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   451
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   452
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   453
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   454
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   455
        @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   456
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   457
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   458
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   459
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   460
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   461
        MethodHandle setVarargs(MemberName member) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   462
            if (member.isVarargs())  return this;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   463
            return asFixedArity();
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   464
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   465
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   466
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   467
        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
   468
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   469
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   470
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   471
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   472
                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
   473
                // 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
   474
                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
   475
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   476
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   477
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   478
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   479
                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
   480
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   481
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   482
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   483
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   484
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   485
                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
   486
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   487
                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
   488
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   489
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   490
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   491
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   492
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   493
        @Override
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   494
        boolean viewAsTypeChecks(MethodType newType, boolean strict) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   495
            super.viewAsTypeChecks(newType, true);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   496
            if (strict) return true;
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   497
            // extra assertion for non-strict checks:
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   498
            assert (type().lastParameterType().getComponentType()
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   499
                    .isAssignableFrom(
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   500
                            newType.lastParameterType().getComponentType()))
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   501
                    : Arrays.asList(this, newType);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   502
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   503
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   504
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   505
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   506
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   507
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   508
                                            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
   509
        MethodType targetType = target.type();
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
        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
   512
            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
   513
            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
   514
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   515
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   516
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   517
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   518
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   519
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   520
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   521
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   522
        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
   523
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   524
        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
   525
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   526
        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
   527
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   528
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   529
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   530
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   531
                Name array = names[argIndex];
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   532
                names[nameCursor++] = new Name(NF_checkSpreadArgument, array, spreadArgCount);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   533
                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
   534
                    indexes[i] = nameCursor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   535
                    names[nameCursor++] = new Name(aload, array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   536
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   537
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   538
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   539
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   540
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
        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
   542
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   543
        // 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
   544
        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
   545
        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
   546
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   547
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   548
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   549
        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
   550
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   551
        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
   552
        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
   553
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   554
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   555
    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
   556
        if (av == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   557
            if (n == 0)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   558
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   559
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   560
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   561
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   562
            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
   563
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   564
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   565
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   566
        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
   567
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   568
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   569
    /** 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
   570
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   571
                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
   572
        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
   573
        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
   574
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   575
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   576
        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
   577
        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
   578
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   579
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   580
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   581
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   582
        // 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
   583
        // 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
   584
        // 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
   585
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   586
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   587
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   588
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   589
        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
   590
        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
   591
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   592
        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
   593
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   594
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   595
        // 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
   596
        // 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
   597
        // 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
   598
        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
   599
        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
   600
        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
   601
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   602
        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
   603
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   604
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   605
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   606
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   607
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   608
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   609
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   610
            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
   611
            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
   612
        }
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
        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
   615
        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
   616
        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
   617
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   618
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   619
        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
   620
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   621
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   622
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   623
    @LambdaForm.Hidden
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   624
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   625
    MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   626
        if (testResult) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   627
            return target;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   628
        } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   629
            return fallback;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   630
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   631
    }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   632
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   633
    // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   634
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   635
    @jdk.internal.HotSpotIntrinsicCandidate
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   636
    static
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   637
    boolean profileBoolean(boolean result, int[] counters) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   638
        // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   639
        int idx = result ? 1 : 0;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   640
        try {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   641
            counters[idx] = Math.addExact(counters[idx], 1);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   642
        } catch (ArithmeticException e) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   643
            // Avoid continuous overflow by halving the problematic count.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   644
            counters[idx] = counters[idx] / 2;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   645
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   646
        return result;
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   647
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   648
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   649
    // Intrinsified by C2. Returns true if obj is a compile-time constant.
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   650
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   651
    @jdk.internal.HotSpotIntrinsicCandidate
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   652
    static
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   653
    boolean isCompileConstant(Object obj) {
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   654
        return false;
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   655
    }
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   656
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   657
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   658
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   659
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   660
                                   MethodHandle fallback) {
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   661
        MethodType type = target.type();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   662
        assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   663
        MethodType basicType = type.basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   664
        LambdaForm form = makeGuardWithTestForm(basicType);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   665
        BoundMethodHandle mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   666
        try {
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   667
            if (PROFILE_GWT) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   668
                int[] counts = new int[2];
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   669
                mh = (BoundMethodHandle)
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   670
                        BoundMethodHandle.speciesData_LLLL().constructor().invokeBasic(type, form,
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   671
                                (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   672
            } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   673
                mh = (BoundMethodHandle)
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   674
                        BoundMethodHandle.speciesData_LLL().constructor().invokeBasic(type, form,
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   675
                                (Object) test, (Object) profile(target), (Object) profile(fallback));
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   676
            }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   677
        } catch (Throwable ex) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   678
            throw uncaughtException(ex);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   679
        }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   680
        assert(mh.type() == type);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   681
        return mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   682
    }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   683
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   684
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   685
    static
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   686
    MethodHandle profile(MethodHandle target) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   687
        if (DONT_INLINE_THRESHOLD >= 0) {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   688
            return makeBlockInliningWrapper(target);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   689
        } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   690
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   691
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   692
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   693
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   694
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   695
     * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   696
     * Corresponding LambdaForm has @DontInline when compiled into bytecode.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   697
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   698
    static
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   699
    MethodHandle makeBlockInliningWrapper(MethodHandle target) {
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   700
        LambdaForm lform;
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   701
        if (DONT_INLINE_THRESHOLD > 0) {
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   702
            lform = PRODUCE_BLOCK_INLINING_FORM.apply(target);
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   703
        } else {
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   704
            lform = PRODUCE_REINVOKER_FORM.apply(target);
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   705
        }
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   706
        return new CountingWrapper(target, lform,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   707
                PRODUCE_BLOCK_INLINING_FORM, PRODUCE_REINVOKER_FORM,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   708
                                   DONT_INLINE_THRESHOLD);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   709
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   710
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   711
    /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   712
    private static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   713
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   714
        public LambdaForm apply(MethodHandle target) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   715
            return DelegatingMethodHandle.makeReinvokerForm(target,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   716
                               MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, "reinvoker.dontInline", false,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   717
                               DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   718
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   719
    };
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   720
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   721
    /** Constructs simple reinvoker lambda form for a particular method handle */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   722
    private static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   723
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   724
        public LambdaForm apply(MethodHandle target) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   725
            return DelegatingMethodHandle.makeReinvokerForm(target,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   726
                    MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   727
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   728
    };
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   729
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   730
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   731
     * Counting method handle. It has 2 states: counting and non-counting.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   732
     * It is in counting state for the first n invocations and then transitions to non-counting state.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   733
     * Behavior in counting and non-counting states is determined by lambda forms produced by
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   734
     * countingFormProducer & nonCountingFormProducer respectively.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   735
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   736
    static class CountingWrapper extends DelegatingMethodHandle {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   737
        private final MethodHandle target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   738
        private int count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   739
        private Function<MethodHandle, LambdaForm> countingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   740
        private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   741
        private volatile boolean isCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   742
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   743
        private CountingWrapper(MethodHandle target, LambdaForm lform,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   744
                                Function<MethodHandle, LambdaForm> countingFromProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   745
                                Function<MethodHandle, LambdaForm> nonCountingFormProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   746
                                int count) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   747
            super(target.type(), lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   748
            this.target = target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   749
            this.count = count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   750
            this.countingFormProducer = countingFromProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   751
            this.nonCountingFormProducer = nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   752
            this.isCounting = (count > 0);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   753
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   754
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   755
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   756
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   757
        protected MethodHandle getTarget() {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   758
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   759
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   760
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   761
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   762
        public MethodHandle asTypeUncached(MethodType newType) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   763
            MethodHandle newTarget = target.asType(newType);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   764
            MethodHandle wrapper;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   765
            if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   766
                LambdaForm lform;
29022
31bbb5dcc603 8071788: BlockInliningWrapper.asType() is broken
vlivanov
parents: 29021
diff changeset
   767
                lform = countingFormProducer.apply(newTarget);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   768
                wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   769
            } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   770
                wrapper = newTarget; // no need for a counting wrapper anymore
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   771
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   772
            return (asTypeCache = wrapper);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   773
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   774
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   775
        boolean countDown() {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   776
            int c = count;
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   777
            if (c <= 1) {
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   778
                // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   779
                if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   780
                    isCounting = false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   781
                    return true;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   782
                } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   783
                    return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   784
                }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   785
            } else {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   786
                count = c - 1;
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   787
                return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   788
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   789
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   790
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   791
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   792
        static void maybeStopCounting(Object o1) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   793
             CountingWrapper wrapper = (CountingWrapper) o1;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   794
             if (wrapper.countDown()) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   795
                 // Reached invocation threshold. Replace counting behavior with a non-counting one.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   796
                 LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   797
                 lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   798
                 wrapper.updateForm(lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   799
             }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   800
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   801
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   802
        static final NamedFunction NF_maybeStopCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   803
        static {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   804
            Class<?> THIS_CLASS = CountingWrapper.class;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   805
            try {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   806
                NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   807
            } catch (ReflectiveOperationException ex) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   808
                throw newInternalError(ex);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   809
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   810
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   811
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   812
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   813
    static
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   814
    LambdaForm makeGuardWithTestForm(MethodType basicType) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   815
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   816
        if (lform != null)  return lform;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   817
        final int THIS_MH      = 0;  // the BMH_LLL
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   818
        final int ARG_BASE     = 1;  // start of incoming arguments
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   819
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   820
        int nameCursor = ARG_LIMIT;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   821
        final int GET_TEST     = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   822
        final int GET_TARGET   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   823
        final int GET_FALLBACK = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   824
        final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   825
        final int CALL_TEST    = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   826
        final int PROFILE      = (GET_COUNTERS != -1) ? nameCursor++ : -1;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   827
        final int TEST         = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   828
        final int SELECT_ALT   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   829
        final int CALL_TARGET  = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   830
        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
   831
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   832
        MethodType lambdaType = basicType.invokerType();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   833
        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
   834
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   835
        BoundMethodHandle.SpeciesData data =
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   836
                (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   837
                                     : BoundMethodHandle.speciesData_LLL();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   838
        names[THIS_MH] = names[THIS_MH].withConstraint(data);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   839
        names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   840
        names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   841
        names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   842
        if (GET_COUNTERS != -1) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   843
            names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   844
        }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   845
        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
   846
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   847
        // call test
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   848
        MethodType testType = basicType.changeReturnType(boolean.class).basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   849
        invokeArgs[0] = names[GET_TEST];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   850
        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
   851
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   852
        // profile branch
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   853
        if (PROFILE != -1) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   854
            names[PROFILE] = new Name(NF_profileBoolean, names[CALL_TEST], names[GET_COUNTERS]);
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   855
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   856
        // call selectAlternative
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   857
        names[SELECT_ALT] = new Name(getConstantHandle(MH_selectAlternative), names[TEST], 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
   858
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   859
        // call target or fallback
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   860
        invokeArgs[0] = names[SELECT_ALT];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   861
        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
   862
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   863
        lform = new LambdaForm("guard", lambdaType.parameterCount(), names, /*forceInline=*/true);
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   864
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   865
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   866
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   867
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   868
    /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
   869
     * The LambdaForm shape for catchException combinator is the following:
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   870
     * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   871
     *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   872
     *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   873
     *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   874
     *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   875
     *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   876
     *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   877
     *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   878
     *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   879
     *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   880
     * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   881
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   882
     * 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
   883
     * 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
   884
     * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   885
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   886
     * 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
   887
     * among catchException combinators with the same basic type.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   888
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   889
    private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   890
        MethodType lambdaType = basicType.invokerType();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   891
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   892
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   893
        if (lform != null) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   894
            return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   895
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   896
        final int THIS_MH      = 0;  // the BMH_LLLLL
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   897
        final int ARG_BASE     = 1;  // start of incoming arguments
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   898
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   899
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   900
        int nameCursor = ARG_LIMIT;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   901
        final int GET_TARGET       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   902
        final int GET_CLASS        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   903
        final int GET_CATCHER      = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   904
        final int GET_COLLECT_ARGS = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   905
        final int GET_UNBOX_RESULT = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   906
        final int BOXED_ARGS       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   907
        final int TRY_CATCH        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   908
        final int UNBOX_RESULT     = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   909
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   910
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   911
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   912
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
26479
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26476
diff changeset
   913
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   914
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   915
        names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   916
        names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   917
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   918
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   919
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   920
        // FIXME: rework argument boxing/result unboxing logic for LF interpretation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   921
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   922
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   923
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   924
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   925
        Object[] args = new Object[invokeBasic.type().parameterCount()];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   926
        args[0] = names[GET_COLLECT_ARGS];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   927
        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
   928
        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
   929
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   930
        // 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
   931
        Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   932
        names[TRY_CATCH] = new Name(NF_guardWithCatch, gwcArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   933
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   934
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   935
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   936
        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
   937
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   938
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   939
        lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   940
24696
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 24572
diff changeset
   941
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   942
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   943
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   944
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   945
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   946
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   947
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   948
        MethodType type = target.type();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   949
        LambdaForm form = makeGuardWithCatchForm(type.basicType());
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   950
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
   951
        // Prepare auxiliary method handles used during LambdaForm interpretation.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   952
        // Box arguments and wrap them into Object[]: ValueConversions.array().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   953
        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
   954
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   955
        // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   956
        MethodHandle unboxResult;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   957
        Class<?> rtype = type.returnType();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   958
        if (rtype.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   959
            if (rtype == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   960
                unboxResult = ValueConversions.ignore();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   961
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   962
                Wrapper w = Wrapper.forPrimitiveType(type.returnType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   963
                unboxResult = ValueConversions.unboxExact(w);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   964
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   965
        } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   966
            unboxResult = MethodHandles.identity(Object.class);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   967
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   968
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   969
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   970
        BoundMethodHandle mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   971
        try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   972
            mh = (BoundMethodHandle)
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   973
                    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
   974
                                                   (Object) collectArgs, (Object) unboxResult);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   975
        } catch (Throwable ex) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   976
            throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   977
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   978
        assert(mh.type() == type);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   979
        return mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   980
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   981
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   982
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   983
     * Intrinsified during LambdaForm compilation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   984
     * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   985
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   986
    @LambdaForm.Hidden
23059
4aca6147df88 8036732: Fix raw warning in java.lang.invoke.MethodHandleImpl.java
darcy
parents: 23039
diff changeset
   987
    static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   988
                                 Object... av) throws Throwable {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   989
        // 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
   990
        try {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   991
            return target.asFixedArity().invokeWithArguments(av);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   992
        } catch (Throwable t) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   993
            if (!exType.isInstance(t)) throw t;
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
   994
            return catcher.asFixedArity().invokeWithArguments(prepend(t, av));
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   995
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   996
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   997
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   998
    /** 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
   999
    @LambdaForm.Hidden
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1000
    private static Object[] prepend(Object elem, Object[] array) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1001
        Object[] newArray = new Object[array.length+1];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1002
        newArray[0] = elem;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1003
        System.arraycopy(array, 0, newArray, 1, array.length);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1004
        return newArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1005
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1006
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1007
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1008
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1009
        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
  1010
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1011
        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
  1012
            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
  1013
            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
  1014
            return mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1015
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1016
        return makePairwiseConvert(NF_throwException.resolvedHandle(), type, false, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1017
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1018
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1019
    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
  1020
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1021
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1022
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1023
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1024
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1025
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1026
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1027
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1028
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1029
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1030
        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
  1031
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1032
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1033
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1034
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1035
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1036
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1037
            throw new InternalError(method.toString());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1038
        mh = mh.withInternalMemberName(method, false);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1039
        mh = mh.asVarargsCollector(Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1040
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1041
        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
  1042
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1043
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1044
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1045
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1046
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1047
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1048
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1049
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1050
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1051
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1052
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1053
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1054
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1055
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1056
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1057
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1058
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1059
    // 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
  1060
    private static class BindCaller {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1061
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1062
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1063
            // Do not use this function to inject calls into system classes.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1064
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1065
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1066
                       hostClass.isPrimitive() ||
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1067
                       hostClass.getName().startsWith("java.") ||
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1068
                       hostClass.getName().startsWith("sun."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1069
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1070
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1071
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1072
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1073
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1074
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1075
            return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1076
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1077
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1078
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1079
            Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1080
            if (hostClass.getClassLoader() != bcc.getClassLoader())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1081
                throw new InternalError(hostClass.getName()+" (CL)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1082
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1083
                if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1084
                    throw new InternalError(hostClass.getName()+" (PD)");
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1085
            } catch (SecurityException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1086
                // Self-check was blocked by security manager.  This is OK.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1087
                // In fact the whole try body could be turned into an assertion.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1088
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1089
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1090
                MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1091
                init.invokeExact();  // force initialization of the class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1092
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1093
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1094
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1095
            MethodHandle bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1096
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1097
                MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1098
                bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1099
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1100
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1101
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1102
            // 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
  1103
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1104
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1105
                Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1106
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1107
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1108
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1109
            return bccInvoker;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1110
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1111
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1112
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1113
                return makeInjectedInvoker(hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1114
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1115
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1116
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1117
        // 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
  1118
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1119
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1120
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1121
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1122
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1123
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1124
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1125
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1126
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1127
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1128
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1129
        // Undo the adapter effect of prepareForInvoker:
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1130
        private static MethodHandle restoreToType(MethodHandle vamh,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1131
                                                  MethodHandle original,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1132
                                                  Class<?> hostClass) {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1133
            MethodType type = original.type();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1134
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1135
            MemberName member = original.internalMemberName();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1136
            mh = mh.asType(type);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1137
            mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1138
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1139
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1140
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1141
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1142
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1143
            final Class<?> THIS_CLASS = BindCaller.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1144
            assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1145
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1146
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1147
                    .findStatic(THIS_CLASS, "checkCallerClass",
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1148
                                MethodType.methodType(boolean.class, Class.class, Class.class));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1149
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1150
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1151
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1152
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1153
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1154
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
  1155
        @CallerSensitive
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1156
        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
  1157
            // 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
  1158
            // correct to ask for the immediate caller here.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
  1159
            Class<?> actual = Reflection.getCallerClass();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1160
            if (actual != expected && actual != expected2)
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1161
                throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1162
                                        +(expected == expected2 ? "" : ", or else "+expected2.getName()));
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1163
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1164
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1165
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1166
        private static final byte[] T_BYTES;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1167
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1168
            final Object[] values = {null};
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29022
diff changeset
  1169
            AccessController.doPrivileged(new PrivilegedAction<>() {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1170
                    public Void run() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1171
                        try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1172
                            Class<T> tClass = T.class;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1173
                            String tName = tClass.getName();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1174
                            String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1175
                            java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1176
                            int len = uconn.getContentLength();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1177
                            byte[] bytes = new byte[len];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1178
                            try (java.io.InputStream str = uconn.getInputStream()) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1179
                                int nr = str.read(bytes);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1180
                                if (nr != len)  throw new java.io.IOException(tResource);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1181
                            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1182
                            values[0] = bytes;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1183
                        } catch (java.io.IOException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1184
                            throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1185
                        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1186
                        return null;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1187
                    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1188
                });
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1189
            T_BYTES = (byte[]) values[0];
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1190
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1191
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1192
        // The following class is used as a template for Unsafe.defineAnonymousClass:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1193
        private static class T {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1194
            static void init() { }  // side effect: initializes this class
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1195
            static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1196
                return vamh.invokeExact(args);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1197
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1198
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1199
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1200
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1201
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1202
    /** 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
  1203
    private static final class WrappedMember extends DelegatingMethodHandle {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1204
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1205
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1206
        private final Class<?> callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1207
        private final boolean isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1208
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1209
        private WrappedMember(MethodHandle target, MethodType type,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1210
                              MemberName member, boolean isInvokeSpecial,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1211
                              Class<?> callerClass) {
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1212
            super(type, target);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1213
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1214
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1215
            this.callerClass = callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1216
            this.isInvokeSpecial = isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1217
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1218
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1219
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1220
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1221
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1222
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1223
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1224
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1225
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1226
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1227
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1228
        boolean isInvokeSpecial() {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1229
            return isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1230
        }
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1231
        @Override
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1232
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1233
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1234
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1235
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1236
        public MethodHandle asTypeUncached(MethodType newType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1237
            // This MH is an alias for target, except for the MemberName
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1238
            // Drop the MemberName if there is any conversion.
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1239
            return asTypeCache = target.asType(newType);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1240
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1241
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1242
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1243
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1244
        if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1245
            return target;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1246
        return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1247
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1248
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1249
    /** Intrinsic IDs */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1250
    /*non-public*/
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1251
    enum Intrinsic {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1252
        SELECT_ALTERNATIVE,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1253
        GUARD_WITH_CATCH,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1254
        NEW_ARRAY,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1255
        ARRAY_LOAD,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1256
        ARRAY_STORE,
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1257
        IDENTITY,
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  1258
        ZERO,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1259
        NONE // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1260
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1261
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1262
    /** Mark arbitrary method handle as intrinsic.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1263
     * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1264
    private static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1265
        private final MethodHandle target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1266
        private final Intrinsic intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1267
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1268
        IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1269
            super(target.type(), target);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1270
            this.target = target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1271
            this.intrinsicName = intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1272
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1273
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1274
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1275
        protected MethodHandle getTarget() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1276
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1277
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1278
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1279
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1280
        Intrinsic intrinsicName() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1281
            return intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1282
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1283
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1284
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1285
        public MethodHandle asTypeUncached(MethodType newType) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1286
            // 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
  1287
            // Drop the name if there is any conversion.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1288
            return asTypeCache = target.asType(newType);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1289
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1290
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1291
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1292
        String internalProperties() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1293
            return super.internalProperties() +
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1294
                    "\n& Intrinsic="+intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1295
        }
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1296
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1297
        @Override
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1298
        public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1299
            if (intrinsicName == Intrinsic.IDENTITY) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1300
                MethodType resultType = type().asCollectorType(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1301
                MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1302
                return newArray.asType(resultType);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1303
            }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1304
            return super.asCollector(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1305
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1306
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1307
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1308
    static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1309
        if (intrinsicName == target.intrinsicName())
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1310
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1311
        return new IntrinsicMethodHandle(target, intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1312
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1313
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1314
    static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1315
        return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1316
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1317
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1318
    /// Collection of multiple arguments.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1319
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1320
    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
  1321
        MethodType type = MethodType.genericMethodType(nargs)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1322
                .changeReturnType(rtype)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1323
                .insertParameterTypes(0, ptypes);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1324
        try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1325
            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
  1326
        } catch (ReflectiveOperationException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1327
            return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1328
        }
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
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1331
    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
  1332
    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
  1333
    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
  1334
    private static Object[] array(Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1335
                { return makeArray(a0); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1336
    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
  1337
                { return makeArray(a0, a1); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1338
    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
  1339
                { return makeArray(a0, a1, a2); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1340
    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
  1341
                { return makeArray(a0, a1, a2, a3); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1342
    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
  1343
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1344
                { 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
  1345
    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
  1346
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1347
                { 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
  1348
    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
  1349
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1350
                { 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
  1351
    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
  1352
                                  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
  1353
                { 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
  1354
    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
  1355
                                  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
  1356
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1357
                { 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
  1358
    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
  1359
                                  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
  1360
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1361
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
27350
94a990d3754a 8062180: MethodHandleImpl.makeArrays throws and swallows java.lang.NoSuchFieldError in normal flow
redestad
parents: 27295
diff changeset
  1362
94a990d3754a 8062180: MethodHandleImpl.makeArrays throws and swallows java.lang.NoSuchFieldError in normal flow
redestad
parents: 27295
diff changeset
  1363
    private static final int ARRAYS_COUNT = 11;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1364
    private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1365
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1366
    // filling versions of the above:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1367
    // 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
  1368
    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
  1369
        Object[] a = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1370
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1371
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1372
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1373
    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
  1374
        Object[] a = Arrays.copyOf(example, len);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1375
        assert(a.getClass() != Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1376
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1377
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1378
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1379
    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
  1380
        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
  1381
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1382
    // 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
  1383
    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
  1384
                { fillWithArguments(a, pos, a0); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1385
    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
  1386
                { 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
  1387
    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
  1388
                { 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
  1389
    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
  1390
                { 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
  1391
    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
  1392
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1393
                { 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
  1394
    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
  1395
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1396
                { 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
  1397
    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
  1398
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1399
                { 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
  1400
    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
  1401
                                  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
  1402
                { 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
  1403
    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
  1404
                                  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
  1405
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1406
                { 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
  1407
    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
  1408
                                  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
  1409
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1410
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1411
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1412
    private static final int FILL_ARRAYS_COUNT = 11; // current number of fillArray methods
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1413
    private static final @Stable MethodHandle[] FILL_ARRAYS = new MethodHandle[FILL_ARRAYS_COUNT];
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1414
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1415
    private static MethodHandle getFillArray(int count) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1416
        assert (count > 0 && count < FILL_ARRAYS_COUNT);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1417
        MethodHandle mh = FILL_ARRAYS[count];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1418
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1419
            return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1420
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1421
        mh = findCollector("fillArray", count, Object[].class, Integer.class, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1422
        FILL_ARRAYS[count] = mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1423
        return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1424
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1425
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1426
    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
  1427
        Object a = w.makeArray(boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1428
        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
  1429
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1430
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1431
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1432
    /** 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
  1433
     *  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
  1434
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1435
    static MethodHandle varargsArray(int nargs) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1436
        MethodHandle mh = ARRAYS[nargs];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1437
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1438
            return mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1439
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1440
        if (nargs < ARRAYS_COUNT) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1441
            mh = findCollector("array", nargs, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1442
        } else {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1443
            mh = buildVarargsArray(getConstantHandle(MH_fillNewArray),
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1444
                    getConstantHandle(MH_arrayIdentity), nargs);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1445
        }
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1446
        assert(assertCorrectArity(mh, nargs));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1447
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1448
        return ARRAYS[nargs] = mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1449
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1450
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1451
    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
  1452
        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
  1453
        return true;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1454
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1455
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1456
    // Array identity function (used as getConstantHandle(MH_arrayIdentity)).
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1457
    static <T> T[] identity(T[] x) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1458
        return x;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1459
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1460
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1461
    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
  1462
        // 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
  1463
        //   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
  1464
        // 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
  1465
        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
  1466
        int rightLen = nargs - leftLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1467
        MethodHandle leftCollector = newArray.bindTo(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1468
        leftCollector = leftCollector.asCollector(Object[].class, leftLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1469
        MethodHandle mh = finisher;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1470
        if (rightLen > 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1471
            MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1472
            if (mh.equals(getConstantHandle(MH_arrayIdentity)))
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1473
                mh = rightFiller;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1474
            else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1475
                mh = MethodHandles.collectArguments(mh, 0, rightFiller);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1476
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1477
        if (mh.equals(getConstantHandle(MH_arrayIdentity)))
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1478
            mh = leftCollector;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1479
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1480
            mh = MethodHandles.collectArguments(mh, 0, leftCollector);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1481
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1482
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1483
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1484
    private static final int LEFT_ARGS = FILL_ARRAYS_COUNT - 1;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1485
    private static final @Stable MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1];
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1486
    /** 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
  1487
     *  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
  1488
     *  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
  1489
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1490
    private static MethodHandle fillToRight(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1491
        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
  1492
        if (filler != null)  return filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1493
        filler = buildFiller(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1494
        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
  1495
        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
  1496
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1497
    private static MethodHandle buildFiller(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1498
        if (nargs <= LEFT_ARGS)
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1499
            return getConstantHandle(MH_arrayIdentity);  // no args to fill; return the array unchanged
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1500
        // 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
  1501
        final int CHUNK = LEFT_ARGS;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1502
        int rightLen = nargs % CHUNK;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1503
        int midLen = nargs - rightLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1504
        if (rightLen == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1505
            midLen = nargs - (rightLen = CHUNK);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1506
            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
  1507
                // 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
  1508
                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
  1509
                    if (j > LEFT_ARGS)  fillToRight(j);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1510
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1511
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1512
        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
  1513
        assert(rightLen > 0);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1514
        MethodHandle midFill = fillToRight(midLen);  // recursive fill
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1515
        MethodHandle rightFill = getFillArray(rightLen).bindTo(midLen);  // [midLen..nargs-1]
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1516
        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
  1517
        assert(rightFill.type().parameterCount() == 1 + rightLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1518
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1519
        // Combine the two fills:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1520
        //   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
  1521
        // 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
  1522
        //   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
  1523
        if (midLen == LEFT_ARGS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1524
            return rightFill;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1525
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1526
            return MethodHandles.collectArguments(rightFill, 0, midFill);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1527
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1528
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1529
    // Type-polymorphic version of varargs maker.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1530
    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
  1531
        = new ClassValue<MethodHandle[]>() {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1532
            @Override
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1533
            protected MethodHandle[] computeValue(Class<?> type) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1534
                return new MethodHandle[256];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1535
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1536
    };
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1537
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1538
    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
  1539
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1540
    /** 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
  1541
     *  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
  1542
     *  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
  1543
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1544
    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
  1545
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1546
        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
  1547
        // 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
  1548
        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
  1549
            int slots = nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1550
            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
  1551
            if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1552
                slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1553
            if (slots > MAX_ARRAY_SLOTS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1554
                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
  1555
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1556
        if (elemType == Object.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1557
            return varargsArray(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1558
        // 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
  1559
        MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1560
        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
  1561
        if (mh != null)  return mh;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1562
        if (nargs == 0) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1563
            Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1564
            mh = MethodHandles.constant(arrayType, example);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1565
        } else if (elemType.isPrimitive()) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1566
            MethodHandle builder = getConstantHandle(MH_fillNewArray);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1567
            MethodHandle producer = buildArrayProducer(arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1568
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1569
        } else {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1570
            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
  1571
            Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1572
            MethodHandle builder = getConstantHandle(MH_fillNewTypedArray).bindTo(example);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1573
            MethodHandle producer = getConstantHandle(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
  1574
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1575
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1576
        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
  1577
        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
  1578
        assert(assertCorrectArity(mh, nargs));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1579
        if (nargs < cache.length)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1580
            cache[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1581
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1582
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1583
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1584
    private static MethodHandle buildArrayProducer(Class<?> arrayType) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1585
        Class<?> elemType = arrayType.getComponentType();
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1586
        assert(elemType.isPrimitive());
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1587
        return getConstantHandle(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
  1588
    }
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1589
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1590
    /*non-public*/ static void assertSame(Object mh1, Object mh2) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1591
        if (mh1 != mh2) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1592
            String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1593
                    mh1, ((MethodHandle)mh1).form,
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1594
                    mh2, ((MethodHandle)mh2).form);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1595
            throw newInternalError(msg);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1596
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1597
    }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1598
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1599
    // Local constant functions:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1600
    /*non-public*/ static final NamedFunction
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1601
        NF_checkSpreadArgument,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1602
        NF_guardWithCatch,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1603
        NF_throwException,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1604
        NF_profileBoolean;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1605
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1606
    static {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1607
        try {
33846
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1608
            NF_checkSpreadArgument = new NamedFunction(MethodHandleImpl.class
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1609
                    .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1610
            NF_guardWithCatch = new NamedFunction(MethodHandleImpl.class
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1611
                    .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1612
                            MethodHandle.class, Object[].class));
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1613
            NF_throwException = new NamedFunction(MethodHandleImpl.class
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1614
                    .getDeclaredMethod("throwException", Throwable.class));
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1615
            NF_profileBoolean = new NamedFunction(MethodHandleImpl.class
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
  1616
                    .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1617
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1618
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1619
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1620
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1621
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1622
    // Indexes into constant method handles:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1623
    private static final int
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1624
            MH_cast                  =  0,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1625
            MH_selectAlternative     =  1,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1626
            MH_copyAsPrimitiveArray  =  2,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1627
            MH_fillNewTypedArray     =  3,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1628
            MH_fillNewArray          =  4,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1629
            MH_arrayIdentity         =  5,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1630
            MH_LIMIT                 =  6;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1631
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1632
    private static MethodHandle getConstantHandle(int idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1633
        MethodHandle handle = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1634
        if (handle != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1635
            return handle;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1636
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1637
        return setCachedHandle(idx, makeConstantHandle(idx));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1638
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1639
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1640
    private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1641
        // Simulate a CAS, to avoid racy duplication of results.
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1642
        MethodHandle prev = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1643
        if (prev != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1644
            return prev;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1645
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1646
        HANDLES[idx] = method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1647
        return method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1648
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1649
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1650
    // Local constant method handles:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1651
    private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1652
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1653
    private static MethodHandle makeConstantHandle(int idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1654
        try {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1655
            switch (idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1656
                case MH_cast:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1657
                    return IMPL_LOOKUP.findVirtual(Class.class, "cast",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1658
                            MethodType.methodType(Object.class, Object.class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1659
                case MH_copyAsPrimitiveArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1660
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "copyAsPrimitiveArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1661
                            MethodType.methodType(Object.class, Wrapper.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1662
                case MH_arrayIdentity:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1663
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "identity",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1664
                            MethodType.methodType(Object[].class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1665
                case MH_fillNewArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1666
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1667
                            MethodType.methodType(Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1668
                case MH_fillNewTypedArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1669
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewTypedArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1670
                            MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1671
                case MH_selectAlternative:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1672
                    return makeIntrinsic(IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1673
                            MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)),
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1674
                        Intrinsic.SELECT_ALTERNATIVE);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1675
            }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1676
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1677
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1678
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1679
        throw newInternalError("Unknown function index: " + idx);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1680
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1681
}