src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java
author psandoz
Fri, 08 Sep 2017 10:46:46 -0700
changeset 48826 c4d9d1b08e2e
parent 47216 71c04702a3d5
child 48827 8772acd913e5
permissions -rw-r--r--
8186209: Tool support for ConstantDynamic 8186046: Minimal ConstantDynamic support 8190972: Ensure that AOT/Graal filters out class files containing CONSTANT_Dynamic ahead of full AOT support Reviewed-by: acorn, coleenp, kvn Contributed-by: lois.foltan@oracle.com, john.r.rose@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
30342
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
     2
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang.invoke;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
    28
import jdk.internal.ref.CleanerFactory;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
    29
import sun.invoke.util.Wrapper;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
    30
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    31
import java.lang.invoke.MethodHandles.Lookup;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    32
import java.lang.reflect.Field;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
    33
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    34
import static java.lang.invoke.MethodHandleNatives.Constants.*;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
    35
import static java.lang.invoke.MethodHandleStatics.TRACE_METHOD_LINKAGE;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    36
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    37
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    38
/**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    39
 * The JVM interface for the method handles package is all here.
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
    40
 * This is an interface internal and private to an implementation of JSR 292.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    41
 * <em>This class is not part of the JSR 292 standard.</em>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
class MethodHandleNatives {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    45
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    46
    private MethodHandleNatives() { } // static only
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    48
    /// MemberName support
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    49
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    50
    static native void init(MemberName self, Object ref);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    51
    static native void expand(MemberName self);
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37343
diff changeset
    52
    static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError, ClassNotFoundException;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    53
    static native int getMembers(Class<?> defc, String matchName, String matchSig,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    54
            int matchFlags, Class<?> caller, int skip, MemberName[] results);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    55
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32986
diff changeset
    56
    /// Field layout queries parallel to jdk.internal.misc.Unsafe:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    57
    static native long objectFieldOffset(MemberName self);  // e.g., returns vmindex
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    58
    static native long staticFieldOffset(MemberName self);  // e.g., returns vmindex
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    59
    static native Object staticFieldBase(MemberName self);  // e.g., returns clazz
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
    60
    static native Object getMemberVMInfo(MemberName self);  // returns {vmindex,vmtarget}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    61
11006
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    62
    /// CallSite support
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    63
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    64
    /** Tell the JVM that we need to change the target of a CallSite. */
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    65
    static native void setCallSiteTargetNormal(CallSite site, MethodHandle target);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    66
    static native void setCallSiteTargetVolatile(CallSite site, MethodHandle target);
734080358bec 7085860: JSR 292: implement CallSite.setTargetNormal and setTargetVolatile as native methods
twisti
parents: 10615
diff changeset
    67
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    68
    static native void copyOutBootstrapArguments(Class<?> caller, int[] indexInfo,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    69
                                                 int start, int end,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    70
                                                 Object[] buf, int pos,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    71
                                                 boolean resolve,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    72
                                                 Object ifNotAvailable);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
    73
31070
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    74
    /** Represents a context to track nmethod dependencies on CallSite instance target. */
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    75
    static class CallSiteContext implements Runnable {
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    76
        //@Injected JVM_nmethodBucket* vmdependencies;
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    77
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    78
        static CallSiteContext make(CallSite cs) {
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    79
            final CallSiteContext newContext = new CallSiteContext();
36435
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    80
            // CallSite instance is tracked by a Cleanable which clears native
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    81
            // structures allocated for CallSite context. Though the CallSite can
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    82
            // become unreachable, its Context is retained by the Cleanable instance
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    83
            // (which is referenced from Cleaner instance which is referenced from
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    84
            // CleanerFactory class) until cleanup is performed.
0408881ad616 8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents: 35641
diff changeset
    85
            CleanerFactory.cleaner().register(cs, newContext);
31070
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    86
            return newContext;
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    87
        }
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    88
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    89
        @Override
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    90
        public void run() {
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    91
            MethodHandleNatives.clearCallSiteContext(this);
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    92
        }
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    93
    }
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    94
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    95
    /** Invalidate all recorded nmethods. */
d6c79efc10fe 8079205: CallSite dependency tracking is broken after sun.misc.Cleaner became automatically cleared
vlivanov
parents: 30362
diff changeset
    96
    private static native void clearCallSiteContext(CallSiteContext context);
30362
2b921e0eb3e0 8057967: CallSite dependency tracking scales devastatingly poorly
vlivanov
parents: 30342
diff changeset
    97
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    98
    private static native void registerNatives();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    99
    static {
9859
47e26ad535c4 7052202: JSR 292: Crash in sun.invoke.util.ValueConversions.fillArray
jrose
parents: 9780
diff changeset
   100
        registerNatives();
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   101
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
30342
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   103
    /**
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   104
     * Compile-time constants go here. This collection exists not only for
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   105
     * reference from clients, but also for ensuring the VM and JDK agree on the
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   106
     * values of these constants (see {@link #verifyConstants()}).
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   107
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   108
    static class Constants {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
        Constants() { } // static only
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   110
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   111
        static final int
30342
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   112
            MN_IS_METHOD           = 0x00010000, // method (not constructor)
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   113
            MN_IS_CONSTRUCTOR      = 0x00020000, // constructor
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   114
            MN_IS_FIELD            = 0x00040000, // field
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   115
            MN_IS_TYPE             = 0x00080000, // nested type
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   116
            MN_CALLER_SENSITIVE    = 0x00100000, // @CallerSensitive annotation detected
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   117
            MN_REFERENCE_KIND_SHIFT = 24, // refKind
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   118
            MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   119
            // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   120
            MN_SEARCH_SUPERCLASSES = 0x00100000,
82a0e2fb9892 8076461: JSR292: remove unused native and constants
vlivanov
parents: 25991
diff changeset
   121
            MN_SEARCH_INTERFACES   = 0x00200000;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   122
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   123
        /**
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   124
         * Constant pool reference-kind codes, as used by CONSTANT_MethodHandle CP entries.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   125
         */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   126
        static final byte
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   127
            REF_NONE                    = 0,  // null value
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   128
            REF_getField                = 1,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   129
            REF_getStatic               = 2,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   130
            REF_putField                = 3,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   131
            REF_putStatic               = 4,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   132
            REF_invokeVirtual           = 5,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   133
            REF_invokeStatic            = 6,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   134
            REF_invokeSpecial           = 7,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   135
            REF_newInvokeSpecial        = 8,
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   136
            REF_invokeInterface         = 9,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   137
            REF_LIMIT                  = 10;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   138
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   139
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   140
    static boolean refKindIsValid(int refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   141
        return (refKind > REF_NONE && refKind < REF_LIMIT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   142
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   143
    static boolean refKindIsField(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   144
        assert(refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   145
        return (refKind <= REF_putStatic);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   146
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   147
    static boolean refKindIsGetter(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   148
        assert(refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   149
        return (refKind <= REF_getStatic);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   150
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   151
    static boolean refKindIsSetter(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   152
        return refKindIsField(refKind) && !refKindIsGetter(refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   153
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   154
    static boolean refKindIsMethod(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   155
        return !refKindIsField(refKind) && (refKind != REF_newInvokeSpecial);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   156
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   157
    static boolean refKindIsConstructor(byte refKind) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   158
        return (refKind == REF_newInvokeSpecial);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   159
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   160
    static boolean refKindHasReceiver(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   161
        assert(refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   162
        return (refKind & 1) != 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   163
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   164
    static boolean refKindIsStatic(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   165
        return !refKindHasReceiver(refKind) && (refKind != REF_newInvokeSpecial);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   166
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   167
    static boolean refKindDoesDispatch(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   168
        assert(refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   169
        return (refKind == REF_invokeVirtual ||
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   170
                refKind == REF_invokeInterface);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   171
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   172
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   173
        final int HR_MASK = ((1 << REF_getField) |
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   174
                             (1 << REF_putField) |
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   175
                             (1 << REF_invokeVirtual) |
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   176
                             (1 << REF_invokeSpecial) |
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   177
                             (1 << REF_invokeInterface)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   178
                            );
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   179
        for (byte refKind = REF_NONE+1; refKind < REF_LIMIT; refKind++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   180
            assert(refKindHasReceiver(refKind) == (((1<<refKind) & HR_MASK) != 0)) : refKind;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   181
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   182
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   183
    static String refKindName(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   184
        assert(refKindIsValid(refKind));
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   185
        switch (refKind) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   186
        case REF_getField:          return "getField";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   187
        case REF_getStatic:         return "getStatic";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   188
        case REF_putField:          return "putField";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   189
        case REF_putStatic:         return "putStatic";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   190
        case REF_invokeVirtual:     return "invokeVirtual";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   191
        case REF_invokeStatic:      return "invokeStatic";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   192
        case REF_invokeSpecial:     return "invokeSpecial";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   193
        case REF_newInvokeSpecial:  return "newInvokeSpecial";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   194
        case REF_invokeInterface:   return "invokeInterface";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   195
        default:                    return "REF_???";
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   196
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   197
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   198
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   199
    private static native int getNamedCon(int which, Object[] name);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   200
    static boolean verifyConstants() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   201
        Object[] box = { null };
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   202
        for (int i = 0; ; i++) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   203
            box[0] = null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   204
            int vmval = getNamedCon(i, box);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   205
            if (box[0] == null)  break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   206
            String name = (String) box[0];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   207
            try {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   208
                Field con = Constants.class.getDeclaredField(name);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   209
                int jval = con.getInt(null);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   210
                if (jval == vmval)  continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   211
                String err = (name+": JVM has "+vmval+" while Java has "+jval);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   212
                if (name.equals("CONV_OP_LIMIT")) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   213
                    System.err.println("warning: "+err);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   214
                    continue;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   215
                }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   216
                throw new InternalError(err);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   217
            } catch (NoSuchFieldException | IllegalAccessException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   218
                String err = (name+": JVM has "+vmval+" which Java does not define");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   219
                // ignore exotic ops the JVM cares about; we just wont issue them
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   220
                //System.err.println("warning: "+err);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   221
                continue;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   222
            }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   223
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   224
        return true;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   225
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   226
    static {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   227
        assert(verifyConstants());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   228
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   229
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   230
    // Up-calls from the JVM.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   231
    // These must NOT be public.
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
    /**
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   234
     * The JVM is linking an invokedynamic instruction.  Create a reified call site for it.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   235
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   236
    static MemberName linkCallSite(Object callerObj,
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   237
                                   int indexInCP,
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   238
                                   Object bootstrapMethodObj,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   239
                                   Object nameObj, Object typeObj,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   240
                                   Object staticArguments,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   241
                                   Object[] appendixResult) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   242
        MethodHandle bootstrapMethod = (MethodHandle)bootstrapMethodObj;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   243
        Class<?> caller = (Class<?>)callerObj;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   244
        String name = nameObj.toString().intern();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   245
        MethodType type = (MethodType)typeObj;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   246
        if (!TRACE_METHOD_LINKAGE)
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   247
            return linkCallSiteImpl(caller, bootstrapMethod, name, type,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   248
                                    staticArguments, appendixResult);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   249
        return linkCallSiteTracing(caller, bootstrapMethod, name, type,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   250
                                   staticArguments, appendixResult);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   251
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   252
    static MemberName linkCallSiteImpl(Class<?> caller,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   253
                                       MethodHandle bootstrapMethod,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   254
                                       String name, MethodType type,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   255
                                       Object staticArguments,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   256
                                       Object[] appendixResult) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   257
        CallSite callSite = CallSite.makeSite(bootstrapMethod,
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   258
                                              name,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   259
                                              type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   260
                                              staticArguments,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   261
                                              caller);
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   262
        if (callSite instanceof ConstantCallSite) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   263
            appendixResult[0] = callSite.dynamicInvoker();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   264
            return Invokers.linkToTargetMethod(type);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   265
        } else {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   266
            appendixResult[0] = callSite;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   267
            return Invokers.linkToCallSiteMethod(type);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20498
diff changeset
   268
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   269
    }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   270
    // Tracing logic:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   271
    static MemberName linkCallSiteTracing(Class<?> caller,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   272
                                          MethodHandle bootstrapMethod,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   273
                                          String name, MethodType type,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   274
                                          Object staticArguments,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   275
                                          Object[] appendixResult) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   276
        Object bsmReference = bootstrapMethod.internalMemberName();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   277
        if (bsmReference == null)  bsmReference = bootstrapMethod;
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   278
        String staticArglist = staticArglistForTrace(staticArguments);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   279
        System.out.println("linkCallSite "+caller.getName()+" "+
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   280
                           bsmReference+" "+
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   281
                           name+type+"/"+staticArglist);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   282
        try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   283
            MemberName res = linkCallSiteImpl(caller, bootstrapMethod, name, type,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   284
                                              staticArguments, appendixResult);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   285
            System.out.println("linkCallSite => "+res+" + "+appendixResult[0]);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   286
            return res;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   287
        } catch (Throwable ex) {
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   288
            ex.printStackTrace(); // print now in case exception is swallowed
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   289
            System.out.println("linkCallSite => throw "+ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   290
            throw ex;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   291
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 21326
diff changeset
   292
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   293
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   294
    // this implements the upcall from the JVM, MethodHandleNatives.linkDynamicConstant:
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   295
    static Object linkDynamicConstant(Object callerObj,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   296
                                      int indexInCP,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   297
                                      Object bootstrapMethodObj,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   298
                                      Object nameObj, Object typeObj,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   299
                                      Object staticArguments) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   300
        MethodHandle bootstrapMethod = (MethodHandle)bootstrapMethodObj;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   301
        Class<?> caller = (Class<?>)callerObj;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   302
        String name = nameObj.toString().intern();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   303
        Class<?> type = (Class<?>)typeObj;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   304
        if (!TRACE_METHOD_LINKAGE)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   305
            return linkDynamicConstantImpl(caller, bootstrapMethod, name, type, staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   306
        return linkDynamicConstantTracing(caller, bootstrapMethod, name, type, staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   307
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   308
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   309
    static Object linkDynamicConstantImpl(Class<?> caller,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   310
                                          MethodHandle bootstrapMethod,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   311
                                          String name, Class<?> type,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   312
                                          Object staticArguments) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   313
        return DynamicConstant.makeConstant(bootstrapMethod, name, type, staticArguments, caller);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   314
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   315
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   316
    private static String staticArglistForTrace(Object staticArguments) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   317
        if (staticArguments instanceof Object[])
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   318
            return "BSA="+java.util.Arrays.asList((Object[]) staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   319
        if (staticArguments instanceof int[])
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   320
            return "BSA@"+java.util.Arrays.toString((int[]) staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   321
        if (staticArguments == null)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   322
            return "BSA0=null";
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   323
        return "BSA1="+staticArguments;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   324
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   325
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   326
    // Tracing logic:
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   327
    static Object linkDynamicConstantTracing(Class<?> caller,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   328
                                             MethodHandle bootstrapMethod,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   329
                                             String name, Class<?> type,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   330
                                             Object staticArguments) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   331
        Object bsmReference = bootstrapMethod.internalMemberName();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   332
        if (bsmReference == null)  bsmReference = bootstrapMethod;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   333
        String staticArglist = staticArglistForTrace(staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   334
        System.out.println("linkDynamicConstant "+caller.getName()+" "+
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   335
                           bsmReference+" "+
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   336
                           name+type+"/"+staticArglist);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   337
        try {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   338
            Object res = linkDynamicConstantImpl(caller, bootstrapMethod, name, type, staticArguments);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   339
            System.out.println("linkDynamicConstantImpl => "+res);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   340
            return res;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   341
        } catch (Throwable ex) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   342
            ex.printStackTrace(); // print now in case exception is swallowed
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   343
            System.out.println("linkDynamicConstant => throw "+ex);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   344
            throw ex;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   345
        }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   346
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   347
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   348
    /** The JVM is requesting pull-mode bootstrap when it provides
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   349
     *  a tuple of the form int[]{ argc, vmindex }.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   350
     *  The BSM is expected to call back to the JVM using the caller
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   351
     *  class and vmindex to resolve the static arguments.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   352
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   353
    static boolean staticArgumentsPulled(Object staticArguments) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   354
        return staticArguments instanceof int[];
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   355
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   356
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   357
    /** A BSM runs in pull-mode if and only if its sole arguments
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   358
     * are (Lookup, BootstrapCallInfo), or can be converted pairwise
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   359
     * to those types, and it is not of variable arity.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   360
     * Excluding error cases, we can just test that the arity is a constant 2.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   361
     *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   362
     * NOTE: This method currently returns false, since pulling is not currently
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   363
     * exposed to a BSM. When pull mode is supported the method block will be
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   364
     * replaced with currently commented out code.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   365
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   366
    static boolean isPullModeBSM(MethodHandle bsm) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   367
        return false;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   368
//        return bsm.type().parameterCount() == 2 && !bsm.isVarargsCollector();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   369
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   370
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   371
    /**
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   372
     * The JVM wants a pointer to a MethodType.  Oblige it by finding or creating one.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   373
     */
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   374
    static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   375
        return MethodType.makeImpl(rtype, ptypes, true);
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   376
    }
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   377
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   378
    /**
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   379
     * The JVM wants to link a call site that requires a dynamic type check.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   380
     * Name is a type-checking invoker, invokeExact or invoke.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   381
     * Return a JVM method (MemberName) to handle the invoking.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   382
     * The method assumes the following arguments on the stack:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   383
     * 0: the method handle being invoked
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   384
     * 1-N: the arguments to the method handle invocation
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   385
     * N+1: an optional, implicitly added argument (typically the given MethodType)
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   386
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   387
     * The nominal method at such a call site is an instance of
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   388
     * a signature-polymorphic method (see @PolymorphicSignature).
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   389
     * Such method instances are user-visible entities which are
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   390
     * "split" from the generic placeholder method in {@code MethodHandle}.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   391
     * (Note that the placeholder method is not identical with any of
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   392
     * its instances.  If invoked reflectively, is guaranteed to throw an
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   393
     * {@code UnsupportedOperationException}.)
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   394
     * If the signature-polymorphic method instance is ever reified,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   395
     * it appears as a "copy" of the original placeholder
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   396
     * (a native final member of {@code MethodHandle}) except
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   397
     * that its type descriptor has shape required by the instance,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   398
     * and the method instance is <em>not</em> varargs.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   399
     * The method instance is also marked synthetic, since the
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   400
     * method (by definition) does not appear in Java source code.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   401
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   402
     * The JVM is allowed to reify this method as instance metadata.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   403
     * For example, {@code invokeBasic} is always reified.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   404
     * But the JVM may instead call {@code linkMethod}.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   405
     * If the result is an * ordered pair of a {@code (method, appendix)},
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   406
     * the method gets all the arguments (0..N inclusive)
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   407
     * plus the appendix (N+1), and uses the appendix to complete the call.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   408
     * In this way, one reusable method (called a "linker method")
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   409
     * can perform the function of any number of polymorphic instance
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   410
     * methods.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   411
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   412
     * Linker methods are allowed to be weakly typed, with any or
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   413
     * all references rewritten to {@code Object} and any primitives
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   414
     * (except {@code long}/{@code float}/{@code double})
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   415
     * rewritten to {@code int}.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   416
     * A linker method is trusted to return a strongly typed result,
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   417
     * according to the specific method type descriptor of the
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   418
     * signature-polymorphic instance it is emulating.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   419
     * This can involve (as necessary) a dynamic check using
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   420
     * data extracted from the appendix argument.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   421
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   422
     * The JVM does not inspect the appendix, other than to pass
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   423
     * it verbatim to the linker method at every call.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   424
     * This means that the JDK runtime has wide latitude
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   425
     * for choosing the shape of each linker method and its
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   426
     * corresponding appendix.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   427
     * Linker methods should be generated from {@code LambdaForm}s
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   428
     * so that they do not become visible on stack traces.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   429
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   430
     * The {@code linkMethod} call is free to omit the appendix
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   431
     * (returning null) and instead emulate the required function
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   432
     * completely in the linker method.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   433
     * As a corner case, if N==255, no appendix is possible.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   434
     * In this case, the method returned must be custom-generated to
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   435
     * to perform any needed type checking.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   436
     * <p>
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   437
     * If the JVM does not reify a method at a call site, but instead
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   438
     * calls {@code linkMethod}, the corresponding call represented
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   439
     * in the bytecodes may mention a valid method which is not
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   440
     * representable with a {@code MemberName}.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   441
     * Therefore, use cases for {@code linkMethod} tend to correspond to
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   442
     * special cases in reflective code such as {@code findVirtual}
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 16906
diff changeset
   443
     * or {@code revealDirect}.
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   444
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   445
    static MemberName linkMethod(Class<?> callerClass, int refKind,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   446
                                 Class<?> defc, String name, Object type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   447
                                 Object[] appendixResult) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   448
        if (!TRACE_METHOD_LINKAGE)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   449
            return linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   450
        return linkMethodTracing(callerClass, refKind, defc, name, type, appendixResult);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   451
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   452
    static MemberName linkMethodImpl(Class<?> callerClass, int refKind,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   453
                                     Class<?> defc, String name, Object type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   454
                                     Object[] appendixResult) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   455
        try {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   456
            if (refKind == REF_invokeVirtual) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   457
                if (defc == MethodHandle.class) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   458
                    return Invokers.methodHandleInvokeLinkerMethod(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   459
                            name, fixMethodType(callerClass, type), appendixResult);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   460
                } else if (defc == VarHandle.class) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   461
                    return varHandleOperationLinkerMethod(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   462
                            name, fixMethodType(callerClass, type), appendixResult);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   463
                }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   464
            }
42105
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   465
        } catch (Error e) {
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   466
            // Pass through an Error, including say StackOverflowError or
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   467
            // OutOfMemoryError
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   468
            throw e;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   469
        } catch (Throwable ex) {
42105
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   470
            // Wrap anything else in LinkageError
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 37792
diff changeset
   471
            throw new LinkageError(ex.getMessage(), ex);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   472
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   473
        throw new LinkageError("no such method "+defc.getName()+"."+name+type);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   474
    }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   475
    private static MethodType fixMethodType(Class<?> callerClass, Object type) {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   476
        if (type instanceof MethodType)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   477
            return (MethodType) type;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   478
        else
32986
ea54ac8672e7 8136893: Improve early java.lang.invoke infrastructure initialization
plevart
parents: 32649
diff changeset
   479
            return MethodType.fromDescriptor((String)type, callerClass.getClassLoader());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   480
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   481
    // Tracing logic:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   482
    static MemberName linkMethodTracing(Class<?> callerClass, int refKind,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   483
                                        Class<?> defc, String name, Object type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   484
                                        Object[] appendixResult) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   485
        System.out.println("linkMethod "+defc.getName()+"."+
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   486
                           name+type+"/"+Integer.toHexString(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   487
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   488
            MemberName res = linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   489
            System.out.println("linkMethod => "+res+" + "+appendixResult[0]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   490
            return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   491
        } catch (Throwable ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   492
            System.out.println("linkMethod => throw "+ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   493
            throw ex;
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   494
        }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   495
    }
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   496
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   497
    /**
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   498
     * Obtain the method to link to the VarHandle operation.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   499
     * This method is located here and not in Invokers to avoid
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   500
     * intializing that and other classes early on in VM bootup.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   501
     */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   502
    private static MemberName varHandleOperationLinkerMethod(String name,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   503
                                                             MethodType mtype,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   504
                                                             Object[] appendixResult) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   505
        // Get the signature method type
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   506
        final MethodType sigType = mtype.basicType();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   507
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   508
        // Get the access kind from the method name
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   509
        VarHandle.AccessMode ak;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   510
        try {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   511
            ak = VarHandle.AccessMode.valueFromMethodName(name);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   512
        } catch (IllegalArgumentException e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   513
            throw MethodHandleStatics.newInternalError(e);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   514
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   515
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   516
        // Create the appendix descriptor constant
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   517
        VarHandle.AccessDescriptor ad = new VarHandle.AccessDescriptor(mtype, ak.at.ordinal(), ak.ordinal());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   518
        appendixResult[0] = ad;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   519
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   520
        if (MethodHandleStatics.VAR_HANDLE_GUARDS) {
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   521
            // If not polymorphic in the return type, such as the compareAndSet
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   522
            // methods that return boolean
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   523
            Class<?> guardReturnType = sigType.returnType();
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   524
            if (ak.at.isMonomorphicInReturnType) {
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   525
                if (ak.at.returnType != mtype.returnType()) {
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   526
                    // The caller contains a different return type than that
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   527
                    // defined by the method
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   528
                    throw newNoSuchMethodErrorOnVarHandle(name, mtype);
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   529
                }
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   530
                // Adjust the return type of the signature method type
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   531
                guardReturnType = ak.at.returnType;
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   532
            }
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   533
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   534
            // Get the guard method type for linking
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   535
            final Class<?>[] guardParams = new Class<?>[sigType.parameterCount() + 2];
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   536
            // VarHandle at start
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   537
            guardParams[0] = VarHandle.class;
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   538
            for (int i = 0; i < sigType.parameterCount(); i++) {
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   539
                guardParams[i + 1] = sigType.parameterType(i);
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   540
            }
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   541
            // Access descriptor at end
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   542
            guardParams[guardParams.length - 1] = VarHandle.AccessDescriptor.class;
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   543
            MethodType guardType = MethodType.makeImpl(guardReturnType, guardParams, true);
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   544
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   545
            MemberName linker = new MemberName(
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   546
                    VarHandleGuards.class, getVarHandleGuardMethodName(guardType),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   547
                    guardType, REF_invokeStatic);
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   548
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   549
            linker = MemberName.getFactory().resolveOrNull(REF_invokeStatic, linker,
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   550
                                                           VarHandleGuards.class);
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   551
            if (linker != null) {
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   552
                return linker;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   553
            }
37792
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   554
            // Fall back to lambda form linkage if guard method is not available
dd626e6f5967 8155258: VarHandle implementation improvements
psandoz
parents: 37585
diff changeset
   555
            // TODO Optionally log fallback ?
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   556
        }
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 43892
diff changeset
   557
        return Invokers.varHandleInvokeLinkerMethod(ak, mtype);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   558
    }
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   559
    static String getVarHandleGuardMethodName(MethodType guardType) {
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   560
        String prefix = "guard_";
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   561
        StringBuilder sb = new StringBuilder(prefix.length() + guardType.parameterCount());
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   562
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   563
        sb.append(prefix);
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   564
        for (int i = 1; i < guardType.parameterCount() - 1; i++) {
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   565
            Class<?> pt = guardType.parameterType(i);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   566
            sb.append(getCharType(pt));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   567
        }
43892
065331fc590e 8172298: Reduce memory churn when linking VarHandles operations
psandoz
parents: 42105
diff changeset
   568
        sb.append('_').append(getCharType(guardType.returnType()));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   569
        return sb.toString();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   570
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   571
    static char getCharType(Class<?> pt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   572
        return Wrapper.forBasicType(pt).basicTypeChar();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   573
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   574
    static NoSuchMethodError newNoSuchMethodErrorOnVarHandle(String name, MethodType mtype) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   575
        return new NoSuchMethodError("VarHandle." + name + mtype);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 36435
diff changeset
   576
    }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   577
7554
8a0ad9757002 6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents: 7052
diff changeset
   578
    /**
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   579
     * The JVM is resolving a CONSTANT_MethodHandle CP entry.  And it wants our help.
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   580
     * It will make an up-call to this method.  (Do not change the name or signature.)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   581
     * The type argument is a Class for field requests and a MethodType for non-fields.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   582
     * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   583
     * Recent versions of the JVM may also pass a resolved MemberName for the type.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   584
     * In that case, the name is ignored and may be null.
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   585
     */
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   586
    static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   587
                                                 Class<?> defc, String name, Object type) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   588
        try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5727
diff changeset
   589
            Lookup lookup = IMPL_LOOKUP.in(callerClass);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   590
            assert(refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11006
diff changeset
   591
            return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type);
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   592
        } catch (ReflectiveOperationException ex) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   593
            throw mapLookupExceptionToError(ex);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   594
        }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   595
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   596
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   597
    /**
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   598
     * Map a reflective exception to a linkage error.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   599
     */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   600
    static LinkageError mapLookupExceptionToError(ReflectiveOperationException ex) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   601
        LinkageError err;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   602
        if (ex instanceof IllegalAccessException) {
21326
6abd31ba3aba 8026818: Defmeth failures with -mode invoke
drchase
parents: 20527
diff changeset
   603
            Throwable cause = ex.getCause();
6abd31ba3aba 8026818: Defmeth failures with -mode invoke
drchase
parents: 20527
diff changeset
   604
            if (cause instanceof AbstractMethodError) {
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   605
                return (AbstractMethodError) cause;
21326
6abd31ba3aba 8026818: Defmeth failures with -mode invoke
drchase
parents: 20527
diff changeset
   606
            } else {
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   607
                err = new IllegalAccessError(ex.getMessage());
21326
6abd31ba3aba 8026818: Defmeth failures with -mode invoke
drchase
parents: 20527
diff changeset
   608
            }
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   609
        } else if (ex instanceof NoSuchMethodException) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   610
            err = new NoSuchMethodError(ex.getMessage());
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   611
        } else if (ex instanceof NoSuchFieldException) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   612
            err = new NoSuchFieldError(ex.getMessage());
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   613
        } else {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   614
            err = new IncompatibleClassChangeError();
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   615
        }
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   616
        return initCauseFrom(err, ex);
5726
72c9d793224b 6939203: JSR 292 needs method handle constants
jrose
parents: 5722
diff changeset
   617
    }
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   618
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   619
    /**
20498
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   620
     * Use best possible cause for err.initCause(), substituting the
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   621
     * cause for err itself if the cause has the same (or better) type.
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   622
     */
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   623
    static <E extends Error> E initCauseFrom(E err, Exception ex) {
20498
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   624
        Throwable th = ex.getCause();
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   625
        @SuppressWarnings("unchecked")
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   626
        final Class<E> Eclass = (Class<E>) err.getClass();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   627
        if (Eclass.isInstance(th))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47216
diff changeset
   628
           return Eclass.cast(th);
20498
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   629
        err.initCause(th == null ? ex : th);
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   630
        return err;
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   631
    }
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   632
6b1da6741f95 8022701: Accessibility checking: InvocationTargetException is thrown instead of IllegalAccessError
drchase
parents: 19804
diff changeset
   633
    /**
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   634
     * Is this method a caller-sensitive method?
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24572
diff changeset
   635
     * I.e., does it call Reflection.getCallerClass or a similar method
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   636
     * to ask about the identity of its caller?
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   637
     */
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   638
    static boolean isCallerSensitive(MemberName mem) {
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   639
        if (!mem.isInvocable())  return false;  // fields are not caller sensitive
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   640
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   641
        return mem.isCallerSensitive() || canBeCalledVirtual(mem);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   642
    }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   643
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   644
    static boolean canBeCalledVirtual(MemberName mem) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16121
diff changeset
   645
        assert(mem.isInvocable());
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   646
        Class<?> defc = mem.getDeclaringClass();
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   647
        switch (mem.getName()) {
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   648
        case "checkMemberAccess":
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   649
            return canBeCalledVirtual(mem, java.lang.SecurityManager.class);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   650
        case "getContextClassLoader":
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   651
            return canBeCalledVirtual(mem, java.lang.Thread.class);
14222
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   652
        }
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   653
        return false;
58f55d4dde46 7196190: Improve method of handling MethodHandles
jrose
parents: 13610
diff changeset
   654
    }
14679
e1fbc33baf51 8003851: MethodHandleNatives dependency on java.sql.DriverManager
mchung
parents: 14222
diff changeset
   655
16118
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   656
    static boolean canBeCalledVirtual(MemberName symbolicRef, Class<?> definingClass) {
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   657
        Class<?> symbolicRefClass = symbolicRef.getDeclaringClass();
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   658
        if (symbolicRefClass == definingClass)  return true;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   659
        if (symbolicRef.isStatic() || symbolicRef.isPrivate())  return false;
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   660
        return (definingClass.isAssignableFrom(symbolicRefClass) ||  // Msym overrides Mdef
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   661
                symbolicRefClass.isInterface());                     // Mdef implements Msym
9f3390f42157 8006439: Improve MethodHandles coverage
vlivanov
parents: 16110
diff changeset
   662
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   663
}