jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
author redestad
Tue, 11 Apr 2017 18:57:46 +0200
changeset 44591 b9bf065070fe
parent 44590 15a77e5b7612
child 45470 fad095de1919
permissions -rw-r--r--
8178387: Reduce memory churn when creating java.lang.invoke entities Reviewed-by: psandoz, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37340
diff changeset
     2
 * Copyright (c) 2008, 2016, 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
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
    28
import jdk.internal.misc.JavaLangInvokeAccess;
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
    29
import jdk.internal.misc.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;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   502
            this.asCollectorCache = target.asCollector(arrayType, 0);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   503
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   504
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   505
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   506
        public boolean isVarargsCollector() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   507
            return true;
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
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   510
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   511
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   512
            return target;
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
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   515
        @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   516
        public MethodHandle asFixedArity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   517
            return target;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   518
        }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   519
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   520
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   521
        MethodHandle setVarargs(MemberName member) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   522
            if (member.isVarargs())  return this;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   523
            return asFixedArity();
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
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
   526
        @Override
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   527
        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
   528
            MethodType type = this.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   529
            int collectArg = type.parameterCount() - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   530
            int newArity = newType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   531
            if (newArity == collectArg+1 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   532
                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
   533
                // 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
   534
                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
   535
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   536
            // check cache
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   537
            MethodHandle acc = asCollectorCache;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   538
            if (acc != null && acc.type().parameterCount() == newArity)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   539
                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
   540
            // build and cache a collector
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   541
            int arrayLength = newArity - collectArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   542
            MethodHandle collector;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   543
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   544
                collector = asFixedArity().asCollector(arrayType, arrayLength);
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   545
                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
   546
            } catch (IllegalArgumentException ex) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   547
                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
   548
            }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   549
            asCollectorCache = collector;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20495
diff changeset
   550
            return asTypeCache = collector.asType(newType);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   551
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   552
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   553
        @Override
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   554
        boolean viewAsTypeChecks(MethodType newType, boolean strict) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   555
            super.viewAsTypeChecks(newType, true);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   556
            if (strict) return true;
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   557
            // extra assertion for non-strict checks:
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   558
            assert (type().lastParameterType().getComponentType()
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   559
                    .isAssignableFrom(
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   560
                            newType.lastParameterType().getComponentType()))
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   561
                    : Arrays.asList(this, newType);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   562
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   563
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   564
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   565
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   566
    /** Factory method:  Spread selected argument. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   567
    static MethodHandle makeSpreadArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   568
                                            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
   569
        MethodType targetType = target.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   570
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   571
        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
   572
            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
   573
            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
   574
            targetType = targetType.changeParameterType(spreadArgPos + i, arg);
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   575
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   576
        target = target.asType(targetType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   577
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   578
        MethodType srcType = targetType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   579
                .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   580
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   581
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   582
        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
   583
        int nameCursor = lambdaType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   584
        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
   585
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   586
        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
   587
            Class<?> src = lambdaType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   588
            if (i == spreadArgPos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   589
                // Spread the array.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   590
                MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   591
                Name array = names[argIndex];
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   592
                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
   593
                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
   594
                    indexes[i] = nameCursor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   595
                    names[nameCursor++] = new Name(aload, array, j);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   596
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   597
            } else if (i < indexes.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   598
                indexes[i] = argIndex;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   599
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   600
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   601
        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
   602
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   603
        // 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
   604
        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
   605
        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
   606
            int idx = indexes[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   607
            targetArgs[i] = names[idx];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   608
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   609
        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
   610
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   611
        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
   612
        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
   613
    }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   614
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   615
    static void checkSpreadArgument(Object av, int n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   616
        if (av == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   617
            if (n == 0)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   618
        } else if (av instanceof Object[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   619
            int len = ((Object[])av).length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   620
            if (len == n)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   621
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   622
            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
   623
            if (len == n)  return;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   624
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   625
        // fall through to error:
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 20527
diff changeset
   626
        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
   627
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   628
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   629
    /** 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
   630
    static MethodHandle makeCollectArguments(MethodHandle target,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   631
                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
   632
        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
   633
        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
   634
        int collectArgCount = collectorType.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   635
        Class<?> collectValType = collectorType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   636
        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
   637
        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
   638
                .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   639
        if (!retainOriginalArgs) {                      // (a..., b...)=>r
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   640
            srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterArray());
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   641
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   642
        // 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
   643
        // 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
   644
        // 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
   645
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   646
        // Now build a LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   647
        MethodType lambdaType = srcType.invokerType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   648
        Name[] names = arguments(2, lambdaType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   649
        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
   650
        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
   651
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   652
        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
   653
        names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   654
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   655
        // 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
   656
        // 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
   657
        // 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
   658
        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
   659
        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
   660
        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
   661
        int chunk = collectArgPos;  // |headArgs|
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   662
        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
   663
        inputArgPos  += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   664
        targetArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   665
        if (collectValType != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   666
            targetArgs[targetArgPos++] = names[collectNamePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   667
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   668
        chunk = collectArgCount;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   669
        if (retainOriginalArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   670
            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
   671
            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
   672
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   673
        inputArgPos += chunk;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   674
        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
   675
        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
   676
        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
   677
        names[targetNamePos] = new Name(target, (Object[]) targetArgs);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   678
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   679
        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
   680
        return SimpleMethodHandle.make(srcType, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   681
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   682
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   683
    @LambdaForm.Hidden
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   684
    static
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   685
    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
   686
        if (testResult) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   687
            return target;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   688
        } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   689
            return fallback;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   690
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   691
    }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   692
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   693
    // 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
   694
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   695
    @jdk.internal.HotSpotIntrinsicCandidate
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   696
    static
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   697
    boolean profileBoolean(boolean result, int[] counters) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   698
        // 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
   699
        int idx = result ? 1 : 0;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   700
        try {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   701
            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
   702
        } catch (ArithmeticException e) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   703
            // 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
   704
            counters[idx] = counters[idx] / 2;
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   705
        }
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   706
        return result;
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   707
    }
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   708
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   709
    // 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
   710
    @LambdaForm.Hidden
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30365
diff changeset
   711
    @jdk.internal.HotSpotIntrinsicCandidate
30336
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   712
    static
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   713
    boolean isCompileConstant(Object obj) {
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   714
        return false;
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   715
    }
36774c81a366 8075263: MHI::checkCustomized isn't eliminated for inlined MethodHandles
vlivanov
parents: 29022
diff changeset
   716
9730
e4b334d47f4b 7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents: 9646
diff changeset
   717
    static
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   718
    MethodHandle makeGuardWithTest(MethodHandle test,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   719
                                   MethodHandle target,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   720
                                   MethodHandle fallback) {
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   721
        MethodType type = target.type();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   722
        assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   723
        MethodType basicType = type.basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   724
        LambdaForm form = makeGuardWithTestForm(basicType);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   725
        BoundMethodHandle mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   726
        try {
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   727
            if (PROFILE_GWT) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   728
                int[] counts = new int[2];
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   729
                mh = (BoundMethodHandle)
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   730
                        BoundMethodHandle.speciesData_LLLL().constructor().invokeBasic(type, form,
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   731
                                (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
   732
            } else {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   733
                mh = (BoundMethodHandle)
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   734
                        BoundMethodHandle.speciesData_LLL().constructor().invokeBasic(type, form,
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   735
                                (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
   736
            }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   737
        } catch (Throwable ex) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   738
            throw uncaughtException(ex);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   739
        }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   740
        assert(mh.type() == type);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   741
        return mh;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   742
    }
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   743
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   744
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   745
    static
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   746
    MethodHandle profile(MethodHandle target) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   747
        if (DONT_INLINE_THRESHOLD >= 0) {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   748
            return makeBlockInliningWrapper(target);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   749
        } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   750
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   751
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   752
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   753
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   754
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   755
     * 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
   756
     * Corresponding LambdaForm has @DontInline when compiled into bytecode.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   757
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   758
    static
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   759
    MethodHandle makeBlockInliningWrapper(MethodHandle target) {
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   760
        LambdaForm lform;
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   761
        if (DONT_INLINE_THRESHOLD > 0) {
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   762
            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
   763
        } else {
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   764
            lform = Makers.PRODUCE_REINVOKER_FORM.apply(target);
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   765
        }
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   766
        return new CountingWrapper(target, lform,
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   767
                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
   768
                                   DONT_INLINE_THRESHOLD);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   769
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   770
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   771
    private final static class Makers {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   772
        /** 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
   773
        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
   774
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   775
            public LambdaForm apply(MethodHandle target) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   776
                return DelegatingMethodHandle.makeReinvokerForm(target,
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   777
                                   MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, false,
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   778
                                   DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   779
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   780
        };
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   781
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   782
        /** Constructs simple reinvoker lambda form for a particular method handle */
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   783
        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
   784
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   785
            public LambdaForm apply(MethodHandle target) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   786
                return DelegatingMethodHandle.makeReinvokerForm(target,
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   787
                        MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   788
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   789
        };
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   790
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   791
        /** Maker of type-polymorphic varargs */
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   792
        static final ClassValue<MethodHandle[]> TYPED_COLLECTORS = new ClassValue<MethodHandle[]>() {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   793
            @Override
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   794
            protected MethodHandle[] computeValue(Class<?> type) {
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   795
                return new MethodHandle[MAX_JVM_ARITY + 1];
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   796
            }
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   797
        };
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
   798
    }
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   799
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   800
    /**
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   801
     * 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
   802
     * 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
   803
     * 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
   804
     * countingFormProducer & nonCountingFormProducer respectively.
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   805
     */
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   806
    static class CountingWrapper extends DelegatingMethodHandle {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   807
        private final MethodHandle target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   808
        private int count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   809
        private Function<MethodHandle, LambdaForm> countingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   810
        private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   811
        private volatile boolean isCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   812
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   813
        private CountingWrapper(MethodHandle target, LambdaForm lform,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   814
                                Function<MethodHandle, LambdaForm> countingFromProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   815
                                Function<MethodHandle, LambdaForm> nonCountingFormProducer,
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   816
                                int count) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   817
            super(target.type(), lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   818
            this.target = target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   819
            this.count = count;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   820
            this.countingFormProducer = countingFromProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   821
            this.nonCountingFormProducer = nonCountingFormProducer;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   822
            this.isCounting = (count > 0);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   823
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   824
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   825
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   826
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   827
        protected MethodHandle getTarget() {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   828
            return target;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   829
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   830
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   831
        @Override
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   832
        public MethodHandle asTypeUncached(MethodType newType) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   833
            MethodHandle newTarget = target.asType(newType);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   834
            MethodHandle wrapper;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   835
            if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   836
                LambdaForm lform;
29022
31bbb5dcc603 8071788: BlockInliningWrapper.asType() is broken
vlivanov
parents: 29021
diff changeset
   837
                lform = countingFormProducer.apply(newTarget);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   838
                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
   839
            } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   840
                wrapper = newTarget; // no need for a counting wrapper anymore
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   841
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   842
            return (asTypeCache = wrapper);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   843
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   844
36120
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   845
        // 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
   846
        private int invocations = CUSTOMIZE_THRESHOLD;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   847
        private void maybeCustomizeTarget() {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   848
            int c = invocations;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   849
            if (c >= 0) {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   850
                if (c == 1) {
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   851
                    target.customize();
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   852
                }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   853
                invocations = c - 1;
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   854
            }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   855
        }
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   856
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   857
        boolean countDown() {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   858
            int c = count;
36120
192b3ccf06ba 8148994: Replacing MH::invokeBasic with a direct call breaks LF customization
vlivanov
parents: 35259
diff changeset
   859
            maybeCustomizeTarget();
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   860
            if (c <= 1) {
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   861
                // 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
   862
                if (isCounting) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   863
                    isCounting = false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   864
                    return true;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   865
                } else {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   866
                    return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   867
                }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   868
            } else {
29021
cf13db21f970 8071787: Don't block inlining when DONT_INLINE_THRESHOLD=0
vlivanov
parents: 29020
diff changeset
   869
                count = c - 1;
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   870
                return false;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   871
            }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   872
        }
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
        @Hidden
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   875
        static void maybeStopCounting(Object o1) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   876
             CountingWrapper wrapper = (CountingWrapper) o1;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   877
             if (wrapper.countDown()) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   878
                 // 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
   879
                 LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   880
                 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
   881
                 wrapper.updateForm(lform);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   882
             }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   883
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   884
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   885
        static final NamedFunction NF_maybeStopCounting;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   886
        static {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   887
            Class<?> THIS_CLASS = CountingWrapper.class;
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   888
            try {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   889
                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
   890
            } catch (ReflectiveOperationException ex) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   891
                throw newInternalError(ex);
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   892
            }
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
    }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 27043
diff changeset
   895
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   896
    static
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   897
    LambdaForm makeGuardWithTestForm(MethodType basicType) {
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   898
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   899
        if (lform != null)  return lform;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   900
        final int THIS_MH      = 0;  // the BMH_LLL
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   901
        final int ARG_BASE     = 1;  // start of incoming arguments
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   902
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   903
        int nameCursor = ARG_LIMIT;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   904
        final int GET_TEST     = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   905
        final int GET_TARGET   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   906
        final int GET_FALLBACK = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   907
        final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   908
        final int CALL_TEST    = nameCursor++;
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   909
        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
   910
        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
   911
        final int SELECT_ALT   = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   912
        final int CALL_TARGET  = nameCursor++;
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   913
        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
   914
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   915
        MethodType lambdaType = basicType.invokerType();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   916
        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
   917
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   918
        BoundMethodHandle.SpeciesData data =
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   919
                (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   920
                                     : BoundMethodHandle.speciesData_LLL();
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   921
        names[THIS_MH] = names[THIS_MH].withConstraint(data);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   922
        names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   923
        names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   924
        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
   925
        if (GET_COUNTERS != -1) {
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   926
            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
   927
        }
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   928
        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
   929
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   930
        // call test
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   931
        MethodType testType = basicType.changeReturnType(boolean.class).basicType();
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   932
        invokeArgs[0] = names[GET_TEST];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   933
        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
   934
29019
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   935
        // profile branch
a859b806c8bd 8063137: Never-taken branches should be pruned when GWT LambdaForms are shared
vlivanov
parents: 27755
diff changeset
   936
        if (PROFILE != -1) {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   937
            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
   938
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   939
        // call selectAlternative
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
   940
        names[SELECT_ALT] = new Name(getConstantHandle(MH_selectAlternative), names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   941
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
   942
        // call target or fallback
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   943
        invokeArgs[0] = names[SELECT_ALT];
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   944
        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
   945
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
   946
        lform = new LambdaForm(lambdaType.parameterCount(), names, /*forceInline=*/true, Kind.GUARD);
26480
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   947
92d431f1ec8d 8056926: Improve caching of GuardWithTest combinator
vlivanov
parents: 26479
diff changeset
   948
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   949
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   950
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   951
    /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
   952
     * The LambdaForm shape for catchException combinator is the following:
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   953
     * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   954
     *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   955
     *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   956
     *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   957
     *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   958
     *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   959
     *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   960
     *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   961
     *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   962
     *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   963
     * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   964
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   965
     * 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
   966
     * 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
   967
     * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   968
     *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   969
     * 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
   970
     * among catchException combinators with the same basic type.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   971
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   972
    private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   973
        MethodType lambdaType = basicType.invokerType();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   974
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   975
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   976
        if (lform != null) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   977
            return lform;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   978
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   979
        final int THIS_MH      = 0;  // the BMH_LLLLL
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   980
        final int ARG_BASE     = 1;  // start of incoming arguments
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   981
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   982
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   983
        int nameCursor = ARG_LIMIT;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   984
        final int GET_TARGET       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   985
        final int GET_CLASS        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   986
        final int GET_CATCHER      = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   987
        final int GET_COLLECT_ARGS = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   988
        final int GET_UNBOX_RESULT = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   989
        final int BOXED_ARGS       = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   990
        final int TRY_CATCH        = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   991
        final int UNBOX_RESULT     = nameCursor++;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   992
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   993
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   994
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   995
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
26479
a60a19ec5ce5 8057657: Annotate LambdaForm parameters with types
vlivanov
parents: 26476
diff changeset
   996
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   997
        names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   998
        names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
   999
        names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1000
        names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1001
        names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1002
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1003
        // FIXME: rework argument boxing/result unboxing logic for LF interpretation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1004
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1005
        // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1006
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1007
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1008
        Object[] args = new Object[invokeBasic.type().parameterCount()];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1009
        args[0] = names[GET_COLLECT_ARGS];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1010
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1011
        names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1012
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1013
        // 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
  1014
        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
  1015
        names[TRY_CATCH] = new Name(getFunction(NF_guardWithCatch), gwcArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1016
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1017
        // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1018
        MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1019
        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
  1020
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1021
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  1022
        lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.GUARD_WITH_CATCH);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1023
24696
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 24572
diff changeset
  1024
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1025
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1026
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1027
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1028
    MethodHandle makeGuardWithCatch(MethodHandle target,
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1029
                                    Class<? extends Throwable> exType,
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1030
                                    MethodHandle catcher) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1031
        MethodType type = target.type();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1032
        LambdaForm form = makeGuardWithCatchForm(type.basicType());
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1033
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24696
diff changeset
  1034
        // Prepare auxiliary method handles used during LambdaForm interpretation.
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1035
        // Box arguments and wrap them into Object[]: ValueConversions.array().
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1036
        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
  1037
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1038
        MethodHandle unboxResult = unboxResultHandle(type.returnType());
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1039
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1040
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1041
        BoundMethodHandle mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1042
        try {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1043
            mh = (BoundMethodHandle) data.constructor().invokeBasic(type, form, (Object) target, (Object) exType,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1044
                    (Object) catcher, (Object) collectArgs, (Object) unboxResult);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1045
        } catch (Throwable ex) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1046
            throw uncaughtException(ex);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1047
        }
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1048
        assert(mh.type() == type);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1049
        return mh;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1050
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1051
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
     * Intrinsified during LambdaForm compilation
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1054
     * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1055
     */
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1056
    @LambdaForm.Hidden
23059
4aca6147df88 8036732: Fix raw warning in java.lang.invoke.MethodHandleImpl.java
darcy
parents: 23039
diff changeset
  1057
    static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1058
                                 Object... av) throws Throwable {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1059
        // 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
  1060
        try {
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1061
            return target.asFixedArity().invokeWithArguments(av);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1062
        } catch (Throwable t) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1063
            if (!exType.isInstance(t)) throw t;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1064
            return catcher.asFixedArity().invokeWithArguments(prepend(av, t));
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1065
        }
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
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1068
    /** Prepend elements to an array. */
23338
0a84d339822a 8036117: MethodHandles.catchException doesn't handle VarargsCollector right (8034120 failed)
vlivanov
parents: 23059
diff changeset
  1069
    @LambdaForm.Hidden
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1070
    private static Object[] prepend(Object[] array, Object... elems) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1071
        int nArray = array.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1072
        int nElems = elems.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1073
        Object[] newArray = new Object[nArray + nElems];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1074
        System.arraycopy(elems, 0, newArray, 0, nElems);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1075
        System.arraycopy(array, 0, newArray, nElems, nArray);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 21649
diff changeset
  1076
        return newArray;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1077
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1078
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1079
    static
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
  1080
    MethodHandle throwException(MethodType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1081
        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
  1082
        int arity = type.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1083
        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
  1084
            MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
  1085
            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
  1086
            return mh;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1087
        }
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1088
        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
  1089
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1090
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
  1091
    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
  1092
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1093
    static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1094
    static MethodHandle fakeMethodHandleInvoke(MemberName method) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1095
        int idx;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1096
        assert(method.isMethodHandleInvoke());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1097
        switch (method.getName()) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1098
        case "invoke":       idx = 0; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1099
        case "invokeExact":  idx = 1; break;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1100
        default:             throw new InternalError(method.getName());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1101
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1102
        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
  1103
        if (mh != null)  return mh;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1104
        MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1105
                                                MethodHandle.class, Object[].class);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1106
        mh = throwException(type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1107
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1108
        if (!method.getInvocationType().equals(mh.type()))
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1109
            throw new InternalError(method.toString());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1110
        mh = mh.withInternalMemberName(method, false);
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 37340
diff changeset
  1111
        mh = mh.withVarargs(true);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1112
        assert(method.isVarargs());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1113
        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
  1114
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1115
    }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1116
    static MethodHandle fakeVarHandleInvoke(MemberName method) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1117
        // TODO caching, is it necessary?
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1118
        MethodType type = MethodType.methodType(method.getReturnType(), UnsupportedOperationException.class,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1119
                                                VarHandle.class, Object[].class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1120
        MethodHandle mh = throwException(type);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1121
        mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke VarHandle"));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1122
        if (!method.getInvocationType().equals(mh.type()))
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1123
            throw new InternalError(method.toString());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1124
        mh = mh.withInternalMemberName(method, false);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1125
        mh = mh.asVarargsCollector(Object[].class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1126
        assert(method.isVarargs());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1127
        return mh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36120
diff changeset
  1128
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13420
diff changeset
  1129
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1130
    /**
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1131
     * Create an alias for the method handle which, when called,
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1132
     * appears to be called from the same class loader and protection domain
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1133
     * as hostClass.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1134
     * This is an expensive no-op unless the method which is called
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1135
     * is sensitive to its caller.  A small number of system methods
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1136
     * are in this category, including Class.forName and Method.invoke.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1137
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1138
    static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1139
    MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1140
        return BindCaller.bindCaller(mh, hostClass);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1141
    }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1142
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1143
    // Put the whole mess into its own nested class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1144
    // 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
  1145
    private static class BindCaller {
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1146
        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
  1147
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1148
        static
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1149
        MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
42333
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1150
            // Code in the the boot layer should now be careful while creating method handles or
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1151
            // 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
  1152
            // 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
  1153
            // from the boot layer to untrusted code.
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1154
            if (hostClass == null
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 14852
diff changeset
  1155
                ||    (hostClass.isArray() ||
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1156
                       hostClass.isPrimitive() ||
42333
83f37c05391b 8133719: java.lang.InternalError in java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller
srastogi
parents: 41207
diff changeset
  1157
                       hostClass.getName().startsWith("java.lang.invoke."))) {
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1158
                throw new InternalError();  // does not happen, and should not anyway
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1159
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1160
            // For simplicity, convert mh to a varargs-like method.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1161
            MethodHandle vamh = prepareForInvoker(mh);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1162
            // Cache the result of makeInjectedInvoker once per argument class.
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1163
            MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1164
            return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1165
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1166
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1167
        private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1168
            try {
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1169
                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
  1170
                assert checkInjectedInvoker(hostClass, invokerClass);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1171
                return IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1172
            } catch (ReflectiveOperationException ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1173
                throw uncaughtException(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1174
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1175
        }
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1176
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1177
        private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1178
            @Override protected MethodHandle computeValue(Class<?> hostClass) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1179
                return makeInjectedInvoker(hostClass);
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
        };
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1182
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1183
        // 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
  1184
        private static MethodHandle prepareForInvoker(MethodHandle mh) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1185
            mh = mh.asFixedArity();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1186
            MethodType mt = mh.type();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1187
            int arity = mt.parameterCount();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1188
            MethodHandle vamh = mh.asType(mt.generic());
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1189
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1190
            vamh = vamh.asSpreader(Object[].class, arity);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1191
            vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1192
            return vamh;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1193
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1194
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1195
        // Undo the adapter effect of prepareForInvoker:
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1196
        private static MethodHandle restoreToType(MethodHandle vamh,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1197
                                                  MethodHandle original,
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1198
                                                  Class<?> hostClass) {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1199
            MethodType type = original.type();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1200
            MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1201
            MemberName member = original.internalMemberName();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1202
            mh = mh.asType(type);
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1203
            mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1204
            return mh;
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1205
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1206
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1207
        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
  1208
            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
  1209
            try {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1210
                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
  1211
            } catch (SecurityException ex) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1212
                // 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
  1213
            }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1214
            try {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1215
                // 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
  1216
                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
  1217
                MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1218
                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
  1219
            } catch (Throwable ex) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1220
                throw new InternalError(ex);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1221
            }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1222
        }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1223
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1224
        private static final MethodHandle MH_checkCallerClass;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1225
        static {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1226
            final Class<?> THIS_CLASS = BindCaller.class;
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1227
            assert(checkCallerClass(THIS_CLASS));
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1228
            try {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1229
                MH_checkCallerClass = IMPL_LOOKUP
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1230
                    .findStatic(THIS_CLASS, "checkCallerClass",
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1231
                                MethodType.methodType(boolean.class, Class.class));
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1232
                assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS));
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1233
            } catch (Throwable ex) {
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1234
                throw new InternalError(ex);
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1235
            }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1236
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1237
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16123
diff changeset
  1238
        @CallerSensitive
40175
8df87018d96a 8161379: Force inline methods calling Reflection.getCallerClass
redestad
parents: 39342
diff changeset
  1239
        @ForceInline // to ensure Reflection.getCallerClass optimization
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1240
        private static boolean checkCallerClass(Class<?> expected) {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1241
            // 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
  1242
            Class<?> actual = Reflection.getCallerClass();
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1243
            if (actual != expected)
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1244
                throw new InternalError("found " + actual.getName() + ", expected " + expected.getName());
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1245
            return true;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1246
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1247
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1248
        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
  1249
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1250
        /** 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
  1251
        private static byte[] generateInvokerTemplate() {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1252
            ClassWriter cw = new ClassWriter(0);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1253
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1254
            // private static class InjectedInvoker {
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1255
            //     @Hidden
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1256
            //     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
  1257
            //        return vamh.invokeExact(args);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1258
            //     }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1259
            // }
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1260
            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
  1261
38422
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1262
            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
  1263
                          "(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
  1264
                          null, null);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1265
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1266
            // Suppress invoker method in stack traces.
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1267
            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
  1268
            av0.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1269
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1270
            mv.visitCode();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1271
            mv.visitVarInsn(ALOAD, 0);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1272
            mv.visitVarInsn(ALOAD, 1);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1273
            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
  1274
                               "([Ljava/lang/Object;)Ljava/lang/Object;", false);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1275
            mv.visitInsn(ARETURN);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1276
            mv.visitMaxs(2, 2);
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1277
            mv.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1278
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1279
            cw.visitEnd();
638589997770 8149574: Fix java/lang/invoke/MethodHandleImpl's use of Unsafe.defineAnonymousClass()
srastogi
parents: 38376
diff changeset
  1280
            return cw.toByteArray();
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1281
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
  1282
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1283
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1284
    /** 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
  1285
    private static final class WrappedMember extends DelegatingMethodHandle {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1286
        private final MethodHandle target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1287
        private final MemberName member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1288
        private final Class<?> callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1289
        private final boolean isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1290
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1291
        private WrappedMember(MethodHandle target, MethodType type,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1292
                              MemberName member, boolean isInvokeSpecial,
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1293
                              Class<?> callerClass) {
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1294
            super(type, target);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1295
            this.target = target;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1296
            this.member = member;
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1297
            this.callerClass = callerClass;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1298
            this.isInvokeSpecial = isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1299
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1300
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1301
        @Override
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1302
        MemberName internalMemberName() {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1303
            return member;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1304
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1305
        @Override
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1306
        Class<?> internalCallerClass() {
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1307
            return callerClass;
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1308
        }
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 20530
diff changeset
  1309
        @Override
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1310
        boolean isInvokeSpecial() {
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1311
            return isInvokeSpecial;
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1312
        }
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1313
        @Override
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1314
        protected MethodHandle getTarget() {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1315
            return target;
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1316
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1317
        @Override
26472
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1318
        public MethodHandle asTypeUncached(MethodType newType) {
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1319
            // This MH is an alias for target, except for the MemberName
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1320
            // Drop the MemberName if there is any conversion.
71b6a6f208c0 8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents: 26471
diff changeset
  1321
            return asTypeCache = target.asType(newType);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1322
        }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1323
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1324
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1325
    static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1326
        if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1327
            return target;
26471
1de6be0c6945 8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents: 26470
diff changeset
  1328
        return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1329
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18769
diff changeset
  1330
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1331
    /** Intrinsic IDs */
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1332
    /*non-public*/
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1333
    enum Intrinsic {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1334
        SELECT_ALTERNATIVE,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1335
        GUARD_WITH_CATCH,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1336
        TRY_FINALLY,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1337
        LOOP,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1338
        NEW_ARRAY,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1339
        ARRAY_LOAD,
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1340
        ARRAY_STORE,
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37674
diff changeset
  1341
        ARRAY_LENGTH,
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1342
        IDENTITY,
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
  1343
        ZERO,
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1344
        NONE // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1345
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1346
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1347
    /** Mark arbitrary method handle as intrinsic.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1348
     * 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
  1349
    static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1350
        private final MethodHandle target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1351
        private final Intrinsic intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1352
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1353
        IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1354
            super(target.type(), target);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1355
            this.target = target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1356
            this.intrinsicName = intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1357
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1358
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1359
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1360
        protected MethodHandle getTarget() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1361
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1362
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1363
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1364
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1365
        Intrinsic intrinsicName() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1366
            return intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1367
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1368
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1369
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1370
        public MethodHandle asTypeUncached(MethodType newType) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1371
            // 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
  1372
            // Drop the name if there is any conversion.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1373
            return asTypeCache = target.asType(newType);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1374
        }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1375
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1376
        @Override
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1377
        String internalProperties() {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1378
            return super.internalProperties() +
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1379
                    "\n& Intrinsic="+intrinsicName;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1380
        }
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1381
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1382
        @Override
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1383
        public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1384
            if (intrinsicName == Intrinsic.IDENTITY) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1385
                MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength);
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1386
                MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1387
                return newArray.asType(resultType);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1388
            }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1389
            return super.asCollector(arrayType, arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
  1390
        }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1391
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1392
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1393
    static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1394
        if (intrinsicName == target.intrinsicName())
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1395
            return target;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1396
        return new IntrinsicMethodHandle(target, intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1397
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1398
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1399
    static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1400
        return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1401
    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1402
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1403
    /// Collection of multiple arguments.
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1404
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1405
    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
  1406
        MethodType type = MethodType.genericMethodType(nargs)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1407
                .changeReturnType(rtype)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1408
                .insertParameterTypes(0, ptypes);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1409
        try {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1410
            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
  1411
        } catch (ReflectiveOperationException ex) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1412
            return null;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1413
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1414
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1415
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1416
    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
  1417
    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
  1418
    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
  1419
    private static Object[] array(Object a0)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1420
                { return makeArray(a0); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1421
    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
  1422
                { return makeArray(a0, a1); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1423
    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
  1424
                { return makeArray(a0, a1, a2); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1425
    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
  1426
                { return makeArray(a0, a1, a2, a3); }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1427
    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
  1428
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1429
                { 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
  1430
    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
  1431
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1432
                { 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
  1433
    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
  1434
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1435
                { 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
  1436
    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
  1437
                                  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
  1438
                { 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
  1439
    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
  1440
                                  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
  1441
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1442
                { 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
  1443
    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
  1444
                                  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
  1445
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1446
                { 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
  1447
94a990d3754a 8062180: MethodHandleImpl.makeArrays throws and swallows java.lang.NoSuchFieldError in normal flow
redestad
parents: 27295
diff changeset
  1448
    private static final int ARRAYS_COUNT = 11;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1449
    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
  1450
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1451
    // filling versions of the above:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1452
    // 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
  1453
    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
  1454
        Object[] a = new Object[len];
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1455
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1456
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1457
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1458
    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
  1459
        Object[] a = Arrays.copyOf(example, len);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1460
        assert(a.getClass() != Object[].class);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1461
        fillWithArguments(a, 0, args);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1462
        return a;
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
    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
  1465
        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
  1466
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1467
    // 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
  1468
    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
  1469
                { fillWithArguments(a, pos, a0); return a; }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1470
    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
  1471
                { 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
  1472
    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
  1473
                { 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
  1474
    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
  1475
                { 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
  1476
    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
  1477
                                  Object a4)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1478
                { 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
  1479
    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
  1480
                                  Object a4, Object a5)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1481
                { 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
  1482
    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
  1483
                                  Object a4, Object a5, Object a6)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1484
                { 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
  1485
    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
  1486
                                  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
  1487
                { 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
  1488
    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
  1489
                                  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
  1490
                                  Object a8)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1491
                { 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
  1492
    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
  1493
                                  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
  1494
                                  Object a8, Object a9)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1495
                { 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
  1496
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1497
    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
  1498
    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
  1499
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1500
    private static MethodHandle getFillArray(int count) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1501
        assert (count > 0 && count < FILL_ARRAYS_COUNT);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1502
        MethodHandle mh = FILL_ARRAYS[count];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1503
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1504
            return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1505
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1506
        mh = findCollector("fillArray", count, Object[].class, Integer.class, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1507
        FILL_ARRAYS[count] = mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1508
        return mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1509
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1510
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1511
    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
  1512
        Object a = w.makeArray(boxes.length);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1513
        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
  1514
        return a;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1515
    }
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
    /** 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
  1518
     *  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
  1519
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1520
    static MethodHandle varargsArray(int nargs) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1521
        MethodHandle mh = ARRAYS[nargs];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1522
        if (mh != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1523
            return mh;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1524
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1525
        if (nargs < ARRAYS_COUNT) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1526
            mh = findCollector("array", nargs, Object[].class);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1527
        } else {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1528
            mh = buildVarargsArray(getConstantHandle(MH_fillNewArray),
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1529
                    getConstantHandle(MH_arrayIdentity), nargs);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1530
        }
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1531
        assert(assertCorrectArity(mh, nargs));
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26472
diff changeset
  1532
        mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1533
        return ARRAYS[nargs] = mh;
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1534
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1535
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1536
    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
  1537
        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
  1538
        return true;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1539
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1540
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1541
    // 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
  1542
    static <T> T[] identity(T[] x) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1543
        return x;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1544
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1545
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1546
    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
  1547
        // 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
  1548
        //   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
  1549
        // 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
  1550
        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
  1551
        int rightLen = nargs - leftLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1552
        MethodHandle leftCollector = newArray.bindTo(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1553
        leftCollector = leftCollector.asCollector(Object[].class, leftLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1554
        MethodHandle mh = finisher;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1555
        if (rightLen > 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1556
            MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1557
            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
  1558
                mh = rightFiller;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1559
            else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1560
                mh = MethodHandles.collectArguments(mh, 0, rightFiller);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1561
        }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1562
        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
  1563
            mh = leftCollector;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1564
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1565
            mh = MethodHandles.collectArguments(mh, 0, leftCollector);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1566
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1567
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1568
27043
02d63b3eb16e 8058892: FILL_ARRAYS and ARRAYS are eagely initialized in MethodHandleImpl
vlivanov
parents: 26625
diff changeset
  1569
    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
  1570
    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
  1571
    /** 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
  1572
     *  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
  1573
     *  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
  1574
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1575
    private static MethodHandle fillToRight(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1576
        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
  1577
        if (filler != null)  return filler;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1578
        filler = buildFiller(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1579
        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
  1580
        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
  1581
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1582
    private static MethodHandle buildFiller(int nargs) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1583
        if (nargs <= LEFT_ARGS)
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1584
            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
  1585
        // 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
  1586
        final int CHUNK = LEFT_ARGS;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1587
        int rightLen = nargs % CHUNK;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1588
        int midLen = nargs - rightLen;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1589
        if (rightLen == 0) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1590
            midLen = nargs - (rightLen = CHUNK);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1591
            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
  1592
                // 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
  1593
                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
  1594
                    if (j > LEFT_ARGS)  fillToRight(j);
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
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1597
        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
  1598
        assert(rightLen > 0);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1599
        MethodHandle midFill = fillToRight(midLen);  // recursive fill
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1600
        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
  1601
        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
  1602
        assert(rightFill.type().parameterCount() == 1 + rightLen);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1603
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1604
        // Combine the two fills:
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1605
        //   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
  1606
        // 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
  1607
        //   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
  1608
        if (midLen == LEFT_ARGS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1609
            return rightFill;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1610
        else
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1611
            return MethodHandles.collectArguments(rightFill, 0, midFill);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1612
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1613
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1614
    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
  1615
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1616
    /** 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
  1617
     *  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
  1618
     *  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
  1619
     */
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1620
    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
  1621
        Class<?> elemType = arrayType.getComponentType();
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1622
        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
  1623
        // 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
  1624
        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
  1625
            int slots = nargs;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1626
            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
  1627
            if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1628
                slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1629
            if (slots > MAX_ARRAY_SLOTS)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1630
                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
  1631
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1632
        if (elemType == Object.class)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1633
            return varargsArray(nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1634
        // other cases:  primitive arrays, subtypes of Object[]
34720
c2192aa0ab88 8144723: MethodHandleImpl.initStatics is no longer needed
redestad
parents: 33874
diff changeset
  1635
        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
  1636
        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
  1637
        if (mh != null)  return mh;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1638
        if (nargs == 0) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1639
            Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1640
            mh = MethodHandles.constant(arrayType, example);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1641
        } else if (elemType.isPrimitive()) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1642
            MethodHandle builder = getConstantHandle(MH_fillNewArray);
26466
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1643
            MethodHandle producer = buildArrayProducer(arrayType);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1644
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1645
        } else {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1646
            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
  1647
            Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1648
            MethodHandle builder = getConstantHandle(MH_fillNewTypedArray).bindTo(example);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1649
            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
  1650
            mh = buildVarargsArray(builder, producer, nargs);
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1651
        }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1652
        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
  1653
        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
  1654
        assert(assertCorrectArity(mh, nargs));
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1655
        if (nargs < cache.length)
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1656
            cache[nargs] = mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1657
        return mh;
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1658
    }
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1659
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1660
    private static MethodHandle buildArrayProducer(Class<?> arrayType) {
3bbb6a284bd4 8049555: Move varargsArray from sun.invoke.util package to java.lang.invoke
vlivanov
parents: 26465
diff changeset
  1661
        Class<?> elemType = arrayType.getComponentType();
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26466
diff changeset
  1662
        assert(elemType.isPrimitive());
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1663
        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
  1664
    }
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1665
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1666
    /*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
  1667
        if (mh1 != mh2) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1668
            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
  1669
                    mh1, ((MethodHandle)mh1).form,
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1670
                    mh2, ((MethodHandle)mh2).form);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1671
            throw newInternalError(msg);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1672
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
  1673
    }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1674
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1675
    // Local constant functions:
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1676
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
  1677
    /* non-public */
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1678
    static final byte NF_checkSpreadArgument = 0,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1679
            NF_guardWithCatch = 1,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1680
            NF_throwException = 2,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1681
            NF_tryFinally = 3,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1682
            NF_loop = 4,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1683
            NF_profileBoolean = 5,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1684
            NF_LIMIT = 6;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1685
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1686
    private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1687
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1688
    static NamedFunction getFunction(byte func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1689
        NamedFunction nf = NFS[func];
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1690
        if (nf != null) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1691
            return nf;
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1692
        }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1693
        return NFS[func] = createFunction(func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1694
    }
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1695
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1696
    private static NamedFunction createFunction(byte func) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1697
        try {
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1698
            switch (func) {
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1699
                case NF_checkSpreadArgument:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1700
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1701
                            .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1702
                case NF_guardWithCatch:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1703
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1704
                            .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1705
                                    MethodHandle.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1706
                case NF_tryFinally:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1707
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1708
                            .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1709
                case NF_loop:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1710
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1711
                            .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1712
                case NF_throwException:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1713
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1714
                            .getDeclaredMethod("throwException", Throwable.class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1715
                case NF_profileBoolean:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1716
                    return new NamedFunction(MethodHandleImpl.class
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1717
                            .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1718
                default:
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1719
                    throw new InternalError("Undefined function: " + func);
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1720
            }
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1721
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1722
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1723
        }
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1724
    }
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1725
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1726
    static {
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1727
        SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1728
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1729
            public Object newMemberName() {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1730
                return new MemberName();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1731
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1732
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1733
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1734
            public String getName(Object mname) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1735
                MemberName memberName = (MemberName)mname;
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1736
                return memberName.getName();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1737
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1738
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1739
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1740
            public boolean isNative(Object mname) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1741
                MemberName memberName = (MemberName)mname;
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1742
                return memberName.isNative();
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1743
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1744
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1745
            @Override
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1746
            public byte[] generateDirectMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1747
                    String className, MethodType[] methodTypes, int[] types) {
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1748
                return GenerateJLIClassesHelper
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1749
                        .generateDirectMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1750
                                className, methodTypes, types);
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1751
            }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1752
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1753
            @Override
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1754
            public byte[] generateDelegatingMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1755
                    String className, MethodType[] methodTypes) {
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1756
                return GenerateJLIClassesHelper
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1757
                        .generateDelegatingMethodHandleHolderClassBytes(
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40273
diff changeset
  1758
                                className, methodTypes);
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1759
            }
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1760
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1761
            @Override
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1762
            public Map.Entry<String, byte[]> generateConcreteBMHClassBytes(
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1763
                    final String types) {
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1764
                return GenerateJLIClassesHelper
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1765
                        .generateConcreteBMHClassBytes(types);
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1766
            }
40419
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1767
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1768
            @Override
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1769
            public byte[] generateBasicFormsClassBytes(final String className) {
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1770
                return GenerateJLIClassesHelper
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1771
                        .generateBasicFormsClassBytes(className);
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40418
diff changeset
  1772
            }
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1773
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1774
            @Override
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1775
            public byte[] generateInvokersHolderClassBytes(final String className,
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1776
                    MethodType[] methodTypes) {
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1777
                return GenerateJLIClassesHelper
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1778
                        .generateInvokersHolderClassBytes(className, methodTypes);
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40419
diff changeset
  1779
            }
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40175
diff changeset
  1780
        });
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1781
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  1782
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1783
    /** 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
  1784
    private static MethodHandle unboxResultHandle(Class<?> returnType) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1785
        if (returnType.isPrimitive()) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1786
            if (returnType == void.class) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1787
                return ValueConversions.ignore();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1788
            } else {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1789
                Wrapper w = Wrapper.forPrimitiveType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1790
                return ValueConversions.unboxExact(w);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1791
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1792
        } else {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1793
            return MethodHandles.identity(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1794
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1795
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1796
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1797
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1798
     * 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
  1799
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1800
     * @param tloop the return type of the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1801
     * @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
  1802
     * @param init sanitized array of initializers for loop-local variables.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1803
     * @param step sanitited array of loop bodies.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1804
     * @param pred sanitized array of predicates.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1805
     * @param fini sanitized array of loop finalizers.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1806
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1807
     * @return a handle that, when invoked, will execute the loop.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1808
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1809
    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
  1810
                                 List<MethodHandle> pred, List<MethodHandle> fini) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1811
        MethodType type = MethodType.methodType(tloop, targs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1812
        BasicType[] initClauseTypes =
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1813
                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
  1814
        LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1815
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1816
        // Prepare auxiliary method handles used during LambdaForm interpretation.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1817
        // 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
  1818
        MethodType varargsType = type.changeReturnType(Object[].class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1819
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1820
        MethodHandle unboxResult = unboxResultHandle(tloop);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1821
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1822
        LoopClauses clauseData =
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1823
                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
  1824
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1825
        BoundMethodHandle mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1826
        try {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1827
            mh = (BoundMethodHandle) data.constructor().invokeBasic(type, form, (Object) clauseData,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1828
                    (Object) collectArgs, (Object) unboxResult);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1829
        } catch (Throwable ex) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1830
            throw uncaughtException(ex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1831
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1832
        assert(mh.type() == type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1833
        return mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1834
    }
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1835
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1836
    private static MethodHandle[] toArray(List<MethodHandle> l) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1837
        return l.toArray(new MethodHandle[0]);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1838
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1839
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1840
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1841
     * 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
  1842
     * 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
  1843
     * 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
  1844
     * {@code t12}):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1845
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1846
     *  loop=Lambda(a0:L,a1:L)=>{
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1847
     *    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
  1848
     *    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
  1849
     *    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
  1850
     *    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
  1851
     *    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
  1852
     *    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
  1853
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1854
     * <p>
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1855
     * {@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
  1856
     * 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
  1857
     * 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
  1858
     * ({@code ValueConversions.unbox()}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1859
     * <p>
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1860
     * 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
  1861
     * 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
  1862
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1863
     * 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
  1864
     * 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
  1865
     * 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
  1866
     * bytecode generation}.
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
    private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1869
        MethodType lambdaType = basicType.invokerType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1870
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1871
        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
  1872
        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
  1873
        final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1874
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1875
        int nameCursor = ARG_LIMIT;
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1876
        final int GET_CLAUSE_DATA = nameCursor++;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1877
        final int GET_COLLECT_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1878
        final int GET_UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1879
        final int BOXED_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1880
        final int LOOP = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1881
        final int UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1882
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1883
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1884
        if (lform == null) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1885
            Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1886
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1887
            BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1888
            names[THIS_MH] = names[THIS_MH].withConstraint(data);
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1889
            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
  1890
            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
  1891
            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
  1892
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1893
            // 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
  1894
            MethodType collectArgsType = basicType.changeReturnType(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1895
            MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1896
            Object[] args = new Object[invokeBasic.type().parameterCount()];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1897
            args[0] = names[GET_COLLECT_ARGS];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1898
            System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1899
            names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.LOOP), args);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1900
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1901
            // 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
  1902
            Object[] lArgs =
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1903
                    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
  1904
                            names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
  1905
            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
  1906
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1907
            // 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
  1908
            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
  1909
            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
  1910
            names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1911
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1912
            lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  1913
                    new LambdaForm(lambdaType.parameterCount(), names, Kind.LOOP));
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1914
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1915
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1916
        // 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
  1917
        return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1918
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1919
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1920
    static class LoopClauses {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1921
        @Stable final MethodHandle[][] clauses;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1922
        LoopClauses(MethodHandle[][] clauses) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1923
            assert clauses.length == 4;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1924
            this.clauses = clauses;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1925
        }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1926
        @Override
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1927
        public String toString() {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1928
            StringBuffer sb = new StringBuffer("LoopClauses -- ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1929
            for (int i = 0; i < 4; ++i) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1930
                if (i > 0) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1931
                    sb.append("       ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1932
                }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1933
                sb.append('<').append(i).append(">: ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1934
                MethodHandle[] hs = clauses[i];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1935
                for (int j = 0; j < hs.length; ++j) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1936
                    if (j > 0) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1937
                        sb.append("          ");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1938
                    }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1939
                    sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1940
                }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1941
            }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1942
            sb.append(" --\n");
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1943
            return sb.toString();
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1944
        }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1945
    }
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1946
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1947
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1948
     * Intrinsified during LambdaForm compilation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1949
     * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1950
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1951
    @LambdaForm.Hidden
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1952
    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
  1953
        final MethodHandle[] init = clauseData.clauses[0];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1954
        final MethodHandle[] step = clauseData.clauses[1];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1955
        final MethodHandle[] pred = clauseData.clauses[2];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1956
        final MethodHandle[] fini = clauseData.clauses[3];
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1957
        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
  1958
        int nArgs = init[0].type().parameterCount();
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1959
        Object[] varsAndArgs = new Object[varSize + nArgs];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1960
        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
  1961
            MethodHandle ih = init[i];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1962
            if (ih.type().returnType() == void.class) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1963
                ih.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1964
            } else {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1965
                varsAndArgs[v++] = ih.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1966
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1967
        }
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  1968
        System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1969
        final int nSteps = step.length;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1970
        for (; ; ) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1971
            for (int i = 0, v = 0; i < nSteps; ++i) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1972
                MethodHandle p = pred[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1973
                MethodHandle s = step[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1974
                MethodHandle f = fini[i];
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1975
                if (s.type().returnType() == void.class) {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1976
                    s.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1977
                } else {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1978
                    varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1979
                }
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1980
                if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  1981
                    return f.invokeWithArguments(varsAndArgs);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1982
                }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1983
            }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1984
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1985
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1986
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1987
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1988
     * 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
  1989
     * MethodHandle) counting loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1990
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  1991
     * @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
  1992
     * @param counter the counter parameter, passed in during loop execution.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1993
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1994
     * @return whether the counter has reached the limit.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1995
     */
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  1996
    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
  1997
        return counter < limit;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1998
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  1999
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2000
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2001
     * 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
  2002
     * MethodHandle) counting loops} to increment the counter.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2003
     *
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2004
     * @param limit the upper bound of the loop counter (ignored).
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2005
     * @param counter the loop counter.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2006
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2007
     * @return the loop counter incremented by 1.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2008
     */
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2009
    static int countedLoopStep(int limit, int counter) {
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2010
        return counter + 1;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2011
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2012
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2013
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2014
     * 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
  2015
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2016
     * @param it the {@link Iterable} over which the loop iterates.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2017
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2018
     * @return an {@link Iterator} over the argument's elements.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2019
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2020
    static Iterator<?> initIterator(Iterable<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2021
        return it.iterator();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2022
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2023
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2024
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2025
     * 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
  2026
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2027
     * @param it the iterator to be checked.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2028
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2029
     * @return {@code true} iff there are more elements to iterate over.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2030
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2031
    static boolean iteratePredicate(Iterator<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2032
        return it.hasNext();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2033
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2034
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2035
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2036
     * 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
  2037
     * MethodHandles#iteratedLoop iterating loops}.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2038
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2039
     * @param it the iterator.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2040
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2041
     * @return the next element from the iterator.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2042
     */
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2043
    static Object iterateNext(Iterator<?> it) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2044
        return it.next();
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2045
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2046
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2047
    /**
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2048
     * 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
  2049
     *
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2050
     * @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
  2051
     * @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
  2052
     * @param rtype the result type of the entire construct.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2053
     * @param argTypes the types of the arguments.
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
     * @return a handle on the constructed {@code try-finally} block.
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2056
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2057
    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
  2058
        MethodType type = MethodType.methodType(rtype, argTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2059
        LambdaForm form = makeTryFinallyForm(type.basicType());
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2060
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2061
        // Prepare auxiliary method handles used during LambdaForm interpretation.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2062
        // 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
  2063
        MethodType varargsType = type.changeReturnType(Object[].class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2064
        MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2065
        MethodHandle unboxResult = unboxResultHandle(rtype);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2066
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2067
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2068
        BoundMethodHandle mh;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2069
        try {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2070
            mh = (BoundMethodHandle) data.constructor().invokeBasic(type, form, (Object) target, (Object) cleanup,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2071
                    (Object) collectArgs, (Object) unboxResult);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2072
        } catch (Throwable ex) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2073
            throw uncaughtException(ex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2074
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2075
        assert(mh.type() == type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2076
        return mh;
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2077
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2078
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2079
    /**
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2080
     * 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
  2081
     * {@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
  2082
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2083
     *  tryFinally=Lambda(a0:L,a1:L)=>{
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2084
     *    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
  2085
     *    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
  2086
     *    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
  2087
     *    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
  2088
     *    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
  2089
     *    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
  2090
     *    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
  2091
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2092
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2093
     * {@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
  2094
     * {@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
  2095
     * {@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
  2096
     * ({@code ValueConversions.unbox()}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2097
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2098
     * 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
  2099
     * forms among tryFinally combinators with the same basic type.
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2100
     */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2101
    private static LambdaForm makeTryFinallyForm(MethodType basicType) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2102
        MethodType lambdaType = basicType.invokerType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2103
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2104
        LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2105
        if (lform != null) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2106
            return lform;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2107
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2108
        final int THIS_MH      = 0;  // the BMH_LLLL
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2109
        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
  2110
        final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2111
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2112
        int nameCursor = ARG_LIMIT;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2113
        final int GET_TARGET       = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2114
        final int GET_CLEANUP      = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2115
        final int GET_COLLECT_ARGS = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2116
        final int GET_UNBOX_RESULT = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2117
        final int BOXED_ARGS       = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2118
        final int TRY_FINALLY      = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2119
        final int UNBOX_RESULT     = nameCursor++;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2120
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2121
        Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2122
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2123
        BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2124
        names[THIS_MH]          = names[THIS_MH].withConstraint(data);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2125
        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
  2126
        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
  2127
        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
  2128
        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
  2129
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2130
        // 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
  2131
        MethodType collectArgsType = basicType.changeReturnType(Object.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2132
        MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2133
        Object[] args = new Object[invokeBasic.type().parameterCount()];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2134
        args[0] = names[GET_COLLECT_ARGS];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2135
        System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2136
        names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY), args);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2137
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2138
        // 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
  2139
        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
  2140
        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
  2141
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2142
        // 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
  2143
        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
  2144
        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
  2145
        names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2146
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42333
diff changeset
  2147
        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
  2148
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2149
        return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2150
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2151
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2152
    /**
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2153
     * Intrinsified during LambdaForm compilation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2154
     * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2155
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2156
    @LambdaForm.Hidden
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2157
    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
  2158
        Throwable t = null;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2159
        Object r = null;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2160
        try {
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40543
diff changeset
  2161
            r = target.invokeWithArguments(av);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2162
        } catch (Throwable thrown) {
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2163
            t = thrown;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2164
            throw t;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2165
        } finally {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2166
            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
  2167
            r = cleanup.invokeWithArguments(args);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2168
        }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2169
        return r;
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2170
    }
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2171
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2172
    // Indexes into constant method handles:
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2173
    static final int
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2174
            MH_cast                  =  0,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2175
            MH_selectAlternative     =  1,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2176
            MH_copyAsPrimitiveArray  =  2,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2177
            MH_fillNewTypedArray     =  3,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2178
            MH_fillNewArray          =  4,
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2179
            MH_arrayIdentity         =  5,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2180
            MH_countedLoopPred       =  6,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2181
            MH_countedLoopStep       =  7,
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2182
            MH_initIterator          =  8,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2183
            MH_iteratePred           =  9,
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38472
diff changeset
  2184
            MH_iterateNext           = 10,
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2185
            MH_Array_newInstance     = 11,
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2186
            MH_LIMIT                 = 12;
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2187
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2188
    static MethodHandle getConstantHandle(int idx) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2189
        MethodHandle handle = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2190
        if (handle != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2191
            return handle;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2192
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2193
        return setCachedHandle(idx, makeConstantHandle(idx));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2194
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2195
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2196
    private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2197
        // Simulate a CAS, to avoid racy duplication of results.
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2198
        MethodHandle prev = HANDLES[idx];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2199
        if (prev != null) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2200
            return prev;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2201
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2202
        HANDLES[idx] = method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2203
        return method;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2204
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2205
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2206
    // Local constant method handles:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2207
    private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2208
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2209
    private static MethodHandle makeConstantHandle(int idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2210
        try {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2211
            switch (idx) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2212
                case MH_cast:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2213
                    return IMPL_LOOKUP.findVirtual(Class.class, "cast",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2214
                            MethodType.methodType(Object.class, Object.class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2215
                case MH_copyAsPrimitiveArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2216
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "copyAsPrimitiveArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2217
                            MethodType.methodType(Object.class, Wrapper.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2218
                case MH_arrayIdentity:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2219
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "identity",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2220
                            MethodType.methodType(Object[].class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2221
                case MH_fillNewArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2222
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2223
                            MethodType.methodType(Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2224
                case MH_fillNewTypedArray:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2225
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewTypedArray",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2226
                            MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2227
                case MH_selectAlternative:
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2228
                    return makeIntrinsic(IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2229
                            MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)),
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2230
                        Intrinsic.SELECT_ALTERNATIVE);
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2231
                case MH_countedLoopPred:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2232
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2233
                            MethodType.methodType(boolean.class, int.class, int.class));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2234
                case MH_countedLoopStep:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2235
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2236
                            MethodType.methodType(int.class, int.class, int.class));
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2237
                case MH_initIterator:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2238
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2239
                            MethodType.methodType(Iterator.class, Iterable.class));
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2240
                case MH_iteratePred:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2241
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 41125
diff changeset
  2242
                            MethodType.methodType(boolean.class, Iterator.class));
33874
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2243
                case MH_iterateNext:
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2244
                    return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
46651fd30c0b 8139885: implement JEP 274: enhanced method handles
mhaupt
parents: 33846
diff changeset
  2245
                            MethodType.methodType(Object.class, Iterator.class));
37674
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2246
                case MH_Array_newInstance:
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2247
                    return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
763f704393cf 8155106: MHs.Lookup.findConstructor returns handles for array classes
mhaupt
parents: 37363
diff changeset
  2248
                            MethodType.methodType(Object.class, Class.class, int.class));
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2249
            }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2250
        } catch (ReflectiveOperationException ex) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2251
            throw newInternalError(ex);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2252
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2253
        throw newInternalError("Unknown function index: " + idx);
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 31802
diff changeset
  2254
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  2255
}