src/java.base/share/classes/java/lang/invoke/Invokers.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 51986 c1db377f6300
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
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
/*
51986
c1db377f6300 8200381: Typos in javadoc - missing verb "be" and alike
igerasim
parents: 49240
diff changeset
     2
 * Copyright (c) 2008, 2018, 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
35252
754852fba784 8144223: Move j.l.invoke.{ForceInline, DontInline, Stable} to jdk.internal.vm.annotation package
psandoz
parents: 33841
diff changeset
    28
import jdk.internal.vm.annotation.DontInline;
754852fba784 8144223: Move j.l.invoke.{ForceInline, DontInline, Stable} to jdk.internal.vm.annotation package
psandoz
parents: 33841
diff changeset
    29
import jdk.internal.vm.annotation.ForceInline;
754852fba784 8144223: Move j.l.invoke.{ForceInline, DontInline, Stable} to jdk.internal.vm.annotation package
psandoz
parents: 33841
diff changeset
    30
import jdk.internal.vm.annotation.Stable;
754852fba784 8144223: Move j.l.invoke.{ForceInline, DontInline, Stable} to jdk.internal.vm.annotation package
psandoz
parents: 33841
diff changeset
    31
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    32
import java.lang.reflect.Array;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    33
import java.util.Arrays;
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    34
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
    35
import static java.lang.invoke.MethodHandleStatics.*;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    36
import static java.lang.invoke.MethodHandleNatives.Constants.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    37
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    38
import static java.lang.invoke.LambdaForm.*;
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
    39
import static java.lang.invoke.LambdaForm.Kind.*;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    40
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    41
/**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
 * Construction and caching of often-used invokers.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    45
class Invokers {
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 23010
diff changeset
    46
    // exact type (sans leading target MH) for the outgoing call
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
    private final MethodType targetType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    49
    // Cached adapter information:
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    50
    private final @Stable MethodHandle[] invokers = new MethodHandle[INV_LIMIT];
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    51
    // Indexes into invokers:
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    52
    static final int
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    53
            INV_EXACT          =  0,  // MethodHandles.exactInvoker
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    54
            INV_GENERIC        =  1,  // MethodHandles.invoker (generic invocation)
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    55
            INV_BASIC          =  2,  // MethodHandles.basicInvoker
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    56
            INV_LIMIT          =  3;
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
    57
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    58
    /** Compute and cache information common to all collecting adapters
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    59
     *  that implement members of the erasure-family of the given erased type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    60
     */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    61
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    62
    Invokers(MethodType targetType) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
        this.targetType = targetType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    64
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    65
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    66
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    67
    MethodHandle exactInvoker() {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    68
        MethodHandle invoker = cachedInvoker(INV_EXACT);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    69
        if (invoker != null)  return invoker;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
    70
        invoker = makeExactOrGeneralInvoker(true);
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    71
        return setCachedInvoker(INV_EXACT, invoker);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    72
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    73
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    74
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    75
    MethodHandle genericInvoker() {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    76
        MethodHandle invoker = cachedInvoker(INV_GENERIC);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    77
        if (invoker != null)  return invoker;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    78
        invoker = makeExactOrGeneralInvoker(false);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    79
        return setCachedInvoker(INV_GENERIC, invoker);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    80
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    81
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    82
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
    83
    MethodHandle basicInvoker() {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    84
        MethodHandle invoker = cachedInvoker(INV_BASIC);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    85
        if (invoker != null)  return invoker;
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    86
        MethodType basicType = targetType.basicType();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    87
        if (basicType != targetType) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    88
            // double cache; not used significantly
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    89
            return setCachedInvoker(INV_BASIC, basicType.invokers().basicInvoker());
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    90
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    91
        invoker = basicType.form().cachedMethodHandle(MethodTypeForm.MH_BASIC_INV);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    92
        if (invoker == null) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    93
            MemberName method = invokeBasicMethod(basicType);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    94
            invoker = DirectMethodHandle.make(method);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    95
            assert(checkInvoker(invoker));
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    96
            invoker = basicType.form().setCachedMethodHandle(MethodTypeForm.MH_BASIC_INV, invoker);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    97
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    98
        return setCachedInvoker(INV_BASIC, invoker);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
    99
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   100
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   101
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   102
    MethodHandle varHandleMethodInvoker(VarHandle.AccessMode ak) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   103
        // TODO cache invoker
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   104
        return makeVarHandleMethodInvoker(ak, false);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   105
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   106
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   107
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   108
    MethodHandle varHandleMethodExactInvoker(VarHandle.AccessMode ak) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   109
        // TODO cache invoker
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   110
        return makeVarHandleMethodInvoker(ak, true);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   111
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   112
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   113
    private MethodHandle cachedInvoker(int idx) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   114
        return invokers[idx];
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   115
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   116
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   117
    private synchronized MethodHandle setCachedInvoker(int idx, final MethodHandle invoker) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   118
        // Simulate a CAS, to avoid racy duplication of results.
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   119
        MethodHandle prev = invokers[idx];
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   120
        if (prev != null)  return prev;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   121
        return invokers[idx] = invoker;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   122
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   123
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   124
    private MethodHandle makeExactOrGeneralInvoker(boolean isExact) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   125
        MethodType mtype = targetType;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   126
        MethodType invokerType = mtype.invokerType();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   127
        int which = (isExact ? MethodTypeForm.LF_EX_INVOKER : MethodTypeForm.LF_GEN_INVOKER);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   128
        LambdaForm lform = invokeHandleForm(mtype, false, which);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   129
        MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, mtype);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   130
        String whichName = (isExact ? "invokeExact" : "invoke");
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26468
diff changeset
   131
        invoker = invoker.withInternalMemberName(MemberName.makeMethodHandleInvoke(whichName, mtype), false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   132
        assert(checkInvoker(invoker));
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   133
        maybeCompileToBytecode(invoker);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   134
        return invoker;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   135
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   136
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   137
    private MethodHandle makeVarHandleMethodInvoker(VarHandle.AccessMode ak, boolean isExact) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   138
        MethodType mtype = targetType;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   139
        MethodType invokerType = mtype.insertParameterTypes(0, VarHandle.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   140
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   141
        LambdaForm lform = varHandleMethodInvokerHandleForm(ak, mtype, isExact);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   142
        VarHandle.AccessDescriptor ad = new VarHandle.AccessDescriptor(mtype, ak.at.ordinal(), ak.ordinal());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   143
        MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, ad);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   144
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   145
        invoker = invoker.withInternalMemberName(MemberName.makeVarHandleMethodInvoke(ak.methodName(), mtype), false);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   146
        assert(checkVarHandleInvoker(invoker));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   147
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   148
        maybeCompileToBytecode(invoker);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   149
        return invoker;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   150
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   151
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   152
    /** If the target type seems to be common enough, eagerly compile the invoker to bytecodes. */
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   153
    private void maybeCompileToBytecode(MethodHandle invoker) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   154
        final int EAGER_COMPILE_ARITY_LIMIT = 10;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   155
        if (targetType == targetType.erase() &&
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   156
            targetType.parameterCount() < EAGER_COMPILE_ARITY_LIMIT) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   157
            invoker.form.compileToBytecode();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   158
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   159
    }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   160
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   161
    // This next one is called from LambdaForm.NamedFunction.<init>.
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   162
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   163
    static MemberName invokeBasicMethod(MethodType basicType) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   164
        assert(basicType == basicType.basicType());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   165
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   166
            //Lookup.findVirtual(MethodHandle.class, name, type);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   167
            return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   168
        } catch (ReflectiveOperationException ex) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   169
            throw newInternalError("JVM cannot find invoker for "+basicType, ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   170
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   171
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   172
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   173
    private boolean checkInvoker(MethodHandle invoker) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   174
        assert(targetType.invokerType().equals(invoker.type()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   175
                : java.util.Arrays.asList(targetType, targetType.invokerType(), invoker);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   176
        assert(invoker.internalMemberName() == null ||
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   177
               invoker.internalMemberName().getMethodType().equals(targetType));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   178
        assert(!invoker.isVarargsCollector());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   179
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   180
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   181
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   182
    private boolean checkVarHandleInvoker(MethodHandle invoker) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   183
        MethodType invokerType = targetType.insertParameterTypes(0, VarHandle.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   184
        assert(invokerType.equals(invoker.type()))
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   185
                : java.util.Arrays.asList(targetType, invokerType, invoker);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   186
        assert(invoker.internalMemberName() == null ||
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   187
               invoker.internalMemberName().getMethodType().equals(targetType));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   188
        assert(!invoker.isVarargsCollector());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   189
        return true;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   190
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   191
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   192
    /**
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   193
     * Find or create an invoker which passes unchanged a given number of arguments
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   194
     * and spreads the rest from a trailing array argument.
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   195
     * The invoker target type is the post-spread type {@code (TYPEOF(uarg*), TYPEOF(sarg*))=>RT}.
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   196
     * All the {@code sarg}s must have a common type {@code C}.  (If there are none, {@code Object} is assumed.}
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   197
     * @param leadingArgCount the number of unchanged (non-spread) arguments
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   198
     * @return {@code invoker.invokeExact(mh, uarg*, C[]{sarg*}) := (RT)mh.invoke(uarg*, sarg*)}
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   199
     */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   200
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   201
    MethodHandle spreadInvoker(int leadingArgCount) {
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   202
        int spreadArgCount = targetType.parameterCount() - leadingArgCount;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   203
        MethodType postSpreadType = targetType;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   204
        Class<?> argArrayType = impliedRestargType(postSpreadType, leadingArgCount);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   205
        if (postSpreadType.parameterSlotCount() <= MethodType.MAX_MH_INVOKER_ARITY) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   206
            return genericInvoker().asSpreader(argArrayType, spreadArgCount);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   207
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   208
        // Cannot build a generic invoker here of type ginvoker.invoke(mh, a*[254]).
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   209
        // Instead, factor sinvoker.invoke(mh, a) into ainvoker.invoke(filter(mh), a)
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   210
        // where filter(mh) == mh.asSpreader(Object[], spreadArgCount)
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   211
        MethodType preSpreadType = postSpreadType
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   212
            .replaceParameterTypes(leadingArgCount, postSpreadType.parameterCount(), argArrayType);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   213
        MethodHandle arrayInvoker = MethodHandles.invoker(preSpreadType);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   214
        MethodHandle makeSpreader = MethodHandles.insertArguments(Lazy.MH_asSpreader, 1, argArrayType, spreadArgCount);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   215
        return MethodHandles.filterArgument(arrayInvoker, 0, makeSpreader);
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   216
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   217
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   218
    private static Class<?> impliedRestargType(MethodType restargType, int fromPos) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   219
        if (restargType.isGeneric())  return Object[].class;  // can be nothing else
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   220
        int maxPos = restargType.parameterCount();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   221
        if (fromPos >= maxPos)  return Object[].class;  // reasonable default
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   222
        Class<?> argType = restargType.parameterType(fromPos);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   223
        for (int i = fromPos+1; i < maxPos; i++) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   224
            if (argType != restargType.parameterType(i))
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   225
                throw newIllegalArgumentException("need homogeneous rest arguments", restargType);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   226
        }
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   227
        if (argType == Object.class)  return Object[].class;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   228
        return Array.newInstance(argType, 0).getClass();
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   229
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   230
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   231
    public String toString() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   232
        return "Invokers"+targetType;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   233
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   234
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   235
    static MemberName methodHandleInvokeLinkerMethod(String name,
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   236
                                                     MethodType mtype,
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   237
                                                     Object[] appendixResult) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   238
        int which;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   239
        switch (name) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   240
            case "invokeExact":  which = MethodTypeForm.LF_EX_LINKER; break;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   241
            case "invoke":       which = MethodTypeForm.LF_GEN_LINKER; break;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   242
            default:             throw new InternalError("not invoker: "+name);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   243
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   244
        LambdaForm lform;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   245
        if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   246
            lform = invokeHandleForm(mtype, false, which);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   247
            appendixResult[0] = mtype;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   248
        } else {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   249
            lform = invokeHandleForm(mtype, true, which);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   250
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   251
        return lform.vmentry;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   252
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   253
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   254
    // argument count to account for trailing "appendix value" (typically the mtype)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   255
    private static final int MH_LINKER_ARG_APPENDED = 1;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   256
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   257
    /** Returns an adapter for invokeExact or generic invoke, as a MH or constant pool linker.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   258
     * If !customized, caller is responsible for supplying, during adapter execution,
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   259
     * a copy of the exact mtype.  This is because the adapter might be generalized to
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   260
     * a basic type.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   261
     * @param mtype the caller's method type (either basic or full-custom)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   262
     * @param customized whether to use a trailing appendix argument (to carry the mtype)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   263
     * @param which bit-encoded 0x01 whether it is a CP adapter ("linker") or MHs.invoker value ("invoker");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   264
     *                          0x02 whether it is for invokeExact or generic invoke
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   265
     */
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   266
    static LambdaForm invokeHandleForm(MethodType mtype, boolean customized, int which) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   267
        boolean isCached;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   268
        if (!customized) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   269
            mtype = mtype.basicType();  // normalize Z to I, String to Object, etc.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   270
            isCached = true;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   271
        } else {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   272
            isCached = false;  // maybe cache if mtype == mtype.basicType()
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   273
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   274
        boolean isLinker, isGeneric;
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   275
        Kind kind;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   276
        switch (which) {
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   277
        case MethodTypeForm.LF_EX_LINKER:   isLinker = true;  isGeneric = false; kind = EXACT_LINKER; break;
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   278
        case MethodTypeForm.LF_EX_INVOKER:  isLinker = false; isGeneric = false; kind = EXACT_INVOKER; break;
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   279
        case MethodTypeForm.LF_GEN_LINKER:  isLinker = true;  isGeneric = true;  kind = GENERIC_LINKER; break;
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   280
        case MethodTypeForm.LF_GEN_INVOKER: isLinker = false; isGeneric = true;  kind = GENERIC_INVOKER; break;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   281
        default: throw new InternalError();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   282
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   283
        LambdaForm lform;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   284
        if (isCached) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   285
            lform = mtype.form().cachedLambdaForm(which);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   286
            if (lform != null)  return lform;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   287
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   288
        // exactInvokerForm (Object,Object)Object
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   289
        //   link with java.lang.invoke.MethodHandle.invokeBasic(MethodHandle,Object,Object)Object/invokeSpecial
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   290
        final int THIS_MH      = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   291
        final int CALL_MH      = THIS_MH + (isLinker ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   292
        final int ARG_BASE     = CALL_MH + 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   293
        final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount();
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   294
        final int INARG_LIMIT  = OUTARG_LIMIT + (isLinker && !customized ? 1 : 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   295
        int nameCursor = OUTARG_LIMIT;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   296
        final int MTYPE_ARG    = customized ? -1 : nameCursor++;  // might be last in-argument
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   297
        final int CHECK_TYPE   = nameCursor++;
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   298
        final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   299
        final int LINKER_CALL  = nameCursor++;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   300
        MethodType invokerFormType = mtype.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   301
        if (isLinker) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   302
            if (!customized)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   303
                invokerFormType = invokerFormType.appendParameterTypes(MemberName.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   304
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   305
            invokerFormType = invokerFormType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   306
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   307
        Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   308
        assert(names.length == nameCursor)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   309
                : Arrays.asList(mtype, customized, which, nameCursor, names.length);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   310
        if (MTYPE_ARG >= INARG_LIMIT) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   311
            assert(names[MTYPE_ARG] == null);
26479
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26471
diff changeset
   312
            BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26471
diff changeset
   313
            names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26471
diff changeset
   314
            NamedFunction getter = speciesData.getterFunction(0);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   315
            names[MTYPE_ARG] = new Name(getter, names[THIS_MH]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   316
            // else if isLinker, then MTYPE is passed in from the caller (e.g., the JVM)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   317
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   318
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   319
        // Make the final call.  If isGeneric, then prepend the result of type checking.
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   320
        MethodType outCallType = mtype.basicType();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   321
        Object[] outArgs = Arrays.copyOfRange(names, CALL_MH, OUTARG_LIMIT, Object[].class);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   322
        Object mtypeArg = (customized ? mtype : names[MTYPE_ARG]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   323
        if (!isGeneric) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   324
            names[CHECK_TYPE] = new Name(getFunction(NF_checkExactType), names[CALL_MH], mtypeArg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   325
            // mh.invokeExact(a*):R => checkExactType(mh, TYPEOF(a*:R)); mh.invokeBasic(a*)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   326
        } else {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   327
            names[CHECK_TYPE] = new Name(getFunction(NF_checkGenericType), names[CALL_MH], mtypeArg);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   328
            // mh.invokeGeneric(a*):R => checkGenericType(mh, TYPEOF(a*:R)).invokeBasic(a*)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   329
            outArgs[0] = names[CHECK_TYPE];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   330
        }
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   331
        if (CHECK_CUSTOM != -1) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   332
            names[CHECK_CUSTOM] = new Name(getFunction(NF_checkCustomized), outArgs[0]);
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   333
        }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   334
        names[LINKER_CALL] = new Name(outCallType, outArgs);
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   335
        if (customized) {
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   336
            lform = new LambdaForm(INARG_LIMIT, names);
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   337
        } else {
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   338
            lform = new LambdaForm(INARG_LIMIT, names, kind);
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   339
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   340
        if (isLinker)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   341
            lform.compileToBytecode();  // JVM needs a real methodOop
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   342
        if (isCached)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   343
            lform = mtype.form().setCachedLambdaForm(which, lform);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   344
        return lform;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   345
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   346
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   347
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   348
    static MemberName varHandleInvokeLinkerMethod(VarHandle.AccessMode ak, MethodType mtype) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   349
        LambdaForm lform;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   350
        if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   351
            lform = varHandleMethodGenericLinkerHandleForm(ak, mtype);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   352
        } else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   353
            // TODO
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   354
            throw newInternalError("Unsupported parameter slot count " + mtype.parameterSlotCount());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   355
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   356
        return lform.vmentry;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   357
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   358
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   359
    private static LambdaForm varHandleMethodGenericLinkerHandleForm(VarHandle.AccessMode ak,
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   360
            MethodType mtype) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   361
        // TODO Cache form?
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   362
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   363
        final int THIS_VH      = 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   364
        final int ARG_BASE     = THIS_VH + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   365
        final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   366
        int nameCursor = ARG_LIMIT;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   367
        final int VAD_ARG      = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   368
        final int CHECK_TYPE   = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   369
        final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   370
        final int LINKER_CALL  = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   371
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   372
        Name[] names = new Name[LINKER_CALL + 1];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   373
        names[THIS_VH] = argument(THIS_VH, BasicType.basicType(Object.class));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   374
        for (int i = 0; i < mtype.parameterCount(); i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   375
            names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i)));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   376
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   377
        names[VAD_ARG] = new Name(ARG_LIMIT, BasicType.basicType(Object.class));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   378
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   379
        names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleGenericType), names[THIS_VH], names[VAD_ARG]);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   380
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   381
        Object[] outArgs = new Object[ARG_LIMIT + 1];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   382
        outArgs[0] = names[CHECK_TYPE];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   383
        for (int i = 0; i < ARG_LIMIT; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   384
            outArgs[i + 1] = names[i];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   385
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   386
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   387
        if (CHECK_CUSTOM != -1) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   388
            names[CHECK_CUSTOM] = new Name(getFunction(NF_checkCustomized), outArgs[0]);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   389
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   390
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   391
        MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   392
                .basicType();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   393
        names[LINKER_CALL] = new Name(outCallType, outArgs);
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   394
        LambdaForm lform = new LambdaForm(ARG_LIMIT + 1, names, VARHANDLE_LINKER);
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   395
        if (LambdaForm.debugNames()) {
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   396
            String name = ak.methodName() + ":VarHandle_invoke_MT_" +
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   397
                    shortenSignature(basicTypeSignature(mtype));
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   398
            LambdaForm.associateWithDebugName(lform, name);
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   399
        }
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   400
        lform.compileToBytecode();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   401
        return lform;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   402
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   403
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   404
    private static LambdaForm varHandleMethodInvokerHandleForm(VarHandle.AccessMode ak,
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   405
            MethodType mtype, boolean isExact) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   406
        // TODO Cache form?
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   407
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   408
        final int THIS_MH      = 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   409
        final int CALL_VH      = THIS_MH + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   410
        final int ARG_BASE     = CALL_VH + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   411
        final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   412
        int nameCursor = ARG_LIMIT;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   413
        final int VAD_ARG      = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   414
        final int CHECK_TYPE   = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   415
        final int LINKER_CALL  = nameCursor++;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   416
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   417
        Name[] names = new Name[LINKER_CALL + 1];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   418
        names[THIS_MH] = argument(THIS_MH, BasicType.basicType(Object.class));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   419
        names[CALL_VH] = argument(CALL_VH, BasicType.basicType(Object.class));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   420
        for (int i = 0; i < mtype.parameterCount(); i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   421
            names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i)));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   422
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   423
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   424
        BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   425
        names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   426
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   427
        NamedFunction getter = speciesData.getterFunction(0);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   428
        names[VAD_ARG] = new Name(getter, names[THIS_MH]);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   429
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   430
        if (isExact) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   431
            names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleExactType), names[CALL_VH], names[VAD_ARG]);
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   432
        } else {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   433
            names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleGenericType), names[CALL_VH], names[VAD_ARG]);
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   434
        }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   435
        Object[] outArgs = new Object[ARG_LIMIT];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   436
        outArgs[0] = names[CHECK_TYPE];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   437
        for (int i = 1; i < ARG_LIMIT; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   438
            outArgs[i] = names[i];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   439
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   440
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   441
        MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   442
                .basicType();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   443
        names[LINKER_CALL] = new Name(outCallType, outArgs);
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   444
        Kind kind = isExact ? VARHANDLE_EXACT_INVOKER : VARHANDLE_INVOKER;
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   445
        LambdaForm lform = new LambdaForm(ARG_LIMIT, names, kind);
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   446
        if (LambdaForm.debugNames()) {
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   447
            String name = ak.methodName() +
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   448
                    (isExact ? ":VarHandle_exactInvoker_" : ":VarHandle_invoker_") +
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   449
                    shortenSignature(basicTypeSignature(mtype));
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   450
            LambdaForm.associateWithDebugName(lform, name);
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   451
        }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   452
        lform.prepare();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   453
        return lform;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   454
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   455
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   456
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   457
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   458
    static MethodHandle checkVarHandleGenericType(VarHandle handle, VarHandle.AccessDescriptor ad) {
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   459
        // Test for exact match on invoker types
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   460
        // TODO match with erased types and add cast of return value to lambda form
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   461
        MethodHandle mh = handle.getMethodHandle(ad.mode);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   462
        if (mh.type() == ad.symbolicMethodTypeInvoker) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   463
            return mh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   464
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   465
        else {
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37343
diff changeset
   466
            return mh.asType(ad.symbolicMethodTypeInvoker);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   467
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   468
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   469
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   470
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   471
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   472
    static MethodHandle checkVarHandleExactType(VarHandle handle, VarHandle.AccessDescriptor ad) {
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   473
        MethodHandle mh = handle.getMethodHandle(ad.mode);
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   474
        MethodType mt = mh.type();
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   475
        if (mt != ad.symbolicMethodTypeInvoker) {
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   476
            throw newWrongMethodTypeException(mt, ad.symbolicMethodTypeInvoker);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   477
        }
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37792
diff changeset
   478
        return mh;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   479
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 35773
diff changeset
   480
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   481
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   482
    static WrongMethodTypeException newWrongMethodTypeException(MethodType actual, MethodType expected) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   483
        // FIXME: merge with JVM logic for throwing WMTE
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   484
        return new WrongMethodTypeException("expected "+expected+" but found "+actual);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   485
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   486
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   487
    /** Static definition of MethodHandle.invokeExact checking code. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   488
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   489
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   490
    static void checkExactType(MethodHandle mh, MethodType expected) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   491
        MethodType actual = mh.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   492
        if (actual != expected)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   493
            throw newWrongMethodTypeException(expected, actual);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   494
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   495
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   496
    /** Static definition of MethodHandle.invokeGeneric checking code.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   497
     * Directly returns the type-adjusted MH to invoke, as follows:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   498
     * {@code (R)MH.invoke(a*) => MH.asType(TYPEOF(a*:R)).invokeBasic(a*)}
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   499
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   500
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   501
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   502
    static MethodHandle checkGenericType(MethodHandle mh,  MethodType expected) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   503
        return mh.asType(expected);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   504
        /* Maybe add more paths here.  Possible optimizations:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   505
         * for (R)MH.invoke(a*),
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   506
         * let MT0 = TYPEOF(a*:R), MT1 = MH.type
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   507
         *
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   508
         * if MT0==MT1 or MT1 can be safely called by MT0
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   509
         *  => MH.invokeBasic(a*)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   510
         * if MT1 can be safely called by MT0[R := Object]
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   511
         *  => MH.invokeBasic(a*) & checkcast(R)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   512
         * if MT1 can be safely called by MT0[* := Object]
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   513
         *  => checkcast(A)* & MH.invokeBasic(a*) & checkcast(R)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   514
         * if a big adapter BA can be pulled out of (MT0,MT1)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   515
         *  => BA.invokeBasic(MT0,MH,a*)
51986
c1db377f6300 8200381: Typos in javadoc - missing verb "be" and alike
igerasim
parents: 49240
diff changeset
   516
         * if a local adapter LA can be cached on static CS0 = new GICS(MT0)
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   517
         *  => CS0.LA.invokeBasic(MH,a*)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   518
         * else
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   519
         *  => MH.asType(MT0).invokeBasic(A*)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   520
         */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   521
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   522
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   523
    static MemberName linkToCallSiteMethod(MethodType mtype) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   524
        LambdaForm lform = callSiteForm(mtype, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   525
        return lform.vmentry;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   526
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   527
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   528
    static MemberName linkToTargetMethod(MethodType mtype) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   529
        LambdaForm lform = callSiteForm(mtype, true);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   530
        return lform.vmentry;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   531
    }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   532
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   533
    // skipCallSite is true if we are optimizing a ConstantCallSite
49240
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47280
diff changeset
   534
    static LambdaForm callSiteForm(MethodType mtype, boolean skipCallSite) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   535
        mtype = mtype.basicType();  // normalize Z to I, String to Object, etc.
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   536
        final int which = (skipCallSite ? MethodTypeForm.LF_MH_LINKER : MethodTypeForm.LF_CS_LINKER);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   537
        LambdaForm lform = mtype.form().cachedLambdaForm(which);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   538
        if (lform != null)  return lform;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   539
        // exactInvokerForm (Object,Object)Object
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   540
        //   link with java.lang.invoke.MethodHandle.invokeBasic(MethodHandle,Object,Object)Object/invokeSpecial
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   541
        final int ARG_BASE     = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   542
        final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   543
        final int INARG_LIMIT  = OUTARG_LIMIT + 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   544
        int nameCursor = OUTARG_LIMIT;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   545
        final int APPENDIX_ARG = nameCursor++;  // the last in-argument
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   546
        final int CSITE_ARG    = skipCallSite ? -1 : APPENDIX_ARG;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   547
        final int CALL_MH      = skipCallSite ? APPENDIX_ARG : nameCursor++;  // result of getTarget
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   548
        final int LINKER_CALL  = nameCursor++;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   549
        MethodType invokerFormType = mtype.appendParameterTypes(skipCallSite ? MethodHandle.class : CallSite.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   550
        Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   551
        assert(names.length == nameCursor);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   552
        assert(names[APPENDIX_ARG] != null);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   553
        if (!skipCallSite)
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   554
            names[CALL_MH] = new Name(getFunction(NF_getCallSiteTarget), names[CSITE_ARG]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   555
        // (site.)invokedynamic(a*):R => mh = site.getTarget(); mh.invokeBasic(a*)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   556
        final int PREPEND_MH = 0, PREPEND_COUNT = 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   557
        Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, OUTARG_LIMIT + PREPEND_COUNT, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   558
        // prepend MH argument:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   559
        System.arraycopy(outArgs, 0, outArgs, PREPEND_COUNT, outArgs.length - PREPEND_COUNT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   560
        outArgs[PREPEND_MH] = names[CALL_MH];
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   561
        names[LINKER_CALL] = new Name(mtype, outArgs);
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   562
        lform = new LambdaForm(INARG_LIMIT, names,
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 40543
diff changeset
   563
                (skipCallSite ? LINK_TO_TARGET_METHOD : LINK_TO_CALL_SITE));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   564
        lform.compileToBytecode();  // JVM needs a real methodOop
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 19804
diff changeset
   565
        lform = mtype.form().setCachedLambdaForm(which, lform);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   566
        return lform;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   567
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   568
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   569
    /** Static definition of MethodHandle.invokeGeneric checking code. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   570
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   571
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   572
    static MethodHandle getCallSiteTarget(CallSite site) {
35773
80def5fa4d65 8071368: Use more concrete types for NamedFunction constants in the code
srastogi
parents: 35252
diff changeset
   573
        return site.getTarget();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   574
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   575
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   576
    @ForceInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   577
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   578
    static void checkCustomized(MethodHandle mh) {
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29020
diff changeset
   579
        if (MethodHandleImpl.isCompileConstant(mh)) return;
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   580
        if (mh.form.customized == null) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   581
            maybeCustomize(mh);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   582
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   583
    }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   584
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   585
    @DontInline
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   586
    /*non-public*/
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 51986
diff changeset
   587
    static void maybeCustomize(MethodHandle mh) {
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   588
        byte count = mh.customizationCount;
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   589
        if (count >= CUSTOMIZE_THRESHOLD) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   590
            mh.customize();
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   591
        } else {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   592
            mh.customizationCount = (byte)(count+1);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   593
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   594
    }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 26479
diff changeset
   595
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   596
    // Local constant functions:
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   597
    private static final byte NF_checkExactType = 0,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   598
        NF_checkGenericType = 1,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   599
        NF_getCallSiteTarget = 2,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   600
        NF_checkCustomized = 3,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   601
        NF_checkVarHandleGenericType = 4,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   602
        NF_checkVarHandleExactType = 5,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   603
        NF_LIMIT = 6;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   604
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   605
    private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   606
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   607
    private static NamedFunction getFunction(byte func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   608
        NamedFunction nf = NFS[func];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   609
        if (nf != null) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   610
            return nf;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   611
        }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   612
        NFS[func] = nf = createFunction(func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   613
        // Each nf must be statically invocable or we get tied up in our bootstraps.
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   614
        assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   615
        return nf;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   616
    }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   617
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   618
    private static NamedFunction createFunction(byte func) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   619
        try {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   620
            switch (func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   621
                case NF_checkExactType:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   622
                    return getNamedFunction("checkExactType", MethodType.methodType(void.class, MethodHandle.class, MethodType.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   623
                case NF_checkGenericType:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   624
                    return getNamedFunction("checkGenericType", MethodType.methodType(MethodHandle.class, MethodHandle.class, MethodType.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   625
                case NF_getCallSiteTarget:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   626
                    return getNamedFunction("getCallSiteTarget", MethodType.methodType(MethodHandle.class, CallSite.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   627
                case NF_checkCustomized:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   628
                    return getNamedFunction("checkCustomized", MethodType.methodType(void.class, MethodHandle.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   629
                case NF_checkVarHandleGenericType:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   630
                    return getNamedFunction("checkVarHandleGenericType", MethodType.methodType(MethodHandle.class, VarHandle.class, VarHandle.AccessDescriptor.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   631
                case NF_checkVarHandleExactType:
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   632
                    return getNamedFunction("checkVarHandleExactType", MethodType.methodType(MethodHandle.class, VarHandle.class, VarHandle.AccessDescriptor.class));
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   633
                default:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   634
                    throw newInternalError("Unknown function: " + func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   635
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   636
        } catch (ReflectiveOperationException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   637
            throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   638
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   639
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   640
47280
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   641
    private static NamedFunction getNamedFunction(String name, MethodType type)
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   642
        throws ReflectiveOperationException
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   643
    {
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   644
        MemberName member = new MemberName(Invokers.class, name, type, REF_invokeStatic);
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   645
        return new NamedFunction(
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   646
                MemberName.getFactory()
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   647
                        .resolveOrFail(REF_invokeStatic, member, Invokers.class, NoSuchMethodException.class));
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   648
    }
95192765a858 8187826: Avoid using reflection to bootstrap NamedFunctions
redestad
parents: 47216
diff changeset
   649
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   650
    private static class Lazy {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   651
        private static final MethodHandle MH_asSpreader;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   652
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   653
        static {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   654
            try {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   655
                MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader",
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   656
                        MethodType.methodType(MethodHandle.class, Class.class, int.class));
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   657
            } catch (ReflectiveOperationException ex) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   658
                throw newInternalError(ex);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   659
            }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   660
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   661
    }
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   662
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   663
    static {
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   664
        // The Holder class will contain pre-generated Invokers resolved
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   665
        // speculatively using MemberName.getFactory().resolveOrNull. However, that
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   666
        // doesn't initialize the class, which subtly breaks inlining etc. By forcing
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   667
        // initialization of the Holder class we avoid these issues.
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   668
        UNSAFE.ensureClassInitialized(Holder.class);
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   669
    }
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   670
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   671
    /* Placeholder class for Invokers generated ahead of time */
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 38328
diff changeset
   672
    final class Holder {}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   673
}