src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 50337 4d1393781fef
child 52486 6f5948597697
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
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
/*
50337
4d1393781fef 8204194: Lazily create MethodHandleImpl$AsVarargsCollector.asCollectorCache
redestad
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
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50337
diff changeset
    28
import jdk.internal.access.JavaLangInvokeAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50337
diff changeset
    29
import jdk.internal.access.SharedSecrets;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    30
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    31
import jdk.internal.org.objectweb.asm.ClassWriter;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    32
import jdk.internal.org.objectweb.asm.MethodVisitor;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    33
import jdk.internal.reflect.CallerSensitive;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    34
import jdk.internal.reflect.Reflection;
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 39342
diff changeset
    35
import jdk.internal.vm.annotation.ForceInline;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    36
import jdk.internal.vm.annotation.Stable;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    37
import sun.invoke.empty.Empty;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    38
import sun.invoke.util.ValueConversions;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    39
import sun.invoke.util.VerifyType;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    40
import sun.invoke.util.Wrapper;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    41
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
    42
import java.lang.reflect.Array;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    43
import java.util.Arrays;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    44
import java.util.Collections;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
    45
import java.util.Iterator;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
    46
import java.util.List;
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
    47
import java.util.Map;
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
    48
import java.util.function.Function;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
    49
import java.util.stream.Stream;
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
    50
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    51
import static java.lang.invoke.LambdaForm.*;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    52
import static java.lang.invoke.MethodHandleStatics.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    53
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
    54
import static jdk.internal.org.objectweb.asm.Opcodes.*;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    55
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    56
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    57
 * Trusted implementation code for MethodHandle.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    58
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    59
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    60
/*non-public*/ abstract class MethodHandleImpl {
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
    61
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    62
    /// Factory methods to create method handles:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    64
    static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, ArrayAccess access) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    65
        if (arrayClass == Object[].class) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    66
            return ArrayAccess.objectAccessor(access);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    67
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    68
        if (!arrayClass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    69
            throw newIllegalArgumentException("not an array: "+arrayClass);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    70
        MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    71
        int cacheIndex = ArrayAccess.cacheIndex(access);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    72
        MethodHandle mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    73
        if (mh != null)  return mh;
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    74
        mh = ArrayAccessor.getAccessor(arrayClass, access);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    75
        MethodType correctType = ArrayAccessor.correctType(arrayClass, access);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    76
        if (mh.type() != correctType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    77
            assert(mh.type().parameterType(0) == Object[].class);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    78
            /* if access == SET */ assert(access != ArrayAccess.SET || mh.type().parameterType(2) == Object.class);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    79
            /* if access == GET */ assert(access != ArrayAccess.GET ||
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    80
                    (mh.type().returnType() == Object.class &&
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    81
                     correctType.parameterType(0).getComponentType() == correctType.returnType()));
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    82
            // safe to view non-strictly, because element type follows from array type
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
    83
            mh = mh.viewAsType(correctType, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    84
        }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    85
        mh = makeIntrinsic(mh, ArrayAccess.intrinsic(access));
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    86
        // Atomically update accessor cache.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    87
        synchronized(cache) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    88
            if (cache[cacheIndex] == null) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    89
                cache[cacheIndex] = mh;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    90
            } else {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    91
                // Throw away newly constructed accessor and use cached version.
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    92
                mh = cache[cacheIndex];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    93
            }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
    94
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
    95
        return mh;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    96
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    97
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    98
    enum ArrayAccess {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
    99
        GET, SET, LENGTH;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   100
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   101
        // As ArrayAccess and ArrayAccessor have a circular dependency, the ArrayAccess properties cannot be stored in
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   102
        // final fields.
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   103
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   104
        static String opName(ArrayAccess a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   105
            switch (a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   106
                case GET: return "getElement";
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   107
                case SET: return "setElement";
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   108
                case LENGTH: return "length";
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   109
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   110
            throw unmatchedArrayAccess(a);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   111
        }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   112
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   113
        static MethodHandle objectAccessor(ArrayAccess a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   114
            switch (a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   115
                case GET: return ArrayAccessor.OBJECT_ARRAY_GETTER;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   116
                case SET: return ArrayAccessor.OBJECT_ARRAY_SETTER;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   117
                case LENGTH: return ArrayAccessor.OBJECT_ARRAY_LENGTH;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   118
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   119
            throw unmatchedArrayAccess(a);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   120
        }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   121
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   122
        static int cacheIndex(ArrayAccess a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   123
            switch (a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   124
                case GET: return ArrayAccessor.GETTER_INDEX;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   125
                case SET: return ArrayAccessor.SETTER_INDEX;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   126
                case LENGTH: return ArrayAccessor.LENGTH_INDEX;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   127
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   128
            throw unmatchedArrayAccess(a);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   129
        }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   130
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   131
        static Intrinsic intrinsic(ArrayAccess a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   132
            switch (a) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   133
                case GET: return Intrinsic.ARRAY_LOAD;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   134
                case SET: return Intrinsic.ARRAY_STORE;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   135
                case LENGTH: return Intrinsic.ARRAY_LENGTH;
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   136
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   137
            throw unmatchedArrayAccess(a);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   138
        }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   139
    }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   140
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   141
    static InternalError unmatchedArrayAccess(ArrayAccess a) {
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   142
        return newInternalError("should not reach here (unmatched ArrayAccess: " + a + ")");
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   143
    }
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   144
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   145
    static final class ArrayAccessor {
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   146
        /// Support for array element and length access
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   147
        static final int GETTER_INDEX = 0, SETTER_INDEX = 1, LENGTH_INDEX = 2, INDEX_LIMIT = 3;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   148
        static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   149
                = new ClassValue<MethodHandle[]>() {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   150
                    @Override
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   151
                    protected MethodHandle[] computeValue(Class<?> type) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   152
                        return new MethodHandle[INDEX_LIMIT];
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   153
                    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   154
                };
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   155
        static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER, OBJECT_ARRAY_LENGTH;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   156
        static {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   157
            MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   158
            cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.GET),    Intrinsic.ARRAY_LOAD);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   159
            cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.SET),    Intrinsic.ARRAY_STORE);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   160
            cache[LENGTH_INDEX] = OBJECT_ARRAY_LENGTH = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.LENGTH), Intrinsic.ARRAY_LENGTH);
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   161
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   162
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   163
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   164
            assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_LENGTH.internalMemberName()));
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   165
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   166
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   167
        static int     getElementI(int[]     a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   168
        static long    getElementJ(long[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   169
        static float   getElementF(float[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   170
        static double  getElementD(double[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   171
        static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   172
        static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   173
        static short   getElementS(short[]   a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   174
        static char    getElementC(char[]    a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   175
        static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   176
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   177
        static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   178
        static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   179
        static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   180
        static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   181
        static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   182
        static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   183
        static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   184
        static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   185
        static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   186
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   187
        static int     lengthI(int[]     a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   188
        static int     lengthJ(long[]    a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   189
        static int     lengthF(float[]   a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   190
        static int     lengthD(double[]  a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   191
        static int     lengthZ(boolean[] a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   192
        static int     lengthB(byte[]    a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   193
        static int     lengthS(short[]   a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   194
        static int     lengthC(char[]    a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   195
        static int     lengthL(Object[]  a)                       { return a.length; }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   196
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   197
        static String name(Class<?> arrayClass, ArrayAccess access) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   198
            Class<?> elemClass = arrayClass.getComponentType();
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   199
            if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   200
            return ArrayAccess.opName(access) + Wrapper.basicTypeChar(elemClass);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   201
        }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   202
        static MethodType type(Class<?> arrayClass, ArrayAccess access) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   203
            Class<?> elemClass = arrayClass.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   204
            Class<?> arrayArgClass = arrayClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   205
            if (!elemClass.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   206
                arrayArgClass = Object[].class;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   207
                elemClass = Object.class;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   208
            }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   209
            switch (access) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   210
                case GET:    return MethodType.methodType(elemClass,  arrayArgClass, int.class);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   211
                case SET:    return MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   212
                case LENGTH: return MethodType.methodType(int.class,  arrayArgClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   213
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   214
            throw unmatchedArrayAccess(access);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   215
        }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   216
        static MethodType correctType(Class<?> arrayClass, ArrayAccess access) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   217
            Class<?> elemClass = arrayClass.getComponentType();
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   218
            switch (access) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   219
                case GET:    return MethodType.methodType(elemClass,  arrayClass, int.class);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   220
                case SET:    return MethodType.methodType(void.class, arrayClass, int.class, elemClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   221
                case LENGTH: return MethodType.methodType(int.class,  arrayClass);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   222
            }
38472
6d7aed566bf3 8157590: MethodHandles.arrayLength() lacks @since tag, implementation throws wrong exception
mhaupt
parents: 38422
diff changeset
   223
            throw unmatchedArrayAccess(access);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   224
        }
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   225
        static MethodHandle getAccessor(Class<?> arrayClass, ArrayAccess access) {
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   226
            String     name = name(arrayClass, access);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
   227
            MethodType type = type(arrayClass, access);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   228
            try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   229
                return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
   230
            } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   231
                throw uncaughtException(ex);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   232
            }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   233
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   234
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   235
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   236
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   237
     * Create a JVM-level adapter method handle to conform the given method
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   238
     * handle to the similar newType, using only pairwise argument conversions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   239
     * For each argument, convert incoming argument to the exact type needed.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   240
     * The argument conversions allowed are casting, boxing and unboxing,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   241
     * integral widening or narrowing, and floating point widening or narrowing.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   242
     * @param srcType required call type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   243
     * @param target original method handle
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   244
     * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   245
     * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   246
     * @return an adapter to the original handle with the desired new type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   247
     *          or the original target if the types are already identical
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   248
     *          or null if the adaptation cannot be made
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   249
     */
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   250
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   251
                                            boolean strict, boolean monobox) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   252
        MethodType dstType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   253
        if (srcType == dstType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   254
            return target;
27755
baf8735fc8bf 8063135: Enable full LF sharing by default
vlivanov
parents: 27350
diff changeset
   255
        return makePairwiseConvertByEditor(target, srcType, strict, monobox);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   256
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   257
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   258
    private static int countNonNull(Object[] array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   259
        int count = 0;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   260
        for (Object x : array) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   261
            if (x != null)  ++count;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   262
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   263
        return count;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   264
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   265
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   266
    static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   267
                                                    boolean strict, boolean monobox) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   268
        Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   269
        int convCount = countNonNull(convSpecs);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   270
        if (convCount == 0)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   271
            return target.viewAsType(srcType, strict);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   272
        MethodType basicSrcType = srcType.basicType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   273
        MethodType midType = target.type().basicType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   274
        BoundMethodHandle mh = target.rebind();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   275
        // FIXME: Reduce number of bindings when there is more than one Class conversion.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   276
        // FIXME: Reduce number of bindings when there are repeated conversions.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   277
        for (int i = 0; i < convSpecs.length-1; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   278
            Object convSpec = convSpecs[i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   279
            if (convSpec == null)  continue;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   280
            MethodHandle fn;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   281
            if (convSpec instanceof Class) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   282
                fn = getConstantHandle(MH_cast).bindTo(convSpec);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   283
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   284
                fn = (MethodHandle) convSpec;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   285
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   286
            Class<?> newType = basicSrcType.parameterType(i);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   287
            if (--convCount == 0)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   288
                midType = srcType;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   289
            else
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   290
                midType = midType.changeParameterType(i, newType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   291
            LambdaForm form2 = mh.editor().filterArgumentForm(1+i, BasicType.basicType(newType));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   292
            mh = mh.copyWithExtendL(midType, form2, fn);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   293
            mh = mh.rebind();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   294
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   295
        Object convSpec = convSpecs[convSpecs.length-1];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   296
        if (convSpec != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   297
            MethodHandle fn;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   298
            if (convSpec instanceof Class) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   299
                if (convSpec == void.class)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   300
                    fn = null;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   301
                else
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   302
                    fn = getConstantHandle(MH_cast).bindTo(convSpec);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   303
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   304
                fn = (MethodHandle) convSpec;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   305
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   306
            Class<?> newType = basicSrcType.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   307
            assert(--convCount == 0);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   308
            midType = srcType;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   309
            if (fn != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   310
                mh = mh.rebind();  // rebind if too complex
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   311
                LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   312
                mh = mh.copyWithExtendL(midType, form2, fn);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   313
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   314
                LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   315
                mh = mh.copyWith(midType, form2);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   316
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   317
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   318
        assert(convCount == 0);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   319
        assert(mh.type().equals(srcType));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   320
        return mh;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   321
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   322
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   323
    static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   324
                                                    boolean strict, boolean monobox) {
26625
cbeb3711fa94 8058291: Missing some checks during parameter validation
vlivanov
parents: 26482
diff changeset
   325
        assert(target.type().parameterCount() == srcType.parameterCount());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   326
        // Calculate extra arguments (temporaries) required in the names array.
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   327
        Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   328
        final int INARG_COUNT = srcType.parameterCount();
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   329
        int convCount = countNonNull(convSpecs);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   330
        boolean retConv = (convSpecs[INARG_COUNT] != null);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   331
        boolean retVoid = srcType.returnType() == void.class;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   332
        if (retConv && retVoid) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   333
            convCount -= 1;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   334
            retConv = false;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   335
        }
13420
62cedce8afa6 7129034: VM crash with a field setter method with a filterArguments
jrose
parents: 11534
diff changeset
   336
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   337
        final int IN_MH         = 0;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   338
        final int INARG_BASE    = 1;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   339
        final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   340
        final int NAME_LIMIT    = INARG_LIMIT + convCount + 1;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   341
        final int RETURN_CONV   = (!retConv ? -1         : NAME_LIMIT - 1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   342
        final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   343
        final int RESULT        = (retVoid ? -1 : NAME_LIMIT - 1);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   344
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   345
        // Now build a LambdaForm.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   346
        MethodType lambdaType = srcType.basicType().invokerType();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   347
        Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   348
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   349
        // Collect the arguments to the outgoing call, maybe with conversions:
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   350
        final int OUTARG_BASE = 0;  // target MH is Name.function, name Name.arguments[0]
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   351
        Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   352
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   353
        int nameCursor = INARG_LIMIT;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   354
        for (int i = 0; i < INARG_COUNT; i++) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   355
            Object convSpec = convSpecs[i];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   356
            if (convSpec == null) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   357
                // do nothing: difference is trivial
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   358
                outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   359
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   360
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   361
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   362
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   363
            if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   364
                Class<?> convClass = (Class<?>) convSpec;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   365
                conv = new Name(getConstantHandle(MH_cast), convClass, names[INARG_BASE + i]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   366
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   367
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   368
                conv = new Name(fn, names[INARG_BASE + i]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   369
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   370
            assert(names[nameCursor] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   371
            names[nameCursor++] = conv;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   372
            assert(outArgs[OUTARG_BASE + i] == null);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   373
            outArgs[OUTARG_BASE + i] = conv;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   374
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   375
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   376
        // Build argument array for the call.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   377
        assert(nameCursor == OUT_CALL);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   378
        names[OUT_CALL] = new Name(target, outArgs);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   379
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   380
        Object convSpec = convSpecs[INARG_COUNT];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   381
        if (!retConv) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   382
            assert(OUT_CALL == names.length-1);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   383
        } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   384
            Name conv;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   385
            if (convSpec == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   386
                conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType())));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   387
            } else if (convSpec instanceof Class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   388
                Class<?> convClass = (Class<?>) convSpec;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   389
                conv = new Name(getConstantHandle(MH_cast), convClass, names[OUT_CALL]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   390
            } else {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   391
                MethodHandle fn = (MethodHandle) convSpec;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   392
                if (fn.type().parameterCount() == 0)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   393
                    conv = new Name(fn);  // don't pass retval to void conversion
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   394
                else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   395
                    conv = new Name(fn, names[OUT_CALL]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   396
            }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   397
            assert(names[RETURN_CONV] == null);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   398
            names[RETURN_CONV] = conv;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   399
            assert(RETURN_CONV == names.length-1);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   400
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   401
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   402
        LambdaForm form = new LambdaForm(lambdaType.parameterCount(), names, RESULT, Kind.CONVERT);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   403
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   404
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   405
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   406
    static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   407
                                            boolean strict, boolean monobox) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   408
        final int INARG_COUNT = srcType.parameterCount();
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   409
        Object[] convSpecs = new Object[INARG_COUNT+1];
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   410
        for (int i = 0; i <= INARG_COUNT; i++) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   411
            boolean isRet = (i == INARG_COUNT);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   412
            Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   413
            Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   414
            if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   415
                convSpecs[i] = valueConversion(src, dst, strict, monobox);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   416
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   417
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   418
        return convSpecs;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   419
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   420
    static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   421
                                            boolean strict) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   422
        return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   423
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   424
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   425
    /**
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   426
     * Find a conversion function from the given source to the given destination.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   427
     * This conversion function will be used as a LF NamedFunction.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   428
     * Return a Class object if a simple cast is needed.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   429
     * Return void.class if void is involved.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   430
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   431
    static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   432
        assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   433
        if (dst == void.class)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   434
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   435
        MethodHandle fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   436
        if (src.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   437
            if (src == void.class) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   438
                return void.class;  // caller must recognize this specially
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   439
            } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   440
                // Examples: int->byte, byte->int, boolean->int (!strict)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   441
                fn = ValueConversions.convertPrimitive(src, dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   442
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   443
                // Examples: int->Integer, boolean->Object, float->Number
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   444
                Wrapper wsrc = Wrapper.forPrimitiveType(src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   445
                fn = ValueConversions.boxExact(wsrc);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   446
                assert(fn.type().parameterType(0) == wsrc.primitiveType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   447
                assert(fn.type().returnType() == wsrc.wrapperType());
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   448
                if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   449
                    // Corner case, such as int->Long, which will probably fail.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   450
                    MethodType mt = MethodType.methodType(dst, src);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   451
                    if (strict)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   452
                        fn = fn.asType(mt);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   453
                    else
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   454
                        fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   455
                }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   456
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   457
        } else if (dst.isPrimitive()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   458
            Wrapper wdst = Wrapper.forPrimitiveType(dst);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   459
            if (monobox || src == wdst.wrapperType()) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   460
                // Use a strongly-typed unboxer, if possible.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   461
                fn = ValueConversions.unboxExact(wdst, strict);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   462
            } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   463
                // Examples:  Object->int, Number->int, Comparable->int, Byte->int
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   464
                // must include additional conversions
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   465
                // src must be examined at runtime, to detect Byte, Character, etc.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   466
                fn = (strict
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   467
                        ? ValueConversions.unboxWiden(wdst)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   468
                        : ValueConversions.unboxCast(wdst));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   469
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   470
        } else {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   471
            // Simple reference conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   472
            // Note:  Do not check for a class hierarchy relation
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   473
            // between src and dst.  In all cases a 'null' argument
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   474
            // will pass the cast conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   475
            return dst;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   476
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   477
        assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   478
        return fn;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   479
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26473
diff changeset
   480
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   481
    static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   482
        MethodType type = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   483
        int last = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   484
        if (type.parameterType(last) != arrayType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   485
            target = target.asType(type.changeParameterType(last, arrayType));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   486
        target = target.asFixedArity();  // make sure this attribute is turned off
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   487
        return new AsVarargsCollector(target, arrayType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   488
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   489
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   490
    private static final class AsVarargsCollector extends DelegatingMethodHandle {
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   491
        private final MethodHandle target;
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14230
diff changeset
   492
        private final Class<?> arrayType;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
   493
        private @Stable MethodHandle asCollectorCache;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   494
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   495
        AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   496
            this(target.type(), target, arrayType);
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   497
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   498
        AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   499
            super(type, target);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   500
            this.target = target;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   501
            this.arrayType = arrayType;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   502
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   503
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   504
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   505
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   506
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   507
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   508
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   509
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   510
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   511
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   512
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   513
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   514
        @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   515
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   516
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   517
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   518
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   519
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   520
        MethodHandle setVarargs(MemberName member) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   521
            if (member.isVarargs())  return this;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   522
            return asFixedArity();
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   523
        }
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   524
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   525
        @Override
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   526
        public MethodHandle withVarargs(boolean makeVarargs) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   527
            if (makeVarargs)  return this;
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   528
            return asFixedArity();
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   529
        }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   530
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   531
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   532
        public MethodHandle asTypeUncached(MethodType newType) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   533
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   534
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   535
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   536
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   537
                type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   538
                // if arity and trailing parameter are compatible, do normal thing
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   539
                return asTypeCache = asFixedArity().asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   540
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   542
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   543
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   544
                return asTypeCache = acc.asType(newType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   545
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   546
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   547
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   548
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   549
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   550
                assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   551
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   552
                throw new WrongMethodTypeException("cannot build collector", ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   553
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   554
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   555
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   556
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   557
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   558
        @Override
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   559
        boolean viewAsTypeChecks(MethodType newType, boolean strict) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   560
            super.viewAsTypeChecks(newType, true);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   561
            if (strict) return true;
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   562
            // extra assertion for non-strict checks:
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   563
            assert (type().lastParameterType().getComponentType()
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   564
                    .isAssignableFrom(
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   565
                            newType.lastParameterType().getComponentType()))
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   566
                    : Arrays.asList(this, newType);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   567
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   568
        }
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   569
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   570
        @Override
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   571
        public Object invokeWithArguments(Object... arguments) throws Throwable {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   572
            MethodType type = this.type();
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   573
            int argc;
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   574
            final int MAX_SAFE = 127;  // 127 longs require 254 slots, which is safe to spread
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   575
            if (arguments == null
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   576
                    || (argc = arguments.length) <= MAX_SAFE
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   577
                    || argc < type.parameterCount()) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   578
                return super.invokeWithArguments(arguments);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   579
            }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   580
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   581
            // a jumbo invocation requires more explicit reboxing of the trailing arguments
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   582
            int uncollected = type.parameterCount() - 1;
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   583
            Class<?> elemType = arrayType.getComponentType();
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   584
            int collected = argc - uncollected;
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   585
            Object collArgs = (elemType == Object.class)
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   586
                ? new Object[collected] : Array.newInstance(elemType, collected);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   587
            if (!elemType.isPrimitive()) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   588
                // simple cast:  just do some casting
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   589
                try {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   590
                    System.arraycopy(arguments, uncollected, collArgs, 0, collected);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   591
                } catch (ArrayStoreException ex) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   592
                    return super.invokeWithArguments(arguments);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   593
                }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   594
            } else {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   595
                // corner case of flat array requires reflection (or specialized copy loop)
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   596
                MethodHandle arraySetter = MethodHandles.arrayElementSetter(arrayType);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   597
                try {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   598
                    for (int i = 0; i < collected; i++) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   599
                        arraySetter.invoke(collArgs, i, arguments[uncollected + i]);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   600
                    }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   601
                } catch (WrongMethodTypeException|ClassCastException ex) {
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   602
                    return super.invokeWithArguments(arguments);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   603
                }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   604
            }
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   605
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   606
            // chop the jumbo list down to size and call in non-varargs mode
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   607
            Object[] newArgs = new Object[uncollected + 1];
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   608
            System.arraycopy(arguments, 0, newArgs, 0, uncollected);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   609
            newArgs[uncollected] = collArgs;
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   610
            return asFixedArity().invokeWithArguments(newArgs);
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   611
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   612
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   613
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   614
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   615
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   616
                                            Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   617
        MethodType targetType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   618
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   619
        for (int i = 0; i < spreadArgCount; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   620
            Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   621
            if (arg == null)  arg = Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   622
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   623
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   624
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   625
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   626
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   627
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   628
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   629
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   630
        Name[] names = arguments(spreadArgCount + 2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   631
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   632
        int[] indexes = new int[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   633
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   634
        for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   635
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   636
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   637
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   638
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   639
                Name array = names[argIndex];
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   640
                names[nameCursor++] = new Name(getFunction(NF_checkSpreadArgument), array, spreadArgCount);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   641
                for (int j = 0; j < spreadArgCount; i++, j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   642
                    indexes[i] = nameCursor;
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
   643
                    names[nameCursor++] = new Name(new NamedFunction(aload, Intrinsic.ARRAY_LOAD), array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   644
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   645
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   646
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   647
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   648
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   649
        assert(nameCursor == names.length-1);  // leave room for the final call
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   650
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   651
        // Build argument array for the call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   652
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   653
        for (int i = 0; i < targetType.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   654
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   655
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   656
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   657
        names[names.length - 1] = new Name(target, (Object[]) targetArgs);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   658
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   659
        LambdaForm form = new LambdaForm(lambdaType.parameterCount(), names, Kind.SPREAD);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   660
        return SimpleMethodHandle.make(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   661
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   662
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   663
    static void checkSpreadArgument(Object av, int n) {
47856
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47825
diff changeset
   664
        if (av == null && n == 0) {
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47825
diff changeset
   665
            return;
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47825
diff changeset
   666
        } else if (av == null) {
76519338df34 8157246: MHs.arrayLength, arrayElementGetter/Setter, arrayConstructor need to specify invocation-time behavior
mchung
parents: 47825
diff changeset
   667
            throw new NullPointerException("null array reference");
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   668
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   669
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   670
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   671
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   672
            int len = java.lang.reflect.Array.getLength(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   673
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   674
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   675
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   676
        throw newIllegalArgumentException("array is not of length "+n);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   677
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   678
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   679
    /** Factory method:  Collect or filter selected argument(s). */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   680
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   681
                MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   682
        MethodType targetType = target.type();          // (a..., c, [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   683
        MethodType collectorType = collector.type();    // (b...)=>c
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   684
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   685
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   686
        int collectValCount = (collectValType == void.class ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   687
        MethodType srcType = targetType                 // (a..., [b...])=>r
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   688
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   689
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   690
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterArray());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   691
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   692
        // in  arglist: [0: ...keep1 | cpos: collect...  | cpos+cacount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   693
        // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   694
        // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   695
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   696
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   697
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   698
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   699
        final int collectNamePos = names.length - 2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   700
        final int targetNamePos  = names.length - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   701
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   702
        Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   703
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   704
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   705
        // Build argument array for the target.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   706
        // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   707
        // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   708
        Name[] targetArgs = new Name[targetType.parameterCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   709
        int inputArgPos  = 1;  // incoming LF args to copy to target
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   710
        int targetArgPos = 0;  // fill pointer for targetArgs
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   711
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   712
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   713
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   714
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   715
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   716
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   717
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   718
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   719
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   720
            System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   721
            targetArgPos += chunk;   // optionally pass on the collected chunk
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   722
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   723
        inputArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   724
        chunk = targetArgs.length - targetArgPos;  // all the rest
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   725
        System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   726
        assert(inputArgPos + chunk == collectNamePos);  // use of rest of input args also
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   727
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   728
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   729
        LambdaForm form = new LambdaForm(lambdaType.parameterCount(), names, Kind.COLLECT);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   730
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   731
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   732
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   733
    @LambdaForm.Hidden
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   734
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   735
    MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   736
        if (testResult) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   737
            return target;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   738
        } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   739
            return fallback;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   740
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   741
    }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   742
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   743
    // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   744
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   745
    @jdk.internal.HotSpotIntrinsicCandidate
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   746
    static
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   747
    boolean profileBoolean(boolean result, int[] counters) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   748
        // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   749
        int idx = result ? 1 : 0;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   750
        try {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   751
            counters[idx] = Math.addExact(counters[idx], 1);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   752
        } catch (ArithmeticException e) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   753
            // Avoid continuous overflow by halving the problematic count.
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   754
            counters[idx] = counters[idx] / 2;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   755
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   756
        return result;
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   757
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   758
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   759
    // Intrinsified by C2. Returns true if obj is a compile-time constant.
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   760
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   761
    @jdk.internal.HotSpotIntrinsicCandidate
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   762
    static
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   763
    boolean isCompileConstant(Object obj) {
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   764
        return false;
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   765
    }
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   766
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   767
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   768
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   769
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   770
                                   MethodHandle fallback) {
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   771
        MethodType type = target.type();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   772
        assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   773
        MethodType basicType = type.basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   774
        LambdaForm form = makeGuardWithTestForm(basicType);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   775
        BoundMethodHandle mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   776
        try {
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   777
            if (PROFILE_GWT) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   778
                int[] counts = new int[2];
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   779
                mh = (BoundMethodHandle)
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47471
diff changeset
   780
                        BoundMethodHandle.speciesData_LLLL().factory().invokeBasic(type, form,
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   781
                                (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   782
            } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   783
                mh = (BoundMethodHandle)
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47471
diff changeset
   784
                        BoundMethodHandle.speciesData_LLL().factory().invokeBasic(type, form,
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   785
                                (Object) test, (Object) profile(target), (Object) profile(fallback));
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   786
            }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   787
        } catch (Throwable ex) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   788
            throw uncaughtException(ex);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   789
        }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   790
        assert(mh.type() == type);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   791
        return mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   792
    }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   793
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   794
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   795
    static
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   796
    MethodHandle profile(MethodHandle target) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   797
        if (DONT_INLINE_THRESHOLD >= 0) {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   798
            return makeBlockInliningWrapper(target);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   799
        } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   800
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   801
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   802
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   803
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   804
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   805
     * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   806
     * Corresponding LambdaForm has @DontInline when compiled into bytecode.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   807
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   808
    static
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   809
    MethodHandle makeBlockInliningWrapper(MethodHandle target) {
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   810
        LambdaForm lform;
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   811
        if (DONT_INLINE_THRESHOLD > 0) {
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   812
            lform = Makers.PRODUCE_BLOCK_INLINING_FORM.apply(target);
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   813
        } else {
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   814
            lform = Makers.PRODUCE_REINVOKER_FORM.apply(target);
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   815
        }
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   816
        return new CountingWrapper(target, lform,
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   817
                Makers.PRODUCE_BLOCK_INLINING_FORM, Makers.PRODUCE_REINVOKER_FORM,
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   818
                                   DONT_INLINE_THRESHOLD);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   819
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   820
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   821
    private final static class Makers {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   822
        /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   823
        static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   824
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   825
            public LambdaForm apply(MethodHandle target) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   826
                return DelegatingMethodHandle.makeReinvokerForm(target,
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   827
                                   MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, false,
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   828
                                   DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   829
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   830
        };
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   831
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   832
        /** Constructs simple reinvoker lambda form for a particular method handle */
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   833
        static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   834
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   835
            public LambdaForm apply(MethodHandle target) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   836
                return DelegatingMethodHandle.makeReinvokerForm(target,
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   837
                        MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   838
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   839
        };
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   840
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   841
        /** Maker of type-polymorphic varargs */
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   842
        static final ClassValue<MethodHandle[]> TYPED_COLLECTORS = new ClassValue<MethodHandle[]>() {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   843
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   844
            protected MethodHandle[] computeValue(Class<?> type) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   845
                return new MethodHandle[MAX_JVM_ARITY + 1];
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   846
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   847
        };
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   848
    }
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   849
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   850
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   851
     * Counting method handle. It has 2 states: counting and non-counting.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   852
     * It is in counting state for the first n invocations and then transitions to non-counting state.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   853
     * Behavior in counting and non-counting states is determined by lambda forms produced by
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   854
     * countingFormProducer & nonCountingFormProducer respectively.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   855
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   856
    static class CountingWrapper extends DelegatingMethodHandle {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   857
        private final MethodHandle target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   858
        private int count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   859
        private Function<MethodHandle, LambdaForm> countingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   860
        private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   861
        private volatile boolean isCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   862
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   863
        private CountingWrapper(MethodHandle target, LambdaForm lform,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   864
                                Function<MethodHandle, LambdaForm> countingFromProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   865
                                Function<MethodHandle, LambdaForm> nonCountingFormProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   866
                                int count) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   867
            super(target.type(), lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   868
            this.target = target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   869
            this.count = count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   870
            this.countingFormProducer = countingFromProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   871
            this.nonCountingFormProducer = nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   872
            this.isCounting = (count > 0);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   873
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   874
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   875
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   876
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   877
        protected MethodHandle getTarget() {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   878
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   879
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   880
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   881
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   882
        public MethodHandle asTypeUncached(MethodType newType) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   883
            MethodHandle newTarget = target.asType(newType);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   884
            MethodHandle wrapper;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   885
            if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   886
                LambdaForm lform;
29022
31bbb5dcc603 8071788: BlockInliningWrapper.asType() is broken
vlivanov
parents: 29021
diff changeset
   887
                lform = countingFormProducer.apply(newTarget);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   888
                wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   889
            } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   890
                wrapper = newTarget; // no need for a counting wrapper anymore
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   891
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   892
            return (asTypeCache = wrapper);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   893
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   894
36120
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   895
        // Customize target if counting happens for too long.
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   896
        private int invocations = CUSTOMIZE_THRESHOLD;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   897
        private void maybeCustomizeTarget() {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   898
            int c = invocations;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   899
            if (c >= 0) {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   900
                if (c == 1) {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   901
                    target.customize();
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   902
                }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   903
                invocations = c - 1;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   904
            }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   905
        }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   906
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   907
        boolean countDown() {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   908
            int c = count;
36120
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   909
            maybeCustomizeTarget();
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   910
            if (c <= 1) {
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   911
                // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   912
                if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   913
                    isCounting = false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   914
                    return true;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   915
                } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   916
                    return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   917
                }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   918
            } else {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   919
                count = c - 1;
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   920
                return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   921
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   922
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   923
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   924
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   925
        static void maybeStopCounting(Object o1) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   926
             CountingWrapper wrapper = (CountingWrapper) o1;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   927
             if (wrapper.countDown()) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   928
                 // Reached invocation threshold. Replace counting behavior with a non-counting one.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   929
                 LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   930
                 lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   931
                 wrapper.updateForm(lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   932
             }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   933
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   934
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   935
        static final NamedFunction NF_maybeStopCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   936
        static {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   937
            Class<?> THIS_CLASS = CountingWrapper.class;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   938
            try {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   939
                NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   940
            } catch (ReflectiveOperationException ex) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   941
                throw newInternalError(ex);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   942
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   943
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   944
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   945
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   946
    static
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   947
    LambdaForm makeGuardWithTestForm(MethodType basicType) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   948
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   949
        if (lform != null)  return lform;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   950
        final int THIS_MH      = 0;  // the BMH_LLL
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   951
        final int ARG_BASE     = 1;  // start of incoming arguments
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   952
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   953
        int nameCursor = ARG_LIMIT;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   954
        final int GET_TEST     = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   955
        final int GET_TARGET   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   956
        final int GET_FALLBACK = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   957
        final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   958
        final int CALL_TEST    = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   959
        final int PROFILE      = (GET_COUNTERS != -1) ? nameCursor++ : -1;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   960
        final int TEST         = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   961
        final int SELECT_ALT   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   962
        final int CALL_TARGET  = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   963
        assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   964
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   965
        MethodType lambdaType = basicType.invokerType();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   966
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   967
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   968
        BoundMethodHandle.SpeciesData data =
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   969
                (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   970
                                     : BoundMethodHandle.speciesData_LLL();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   971
        names[THIS_MH] = names[THIS_MH].withConstraint(data);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   972
        names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   973
        names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   974
        names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   975
        if (GET_COUNTERS != -1) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   976
            names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   977
        }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   978
        Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   979
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   980
        // call test
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   981
        MethodType testType = basicType.changeReturnType(boolean.class).basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   982
        invokeArgs[0] = names[GET_TEST];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   983
        names[CALL_TEST] = new Name(testType, invokeArgs);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   984
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   985
        // profile branch
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   986
        if (PROFILE != -1) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   987
            names[PROFILE] = new Name(getFunction(NF_profileBoolean), names[CALL_TEST], names[GET_COUNTERS]);
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   988
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   989
        // call selectAlternative
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
   990
        names[SELECT_ALT] = new Name(new NamedFunction(getConstantHandle(MH_selectAlternative), Intrinsic.SELECT_ALTERNATIVE), names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   991
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   992
        // call target or fallback
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   993
        invokeArgs[0] = names[SELECT_ALT];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   994
        names[CALL_TARGET] = new Name(basicType, invokeArgs);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   995
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   996
        lform = new LambdaForm(lambdaType.parameterCount(), names, /*forceInline=*/true, Kind.GUARD);
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   997
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   998
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   999
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1000
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1001
    /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
  1002
     * The LambdaForm shape for catchException combinator is the following:
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1003
     * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1004
     *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1005
     *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1006
     *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1007
     *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1008
     *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1009
     *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1010
     *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1011
     *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1012
     *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1013
     * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1014
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1015
     * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1016
     * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1017
     * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1018
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1019
     * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1020
     * among catchException combinators with the same basic type.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1021
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1022
    private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1023
        MethodType lambdaType = basicType.invokerType();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1024
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1025
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1026
        if (lform != null) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1027
            return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1028
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1029
        final int THIS_MH      = 0;  // the BMH_LLLLL
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1030
        final int ARG_BASE     = 1;  // start of incoming arguments
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1031
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1032
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1033
        int nameCursor = ARG_LIMIT;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1034
        final int GET_TARGET       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1035
        final int GET_CLASS        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1036
        final int GET_CATCHER      = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1037
        final int GET_COLLECT_ARGS = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1038
        final int GET_UNBOX_RESULT = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1039
        final int BOXED_ARGS       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1040
        final int TRY_CATCH        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1041
        final int UNBOX_RESULT     = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1042
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1043
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1044
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1045
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
26479
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26476
diff changeset
  1046
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1047
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1048
        names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1049
        names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1050
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1051
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1052
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1053
        // FIXME: rework argument boxing/result unboxing logic for LF interpretation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1054
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1055
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1056
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1057
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1058
        Object[] args = new Object[invokeBasic.type().parameterCount()];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1059
        args[0] = names[GET_COLLECT_ARGS];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1060
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
  1061
        names[BOXED_ARGS] = new Name(new NamedFunction(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1062
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1063
        // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1064
        Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1065
        names[TRY_CATCH] = new Name(getFunction(NF_guardWithCatch), gwcArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1066
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1067
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1068
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1069
        Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1070
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1071
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  1072
        lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.GUARD_WITH_CATCH);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1073
24696
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 24572
diff changeset
  1074
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1075
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1076
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1077
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1078
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1079
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1080
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1081
        MethodType type = target.type();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1082
        LambdaForm form = makeGuardWithCatchForm(type.basicType());
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1083
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
  1084
        // Prepare auxiliary method handles used during LambdaForm interpretation.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1085
        // Box arguments and wrap them into Object[]: ValueConversions.array().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1086
        MethodType varargsType = type.changeReturnType(Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1087
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1088
        MethodHandle unboxResult = unboxResultHandle(type.returnType());
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1089
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1090
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1091
        BoundMethodHandle mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1092
        try {
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47471
diff changeset
  1093
            mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) exType,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1094
                    (Object) catcher, (Object) collectArgs, (Object) unboxResult);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1095
        } catch (Throwable ex) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1096
            throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1097
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1098
        assert(mh.type() == type);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1099
        return mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1100
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1101
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1102
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1103
     * Intrinsified during LambdaForm compilation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1104
     * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1105
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1106
    @LambdaForm.Hidden
23059
4aca6147df88 8036732: Fix raw warning in java.lang.invoke.MethodHandleImpl.java
darcy
parents: 23039
diff changeset
  1107
    static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1108
                                 Object... av) throws Throwable {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1109
        // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1110
        try {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1111
            return target.asFixedArity().invokeWithArguments(av);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1112
        } catch (Throwable t) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1113
            if (!exType.isInstance(t)) throw t;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1114
            return catcher.asFixedArity().invokeWithArguments(prepend(av, t));
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1115
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1116
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1117
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1118
    /** Prepend elements to an array. */
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1119
    @LambdaForm.Hidden
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1120
    private static Object[] prepend(Object[] array, Object... elems) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1121
        int nArray = array.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1122
        int nElems = elems.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1123
        Object[] newArray = new Object[nArray + nElems];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1124
        System.arraycopy(elems, 0, newArray, 0, nElems);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1125
        System.arraycopy(array, 0, newArray, nElems, nArray);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1126
        return newArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1127
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1128
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1129
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1130
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1131
        assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1132
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1133
        if (arity > 1) {
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26467
diff changeset
  1134
            MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
  1135
            mh = MethodHandles.dropArguments(mh, 1, Arrays.copyOfRange(type.parameterArray(), 1, arity));
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26467
diff changeset
  1136
            return mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1137
        }
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1138
        return makePairwiseConvert(getFunction(NF_throwException).resolvedHandle(), type, false, true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1139
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1140
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1141
    static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1142
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1143
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1144
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1145
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1146
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1147
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1148
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1149
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1150
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1151
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1152
        MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1153
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1154
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1155
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1156
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1157
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1158
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1159
            throw new InternalError(method.toString());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1160
        mh = mh.withInternalMemberName(method, false);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37340
diff changeset
  1161
        mh = mh.withVarargs(true);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1162
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1163
        FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1164
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1165
    }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1166
    static MethodHandle fakeVarHandleInvoke(MemberName method) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1167
        // TODO caching, is it necessary?
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1168
        MethodType type = MethodType.methodType(method.getReturnType(), UnsupportedOperationException.class,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1169
                                                VarHandle.class, Object[].class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1170
        MethodHandle mh = throwException(type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1171
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke VarHandle"));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1172
        if (!method.getInvocationType().equals(mh.type()))
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1173
            throw new InternalError(method.toString());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1174
        mh = mh.withInternalMemberName(method, false);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1175
        mh = mh.asVarargsCollector(Object[].class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1176
        assert(method.isVarargs());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1177
        return mh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1178
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1179
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1180
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1181
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1182
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1183
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1184
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1185
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1186
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1187
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1188
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1189
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1190
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1191
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1192
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1193
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1194
    // That way we can lazily load the code and set up the constants.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1195
    private static class BindCaller {
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1196
        private static MethodType INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1197
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1198
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1199
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
47471
304ef03403b1 8190323: "the the" typos
rriggs
parents: 47294
diff changeset
  1200
            // Code in the boot layer should now be careful while creating method handles or
42333
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1201
            // functional interface instances created from method references to @CallerSensitive  methods,
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1202
            // it needs to be ensured the handles or interface instances are kept safe and are not passed
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1203
            // from the boot layer to untrusted code.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1204
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1205
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1206
                       hostClass.isPrimitive() ||
42333
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1207
                       hostClass.getName().startsWith("java.lang.invoke."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1208
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1209
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1210
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1211
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1212
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1213
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1214
            return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1215
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1216
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1217
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1218
            try {
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1219
                Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, INJECTED_INVOKER_TEMPLATE, null);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1220
                assert checkInjectedInvoker(hostClass, invokerClass);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1221
                return IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1222
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1223
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1224
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1225
        }
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1226
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1227
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1228
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1229
                return makeInjectedInvoker(hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1230
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1231
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1232
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1233
        // Adapt mh so that it can be called directly from an injected invoker:
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1234
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1235
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1236
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1237
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1238
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1239
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1240
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1241
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1242
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1243
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1244
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1245
        // Undo the adapter effect of prepareForInvoker:
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1246
        private static MethodHandle restoreToType(MethodHandle vamh,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1247
                                                  MethodHandle original,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1248
                                                  Class<?> hostClass) {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1249
            MethodType type = original.type();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1250
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1251
            MemberName member = original.internalMemberName();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1252
            mh = mh.asType(type);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1253
            mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1254
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1255
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1256
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1257
        private static boolean checkInjectedInvoker(Class<?> hostClass, Class<?> invokerClass) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1258
            assert (hostClass.getClassLoader() == invokerClass.getClassLoader()) : hostClass.getName()+" (CL)";
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1259
            try {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1260
                assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1261
            } catch (SecurityException ex) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1262
                // Self-check was blocked by security manager. This is OK.
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1263
            }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1264
            try {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1265
                // Test the invoker to ensure that it really injects into the right place.
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1266
                MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1267
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1268
                return (boolean)invoker.invoke(vamh, new Object[]{ invokerClass });
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1269
            } catch (Throwable ex) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1270
                throw new InternalError(ex);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1271
            }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1272
        }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1273
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1274
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1275
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1276
            final Class<?> THIS_CLASS = BindCaller.class;
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1277
            assert(checkCallerClass(THIS_CLASS));
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1278
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1279
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1280
                    .findStatic(THIS_CLASS, "checkCallerClass",
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1281
                                MethodType.methodType(boolean.class, Class.class));
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1282
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS));
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1283
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1284
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1285
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1286
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1287
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
  1288
        @CallerSensitive
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 39342
diff changeset
  1289
        @ForceInline // to ensure Reflection.getCallerClass optimization
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1290
        private static boolean checkCallerClass(Class<?> expected) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1291
            // This method is called via MH_checkCallerClass and so it's correct to ask for the immediate caller here.
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
  1292
            Class<?> actual = Reflection.getCallerClass();
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1293
            if (actual != expected)
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1294
                throw new InternalError("found " + actual.getName() + ", expected " + expected.getName());
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1295
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1296
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1297
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1298
        private static final byte[] INJECTED_INVOKER_TEMPLATE = generateInvokerTemplate();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1299
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1300
        /** Produces byte code for a class that is used as an injected invoker. */
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1301
        private static byte[] generateInvokerTemplate() {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1302
            ClassWriter cw = new ClassWriter(0);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1303
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1304
            // private static class InjectedInvoker {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1305
            //     @Hidden
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1306
            //     static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1307
            //        return vamh.invokeExact(args);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1308
            //     }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1309
            // }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1310
            cw.visit(52, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1311
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1312
            MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1313
                          "(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1314
                          null, null);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1315
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1316
            // Suppress invoker method in stack traces.
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1317
            AnnotationVisitor av0 = mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1318
            av0.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1319
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1320
            mv.visitCode();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1321
            mv.visitVarInsn(ALOAD, 0);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1322
            mv.visitVarInsn(ALOAD, 1);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1323
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1324
                               "([Ljava/lang/Object;)Ljava/lang/Object;", false);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1325
            mv.visitInsn(ARETURN);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1326
            mv.visitMaxs(2, 2);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1327
            mv.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1328
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1329
            cw.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1330
            return cw.toByteArray();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1331
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1332
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1333
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1334
    /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1335
    private static final class WrappedMember extends DelegatingMethodHandle {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1336
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1337
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1338
        private final Class<?> callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1339
        private final boolean isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1340
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1341
        private WrappedMember(MethodHandle target, MethodType type,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1342
                              MemberName member, boolean isInvokeSpecial,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1343
                              Class<?> callerClass) {
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1344
            super(type, target);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1345
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1346
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1347
            this.callerClass = callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1348
            this.isInvokeSpecial = isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1349
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1350
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1351
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1352
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1353
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1354
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1355
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1356
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1357
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1358
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1359
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1360
        boolean isInvokeSpecial() {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1361
            return isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1362
        }
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1363
        @Override
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1364
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1365
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1366
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1367
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1368
        public MethodHandle asTypeUncached(MethodType newType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1369
            // This MH is an alias for target, except for the MemberName
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1370
            // Drop the MemberName if there is any conversion.
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1371
            return asTypeCache = target.asType(newType);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1372
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1373
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1374
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1375
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1376
        if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1377
            return target;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1378
        return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1379
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1380
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1381
    /** Intrinsic IDs */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1382
    /*non-public*/
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1383
    enum Intrinsic {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1384
        SELECT_ALTERNATIVE,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1385
        GUARD_WITH_CATCH,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1386
        TRY_FINALLY,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1387
        LOOP,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1388
        NEW_ARRAY,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1389
        ARRAY_LOAD,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1390
        ARRAY_STORE,
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  1391
        ARRAY_LENGTH,
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1392
        IDENTITY,
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  1393
        ZERO,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1394
        NONE // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1395
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1396
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1397
    /** Mark arbitrary method handle as intrinsic.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1398
     * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1399
    static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1400
        private final MethodHandle target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1401
        private final Intrinsic intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1402
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1403
        IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1404
            super(target.type(), target);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1405
            this.target = target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1406
            this.intrinsicName = intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1407
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1408
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1409
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1410
        protected MethodHandle getTarget() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1411
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1412
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1413
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1414
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1415
        Intrinsic intrinsicName() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1416
            return intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1417
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1418
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1419
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1420
        public MethodHandle asTypeUncached(MethodType newType) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1421
            // This MH is an alias for target, except for the intrinsic name
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1422
            // Drop the name if there is any conversion.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1423
            return asTypeCache = target.asType(newType);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1424
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1425
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1426
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1427
        String internalProperties() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1428
            return super.internalProperties() +
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1429
                    "\n& Intrinsic="+intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1430
        }
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1431
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1432
        @Override
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1433
        public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1434
            if (intrinsicName == Intrinsic.IDENTITY) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1435
                MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength);
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1436
                MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1437
                return newArray.asType(resultType);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1438
            }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1439
            return super.asCollector(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1440
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1441
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1442
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1443
    static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1444
        if (intrinsicName == target.intrinsicName())
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1445
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1446
        return new IntrinsicMethodHandle(target, intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1447
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1448
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1449
    static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1450
        return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1451
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1452
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1453
    /// Collection of multiple arguments.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1454
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1455
    private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1456
        MethodType type = MethodType.genericMethodType(nargs)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1457
                .changeReturnType(rtype)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1458
                .insertParameterTypes(0, ptypes);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1459
        try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1460
            return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1461
        } catch (ReflectiveOperationException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1462
            return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1463
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1464
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1465
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1466
    private static final Object[] NO_ARGS_ARRAY = {};
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1467
    private static Object[] makeArray(Object... args) { return args; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1468
    private static Object[] array() { return NO_ARGS_ARRAY; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1469
    private static Object[] array(Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1470
                { return makeArray(a0); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1471
    private static Object[] array(Object a0, Object a1)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1472
                { return makeArray(a0, a1); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1473
    private static Object[] array(Object a0, Object a1, Object a2)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1474
                { return makeArray(a0, a1, a2); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1475
    private static Object[] array(Object a0, Object a1, Object a2, Object a3)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1476
                { return makeArray(a0, a1, a2, a3); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1477
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1478
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1479
                { return makeArray(a0, a1, a2, a3, a4); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1480
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1481
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1482
                { return makeArray(a0, a1, a2, a3, a4, a5); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1483
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1484
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1485
                { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1486
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1487
                                  Object a4, Object a5, Object a6, Object a7)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1488
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1489
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1490
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1491
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1492
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1493
    private static Object[] array(Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1494
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1495
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1496
                { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
27350
94a990d3754a 8062180: MethodHandleImpl.makeArrays throws and swallows java.lang.NoSuchFieldError in normal flow
redestad
parents: 27295
diff changeset
  1497
94a990d3754a 8062180: MethodHandleImpl.makeArrays throws and swallows java.lang.NoSuchFieldError in normal flow
redestad
parents: 27295
diff changeset
  1498
    private static final int ARRAYS_COUNT = 11;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1499
    private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1500
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1501
    // filling versions of the above:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1502
    // using Integer len instead of int len and no varargs to avoid bootstrapping problems
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1503
    private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1504
        Object[] a = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1505
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1506
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1507
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1508
    private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1509
        Object[] a = Arrays.copyOf(example, len);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1510
        assert(a.getClass() != Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1511
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1512
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1513
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1514
    private static void fillWithArguments(Object[] a, int pos, Object... args) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1515
        System.arraycopy(args, 0, a, pos, args.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1516
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1517
    // using Integer pos instead of int pos to avoid bootstrapping problems
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1518
    private static Object[] fillArray(Integer pos, Object[] a, Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1519
                { fillWithArguments(a, pos, a0); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1520
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1521
                { fillWithArguments(a, pos, a0, a1); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1522
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1523
                { fillWithArguments(a, pos, a0, a1, a2); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1524
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1525
                { fillWithArguments(a, pos, a0, a1, a2, a3); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1526
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1527
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1528
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1529
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1530
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1531
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1532
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1533
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1534
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1535
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1536
                                  Object a4, Object a5, Object a6, Object a7)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1537
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1538
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1539
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1540
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1541
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1542
    private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1543
                                  Object a4, Object a5, Object a6, Object a7,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1544
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1545
                { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1546
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1547
    private static final int FILL_ARRAYS_COUNT = 11; // current number of fillArray methods
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1548
    private static final @Stable MethodHandle[] FILL_ARRAYS = new MethodHandle[FILL_ARRAYS_COUNT];
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1549
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1550
    private static MethodHandle getFillArray(int count) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1551
        assert (count > 0 && count < FILL_ARRAYS_COUNT);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1552
        MethodHandle mh = FILL_ARRAYS[count];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1553
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1554
            return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1555
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1556
        mh = findCollector("fillArray", count, Object[].class, Integer.class, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1557
        FILL_ARRAYS[count] = mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1558
        return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1559
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1560
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1561
    private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1562
        Object a = w.makeArray(boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1563
        w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1564
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1565
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1566
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1567
    /** Return a method handle that takes the indicated number of Object
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1568
     *  arguments and returns an Object array of them, as if for varargs.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1569
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1570
    static MethodHandle varargsArray(int nargs) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1571
        MethodHandle mh = ARRAYS[nargs];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1572
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1573
            return mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1574
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1575
        if (nargs < ARRAYS_COUNT) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1576
            mh = findCollector("array", nargs, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1577
        } else {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1578
            mh = buildVarargsArray(getConstantHandle(MH_fillNewArray),
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1579
                    getConstantHandle(MH_arrayIdentity), nargs);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1580
        }
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1581
        assert(assertCorrectArity(mh, nargs));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1582
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1583
        return ARRAYS[nargs] = mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1584
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1585
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1586
    private static boolean assertCorrectArity(MethodHandle mh, int arity) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1587
        assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1588
        return true;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1589
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1590
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1591
    // Array identity function (used as getConstantHandle(MH_arrayIdentity)).
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1592
    static <T> T[] identity(T[] x) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1593
        return x;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1594
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1595
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1596
    private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1597
        // Build up the result mh as a sequence of fills like this:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1598
        //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1599
        // The various fill(_,10*I,___*[J]) are reusable.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1600
        int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1601
        int rightLen = nargs - leftLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1602
        MethodHandle leftCollector = newArray.bindTo(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1603
        leftCollector = leftCollector.asCollector(Object[].class, leftLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1604
        MethodHandle mh = finisher;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1605
        if (rightLen > 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1606
            MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1607
            if (mh.equals(getConstantHandle(MH_arrayIdentity)))
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1608
                mh = rightFiller;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1609
            else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1610
                mh = MethodHandles.collectArguments(mh, 0, rightFiller);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1611
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1612
        if (mh.equals(getConstantHandle(MH_arrayIdentity)))
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1613
            mh = leftCollector;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1614
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1615
            mh = MethodHandles.collectArguments(mh, 0, leftCollector);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1616
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1617
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1618
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1619
    private static final int LEFT_ARGS = FILL_ARRAYS_COUNT - 1;
35252
754852fba784 8144223: Move j.l.invoke.{ForceInline, DontInline, Stable} to jdk.internal.vm.annotation package
psandoz
parents: 33874
diff changeset
  1620
    private static final @Stable MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY + 1];
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1621
    /** fill_array_to_right(N).invoke(a, argL..arg[N-1])
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1622
     *  fills a[L]..a[N-1] with corresponding arguments,
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1623
     *  and then returns a.  The value L is a global constant (LEFT_ARGS).
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1624
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1625
    private static MethodHandle fillToRight(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1626
        MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1627
        if (filler != null)  return filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1628
        filler = buildFiller(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1629
        assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1630
        return FILL_ARRAY_TO_RIGHT[nargs] = filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1631
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1632
    private static MethodHandle buildFiller(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1633
        if (nargs <= LEFT_ARGS)
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1634
            return getConstantHandle(MH_arrayIdentity);  // no args to fill; return the array unchanged
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1635
        // we need room for both mh and a in mh.invoke(a, arg*[nargs])
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1636
        final int CHUNK = LEFT_ARGS;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1637
        int rightLen = nargs % CHUNK;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1638
        int midLen = nargs - rightLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1639
        if (rightLen == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1640
            midLen = nargs - (rightLen = CHUNK);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1641
            if (FILL_ARRAY_TO_RIGHT[midLen] == null) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1642
                // build some precursors from left to right
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1643
                for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1644
                    if (j > LEFT_ARGS)  fillToRight(j);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1645
            }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1646
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1647
        if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1648
        assert(rightLen > 0);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1649
        MethodHandle midFill = fillToRight(midLen);  // recursive fill
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1650
        MethodHandle rightFill = getFillArray(rightLen).bindTo(midLen);  // [midLen..nargs-1]
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1651
        assert(midFill.type().parameterCount()   == 1 + midLen - LEFT_ARGS);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1652
        assert(rightFill.type().parameterCount() == 1 + rightLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1653
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1654
        // Combine the two fills:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1655
        //   right(mid(a, x10..x19), x20..x23)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1656
        // The final product will look like this:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1657
        //   right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1658
        if (midLen == LEFT_ARGS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1659
            return rightFill;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1660
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1661
            return MethodHandles.collectArguments(rightFill, 0, midFill);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1662
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1663
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1664
    static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1665
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1666
    /** Return a method handle that takes the indicated number of
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1667
     *  typed arguments and returns an array of them.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1668
     *  The type argument is the array type.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1669
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1670
    static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1671
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1672
        if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1673
        // FIXME: Need more special casing and caching here.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1674
        if (nargs >= MAX_JVM_ARITY/2 - 1) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1675
            int slots = nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1676
            final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1677
            if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1678
                slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1679
            if (slots > MAX_ARRAY_SLOTS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1680
                throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1681
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1682
        if (elemType == Object.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1683
            return varargsArray(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1684
        // other cases:  primitive arrays, subtypes of Object[]
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
  1685
        MethodHandle cache[] = Makers.TYPED_COLLECTORS.get(elemType);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1686
        MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1687
        if (mh != null)  return mh;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1688
        if (nargs == 0) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1689
            Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1690
            mh = MethodHandles.constant(arrayType, example);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1691
        } else if (elemType.isPrimitive()) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1692
            MethodHandle builder = getConstantHandle(MH_fillNewArray);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1693
            MethodHandle producer = buildArrayProducer(arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1694
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1695
        } else {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1696
            Class<? extends Object[]> objArrayType = arrayType.asSubclass(Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1697
            Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1698
            MethodHandle builder = getConstantHandle(MH_fillNewTypedArray).bindTo(example);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1699
            MethodHandle producer = getConstantHandle(MH_arrayIdentity); // must be weakly typed
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1700
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1701
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1702
        mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1703
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1704
        assert(assertCorrectArity(mh, nargs));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1705
        if (nargs < cache.length)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1706
            cache[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1707
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1708
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1709
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1710
    private static MethodHandle buildArrayProducer(Class<?> arrayType) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1711
        Class<?> elemType = arrayType.getComponentType();
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1712
        assert(elemType.isPrimitive());
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1713
        return getConstantHandle(MH_copyAsPrimitiveArray).bindTo(Wrapper.forPrimitiveType(elemType));
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1714
    }
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1715
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1716
    /*non-public*/ static void assertSame(Object mh1, Object mh2) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1717
        if (mh1 != mh2) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1718
            String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1719
                    mh1, ((MethodHandle)mh1).form,
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1720
                    mh2, ((MethodHandle)mh2).form);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1721
            throw newInternalError(msg);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1722
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1723
    }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1724
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1725
    // Local constant functions:
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1726
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
  1727
    /* non-public */
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1728
    static final byte NF_checkSpreadArgument = 0,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1729
            NF_guardWithCatch = 1,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1730
            NF_throwException = 2,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1731
            NF_tryFinally = 3,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1732
            NF_loop = 4,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1733
            NF_profileBoolean = 5,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1734
            NF_LIMIT = 6;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1735
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1736
    private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1737
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1738
    static NamedFunction getFunction(byte func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1739
        NamedFunction nf = NFS[func];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1740
        if (nf != null) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1741
            return nf;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1742
        }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1743
        return NFS[func] = createFunction(func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1744
    }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1745
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1746
    private static NamedFunction createFunction(byte func) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1747
        try {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1748
            switch (func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1749
                case NF_checkSpreadArgument:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1750
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1751
                            .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1752
                case NF_guardWithCatch:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1753
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1754
                            .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1755
                                    MethodHandle.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1756
                case NF_tryFinally:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1757
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1758
                            .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1759
                case NF_loop:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1760
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1761
                            .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1762
                case NF_throwException:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1763
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1764
                            .getDeclaredMethod("throwException", Throwable.class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1765
                case NF_profileBoolean:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1766
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1767
                            .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1768
                default:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1769
                    throw new InternalError("Undefined function: " + func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1770
            }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1771
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1772
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1773
        }
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1774
    }
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1775
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1776
    static {
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1777
        SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1778
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1779
            public Object newMemberName() {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1780
                return new MemberName();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1781
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1782
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1783
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1784
            public String getName(Object mname) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1785
                MemberName memberName = (MemberName)mname;
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1786
                return memberName.getName();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1787
            }
47818
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47471
diff changeset
  1788
            @Override
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47471
diff changeset
  1789
            public Class<?> getDeclaringClass(Object mname) {
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47471
diff changeset
  1790
                MemberName memberName = (MemberName)mname;
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47471
diff changeset
  1791
                return memberName.getDeclaringClass();
2f6ab27efb60 8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint
bchristi
parents: 47471
diff changeset
  1792
            }
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1793
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1794
            @Override
47294
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1795
            public MethodType getMethodType(Object mname) {
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1796
                MemberName memberName = (MemberName)mname;
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1797
                return memberName.getMethodType();
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1798
            }
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1799
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1800
            @Override
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1801
            public String getMethodDescriptor(Object mname) {
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1802
                MemberName memberName = (MemberName)mname;
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1803
                return memberName.getMethodDescriptor();
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1804
            }
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1805
7d67bb6b0599 8186050: StackFrame should provide the method signature
mchung
parents: 47250
diff changeset
  1806
            @Override
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1807
            public boolean isNative(Object mname) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1808
                MemberName memberName = (MemberName)mname;
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1809
                return memberName.isNative();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1810
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1811
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1812
            @Override
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1813
            public byte[] generateDirectMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1814
                    String className, MethodType[] methodTypes, int[] types) {
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1815
                return GenerateJLIClassesHelper
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1816
                        .generateDirectMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1817
                                className, methodTypes, types);
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1818
            }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1819
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1820
            @Override
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1821
            public byte[] generateDelegatingMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1822
                    String className, MethodType[] methodTypes) {
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1823
                return GenerateJLIClassesHelper
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1824
                        .generateDelegatingMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1825
                                className, methodTypes);
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1826
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1827
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1828
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1829
            public Map.Entry<String, byte[]> generateConcreteBMHClassBytes(
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1830
                    final String types) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1831
                return GenerateJLIClassesHelper
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1832
                        .generateConcreteBMHClassBytes(types);
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1833
            }
40419
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1834
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1835
            @Override
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1836
            public byte[] generateBasicFormsClassBytes(final String className) {
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1837
                return GenerateJLIClassesHelper
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1838
                        .generateBasicFormsClassBytes(className);
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1839
            }
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1840
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1841
            @Override
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1842
            public byte[] generateInvokersHolderClassBytes(final String className,
49240
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47856
diff changeset
  1843
                    MethodType[] invokerMethodTypes,
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47856
diff changeset
  1844
                    MethodType[] callSiteMethodTypes) {
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1845
                return GenerateJLIClassesHelper
49240
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47856
diff changeset
  1846
                        .generateInvokersHolderClassBytes(className,
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47856
diff changeset
  1847
                                invokerMethodTypes, callSiteMethodTypes);
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1848
            }
49240
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 47856
diff changeset
  1849
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1850
        });
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1851
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1852
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1853
    /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1854
    private static MethodHandle unboxResultHandle(Class<?> returnType) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1855
        if (returnType.isPrimitive()) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1856
            if (returnType == void.class) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1857
                return ValueConversions.ignore();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1858
            } else {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1859
                Wrapper w = Wrapper.forPrimitiveType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1860
                return ValueConversions.unboxExact(w);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1861
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1862
        } else {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1863
            return MethodHandles.identity(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1864
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1865
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1866
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1867
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1868
     * Assembles a loop method handle from the given handles and type information.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1869
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1870
     * @param tloop the return type of the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1871
     * @param targs types of the arguments to be passed to the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1872
     * @param init sanitized array of initializers for loop-local variables.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1873
     * @param step sanitited array of loop bodies.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1874
     * @param pred sanitized array of predicates.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1875
     * @param fini sanitized array of loop finalizers.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1876
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1877
     * @return a handle that, when invoked, will execute the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1878
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1879
    static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1880
                                 List<MethodHandle> pred, List<MethodHandle> fini) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1881
        MethodType type = MethodType.methodType(tloop, targs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1882
        BasicType[] initClauseTypes =
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1883
                init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1884
        LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1885
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1886
        // Prepare auxiliary method handles used during LambdaForm interpretation.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1887
        // Box arguments and wrap them into Object[]: ValueConversions.array().
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1888
        MethodType varargsType = type.changeReturnType(Object[].class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1889
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1890
        MethodHandle unboxResult = unboxResultHandle(tloop);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1891
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1892
        LoopClauses clauseData =
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1893
                new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)});
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1894
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1895
        BoundMethodHandle mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1896
        try {
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47471
diff changeset
  1897
            mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData,
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1898
                    (Object) collectArgs, (Object) unboxResult);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1899
        } catch (Throwable ex) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1900
            throw uncaughtException(ex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1901
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1902
        assert(mh.type() == type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1903
        return mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1904
    }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1905
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1906
    private static MethodHandle[] toArray(List<MethodHandle> l) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1907
        return l.toArray(new MethodHandle[0]);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1908
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1909
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1910
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1911
     * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1912
     * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1913
     * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1914
     * {@code t12}):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1915
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1916
     *  loop=Lambda(a0:L,a1:L)=>{
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1917
     *    t2:L=BoundMethodHandle$Species_L3.argL0(a0:L);    // LoopClauses holding init, step, pred, fini handles
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1918
     *    t3:L=BoundMethodHandle$Species_L3.argL1(a0:L);    // helper handle to box the arguments into an Object[]
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1919
     *    t4:L=BoundMethodHandle$Species_L3.argL2(a0:L);    // helper handle to unbox the result
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1920
     *    t5:L=MethodHandle.invokeBasic(t3:L,a1:L);         // box the arguments into an Object[]
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1921
     *    t6:L=MethodHandleImpl.loop(null,t2:L,t3:L);       // call the loop executor
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1922
     *    t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L}    // unbox the result; return the result
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1923
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1924
     * <p>
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1925
     * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1926
     * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1927
     * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1928
     * ({@code ValueConversions.unbox()}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1929
     * <p>
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1930
     * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1931
     * forms among loop combinators with the same basic type.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1932
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1933
     * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1934
     * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1935
     * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1936
     * bytecode generation}.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1937
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1938
    private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1939
        MethodType lambdaType = basicType.invokerType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1940
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1941
        final int THIS_MH = 0;  // the BMH_LLL
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1942
        final int ARG_BASE = 1; // start of incoming arguments
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1943
        final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1944
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1945
        int nameCursor = ARG_LIMIT;
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1946
        final int GET_CLAUSE_DATA = nameCursor++;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1947
        final int GET_COLLECT_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1948
        final int GET_UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1949
        final int BOXED_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1950
        final int LOOP = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1951
        final int UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1952
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1953
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1954
        if (lform == null) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1955
            Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1956
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1957
            BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1958
            names[THIS_MH] = names[THIS_MH].withConstraint(data);
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1959
            names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1960
            names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1961
            names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1962
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1963
            // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1964
            MethodType collectArgsType = basicType.changeReturnType(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1965
            MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1966
            Object[] args = new Object[invokeBasic.type().parameterCount()];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1967
            args[0] = names[GET_COLLECT_ARGS];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1968
            System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
  1969
            names[BOXED_ARGS] = new Name(new NamedFunction(invokeBasic, Intrinsic.LOOP), args);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1970
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1971
            // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1972
            Object[] lArgs =
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1973
                    new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1974
                            names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1975
            names[LOOP] = new Name(getFunction(NF_loop), lArgs);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1976
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1977
            // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1978
            MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1979
            Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]};
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1980
            names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1981
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1982
            lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  1983
                    new LambdaForm(lambdaType.parameterCount(), names, Kind.LOOP));
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1984
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1985
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1986
        // BOXED_ARGS is the index into the names array where the loop idiom starts
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1987
        return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1988
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1989
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1990
    static class LoopClauses {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1991
        @Stable final MethodHandle[][] clauses;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1992
        LoopClauses(MethodHandle[][] clauses) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1993
            assert clauses.length == 4;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1994
            this.clauses = clauses;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1995
        }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1996
        @Override
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1997
        public String toString() {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1998
            StringBuffer sb = new StringBuffer("LoopClauses -- ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1999
            for (int i = 0; i < 4; ++i) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2000
                if (i > 0) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2001
                    sb.append("       ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2002
                }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2003
                sb.append('<').append(i).append(">: ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2004
                MethodHandle[] hs = clauses[i];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2005
                for (int j = 0; j < hs.length; ++j) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2006
                    if (j > 0) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2007
                        sb.append("          ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2008
                    }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2009
                    sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2010
                }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2011
            }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2012
            sb.append(" --\n");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2013
            return sb.toString();
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2014
        }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2015
    }
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2016
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2017
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2018
     * Intrinsified during LambdaForm compilation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2019
     * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2020
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2021
    @LambdaForm.Hidden
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2022
    static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2023
        final MethodHandle[] init = clauseData.clauses[0];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2024
        final MethodHandle[] step = clauseData.clauses[1];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2025
        final MethodHandle[] pred = clauseData.clauses[2];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2026
        final MethodHandle[] fini = clauseData.clauses[3];
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2027
        int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2028
        int nArgs = init[0].type().parameterCount();
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2029
        Object[] varsAndArgs = new Object[varSize + nArgs];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2030
        for (int i = 0, v = 0; i < init.length; ++i) {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2031
            MethodHandle ih = init[i];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2032
            if (ih.type().returnType() == void.class) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2033
                ih.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2034
            } else {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2035
                varsAndArgs[v++] = ih.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2036
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2037
        }
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2038
        System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2039
        final int nSteps = step.length;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2040
        for (; ; ) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2041
            for (int i = 0, v = 0; i < nSteps; ++i) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2042
                MethodHandle p = pred[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2043
                MethodHandle s = step[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2044
                MethodHandle f = fini[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2045
                if (s.type().returnType() == void.class) {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2046
                    s.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2047
                } else {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2048
                    varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2049
                }
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2050
                if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2051
                    return f.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2052
                }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2053
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2054
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2055
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2056
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2057
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2058
     * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2059
     * MethodHandle) counting loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2060
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2061
     * @param limit the upper bound of the parameter, statically bound at loop creation time.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2062
     * @param counter the counter parameter, passed in during loop execution.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2063
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2064
     * @return whether the counter has reached the limit.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2065
     */
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2066
    static boolean countedLoopPredicate(int limit, int counter) {
40418
fb874cf24974 8164102: MethodHandles.countedLoop/4 works incorrect for start/end = Integer.MAX_VALUE
mhaupt
parents: 40408
diff changeset
  2067
        return counter < limit;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2068
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2069
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2070
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2071
     * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2072
     * MethodHandle) counting loops} to increment the counter.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2073
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2074
     * @param limit the upper bound of the loop counter (ignored).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2075
     * @param counter the loop counter.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2076
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2077
     * @return the loop counter incremented by 1.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2078
     */
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2079
    static int countedLoopStep(int limit, int counter) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2080
        return counter + 1;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2081
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2082
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2083
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2084
     * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2085
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2086
     * @param it the {@link Iterable} over which the loop iterates.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2087
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2088
     * @return an {@link Iterator} over the argument's elements.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2089
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2090
    static Iterator<?> initIterator(Iterable<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2091
        return it.iterator();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2092
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2093
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2094
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2095
     * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2096
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2097
     * @param it the iterator to be checked.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2098
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2099
     * @return {@code true} iff there are more elements to iterate over.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2100
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2101
    static boolean iteratePredicate(Iterator<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2102
        return it.hasNext();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2103
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2104
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2105
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2106
     * This method is bound as the step for retrieving the current value from the iterator in {@linkplain
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2107
     * MethodHandles#iteratedLoop iterating loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2108
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2109
     * @param it the iterator.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2110
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2111
     * @return the next element from the iterator.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2112
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2113
    static Object iterateNext(Iterator<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2114
        return it.next();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2115
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2116
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2117
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2118
     * Makes a {@code try-finally} handle that conforms to the type constraints.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2119
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2120
     * @param target the target to execute in a {@code try-finally} block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2121
     * @param cleanup the cleanup to execute in the {@code finally} block.
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2122
     * @param rtype the result type of the entire construct.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2123
     * @param argTypes the types of the arguments.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2124
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2125
     * @return a handle on the constructed {@code try-finally} block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2126
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2127
    static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, List<Class<?>> argTypes) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2128
        MethodType type = MethodType.methodType(rtype, argTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2129
        LambdaForm form = makeTryFinallyForm(type.basicType());
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2130
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2131
        // Prepare auxiliary method handles used during LambdaForm interpretation.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2132
        // Box arguments and wrap them into Object[]: ValueConversions.array().
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2133
        MethodType varargsType = type.changeReturnType(Object[].class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2134
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2135
        MethodHandle unboxResult = unboxResultHandle(rtype);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2136
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2137
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2138
        BoundMethodHandle mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2139
        try {
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47471
diff changeset
  2140
            mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2141
                    (Object) collectArgs, (Object) unboxResult);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2142
        } catch (Throwable ex) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2143
            throw uncaughtException(ex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2144
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2145
        assert(mh.type() == type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2146
        return mh;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2147
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2148
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2149
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2150
     * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2151
     * {@code a1}, and a reference return type, with the return value represented by {@code t8}):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2152
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2153
     *  tryFinally=Lambda(a0:L,a1:L)=>{
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2154
     *    t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L);  // target method handle
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2155
     *    t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L);  // cleanup method handle
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2156
     *    t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L);  // helper handle to box the arguments into an Object[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2157
     *    t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L);  // helper handle to unbox the result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2158
     *    t6:L=MethodHandle.invokeBasic(t4:L,a1:L);         // box the arguments into an Object[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2159
     *    t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2160
     *    t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L}    // unbox the result; return the result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2161
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2162
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2163
     * {@code argL0} and {@code argL1} are the target and cleanup method handles.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2164
     * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2165
     * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2166
     * ({@code ValueConversions.unbox()}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2167
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2168
     * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2169
     * forms among tryFinally combinators with the same basic type.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2170
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2171
    private static LambdaForm makeTryFinallyForm(MethodType basicType) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2172
        MethodType lambdaType = basicType.invokerType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2173
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2174
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2175
        if (lform != null) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2176
            return lform;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2177
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2178
        final int THIS_MH      = 0;  // the BMH_LLLL
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2179
        final int ARG_BASE     = 1;  // start of incoming arguments
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2180
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2181
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2182
        int nameCursor = ARG_LIMIT;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2183
        final int GET_TARGET       = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2184
        final int GET_CLEANUP      = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2185
        final int GET_COLLECT_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2186
        final int GET_UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2187
        final int BOXED_ARGS       = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2188
        final int TRY_FINALLY      = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2189
        final int UNBOX_RESULT     = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2190
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2191
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2192
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2193
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2194
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2195
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2196
        names[GET_CLEANUP]      = new Name(data.getterFunction(1), names[THIS_MH]);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2197
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2198
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2199
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2200
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2201
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2202
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2203
        Object[] args = new Object[invokeBasic.type().parameterCount()];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2204
        args[0] = names[GET_COLLECT_ARGS];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2205
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
  2206
        names[BOXED_ARGS] = new Name(new NamedFunction(invokeBasic, Intrinsic.TRY_FINALLY), args);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2207
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2208
        // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2209
        Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]};
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  2210
        names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2211
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2212
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2213
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2214
        Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]};
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2215
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2216
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  2217
        lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TRY_FINALLY);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2218
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2219
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2220
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2221
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2222
    /**
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2223
     * Intrinsified during LambdaForm compilation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2224
     * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2225
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2226
    @LambdaForm.Hidden
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2227
    static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2228
        Throwable t = null;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2229
        Object r = null;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2230
        try {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2231
            r = target.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2232
        } catch (Throwable thrown) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2233
            t = thrown;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2234
            throw t;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2235
        } finally {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2236
            Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r);
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2237
            r = cleanup.invokeWithArguments(args);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2238
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2239
        return r;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2240
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2241
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2242
    // Indexes into constant method handles:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2243
    static final int
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2244
            MH_cast                  =  0,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2245
            MH_selectAlternative     =  1,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2246
            MH_copyAsPrimitiveArray  =  2,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2247
            MH_fillNewTypedArray     =  3,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2248
            MH_fillNewArray          =  4,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2249
            MH_arrayIdentity         =  5,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2250
            MH_countedLoopPred       =  6,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2251
            MH_countedLoopStep       =  7,
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2252
            MH_initIterator          =  8,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2253
            MH_iteratePred           =  9,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2254
            MH_iterateNext           = 10,
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2255
            MH_Array_newInstance     = 11,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2256
            MH_LIMIT                 = 12;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2257
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2258
    static MethodHandle getConstantHandle(int idx) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2259
        MethodHandle handle = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2260
        if (handle != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2261
            return handle;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2262
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2263
        return setCachedHandle(idx, makeConstantHandle(idx));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2264
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2265
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2266
    private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2267
        // Simulate a CAS, to avoid racy duplication of results.
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2268
        MethodHandle prev = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2269
        if (prev != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2270
            return prev;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2271
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2272
        HANDLES[idx] = method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2273
        return method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2274
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2275
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2276
    // Local constant method handles:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2277
    private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2278
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2279
    private static MethodHandle makeConstantHandle(int idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2280
        try {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2281
            switch (idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2282
                case MH_cast:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2283
                    return IMPL_LOOKUP.findVirtual(Class.class, "cast",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2284
                            MethodType.methodType(Object.class, Object.class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2285
                case MH_copyAsPrimitiveArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2286
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "copyAsPrimitiveArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2287
                            MethodType.methodType(Object.class, Wrapper.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2288
                case MH_arrayIdentity:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2289
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "identity",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2290
                            MethodType.methodType(Object[].class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2291
                case MH_fillNewArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2292
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2293
                            MethodType.methodType(Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2294
                case MH_fillNewTypedArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2295
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewTypedArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2296
                            MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2297
                case MH_selectAlternative:
45470
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
  2298
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
fad095de1919 8161207: remove extra MethodHandle subclass in MethodHandleImpl
rpressler
parents: 44591
diff changeset
  2299
                            MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2300
                case MH_countedLoopPred:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2301
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2302
                            MethodType.methodType(boolean.class, int.class, int.class));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2303
                case MH_countedLoopStep:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2304
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2305
                            MethodType.methodType(int.class, int.class, int.class));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2306
                case MH_initIterator:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2307
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2308
                            MethodType.methodType(Iterator.class, Iterable.class));
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2309
                case MH_iteratePred:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2310
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2311
                            MethodType.methodType(boolean.class, Iterator.class));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2312
                case MH_iterateNext:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2313
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2314
                            MethodType.methodType(Object.class, Iterator.class));
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2315
                case MH_Array_newInstance:
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2316
                    return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2317
                            MethodType.methodType(Object.class, Class.class, int.class));
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2318
            }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2319
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2320
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2321
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2322
        throw newInternalError("Unknown function index: " + idx);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2323
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2324
}