src/java.base/share/classes/java/lang/invoke/package-info.java
author psandoz
Fri, 08 Sep 2017 10:46:46 -0700
changeset 48826 c4d9d1b08e2e
parent 47250 a0f26f0da4f1
child 49576 535498e7602f
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
/*
45433
01ecdcf197df 8181290: Invalid HTML 5 in core-libs docs
jjg
parents: 42103
diff changeset
     2
 * Copyright (c) 2008, 2017, 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
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    26
/**
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    27
 * The {@code java.lang.invoke} package provides low-level primitives for interacting
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    28
 * with the Java Virtual Machine.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    29
 *
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    30
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    31
 * As described in the Java Virtual Machine Specification, certain types in this package
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    32
 * are given special treatment by the virtual machine:
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    33
 * <ul>
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 25859
diff changeset
    34
 * <li>The classes {@link java.lang.invoke.MethodHandle MethodHandle}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 25859
diff changeset
    35
 * {@link java.lang.invoke.VarHandle VarHandle} contain
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    36
 * <a href="MethodHandle.html#sigpoly">signature polymorphic methods</a>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    37
 * which can be linked regardless of their type descriptor.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
    38
 * Normally, method linkage requires exact matching of type descriptors.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    39
 * </li>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    40
 *
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    41
 * <li>The JVM bytecode format supports immediate constants of
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    42
 * the classes {@link java.lang.invoke.MethodHandle MethodHandle} and
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    43
 * {@link java.lang.invoke.MethodType MethodType}.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    44
 * </li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    45
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    46
 * <li>The {@code invokedynamic} instruction makes use of bootstrap {@code MethodHandle}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    47
 * constants to dynamically resolve {@code CallSite} objects for custom method invocation
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    48
 * behavior.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    49
 * </li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    50
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    51
 * <li>The {@code ldc} instruction makes use of bootstrap {@code MethodHandle} constants
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    52
 * to dynamically resolve custom constant values.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    53
 * </li>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    54
 * </ul>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    55
 *
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    56
 * <h1><a id="jvm_mods"></a>Dynamic resolution of call sites and constants</h1>
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    57
 * The following low-level information summarizes relevant parts of the
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    58
 * Java Virtual Machine specification.  For full details, please see the
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    59
 * current version of that specification.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    60
 *
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    61
 * <h2><a id="indyinsn"></a>Dynamically-computed call sites</h2>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    62
 * An {@code invokedynamic} instruction is originally in an unlinked state.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    63
 * In this state, there is no target method for the instruction to invoke.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    64
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    65
 * Before the JVM can execute an {@code invokedynamic} instruction,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    66
 * the instruction must first be <em>linked</em>.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    67
 * Linking is accomplished by calling a <em>bootstrap method</em>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    68
 * which is given the static information content of the call,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    69
 * and which must produce a {@link java.lang.invoke.CallSite}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    70
 * that gives the behavior of the invocation.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    71
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    72
 * Each {@code invokedynamic} instruction statically specifies its own
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    73
 * bootstrap method as a constant pool reference.
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    74
 * The constant pool reference also specifies the invocation's name and method type descriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    75
 * just like {@code invokestatic} and the other invoke instructions.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    76
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    77
 * <h2><a id="condycon"></a>Dynamically-computed constants</h2>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    78
 * The constant pool may contain constants tagged {@code CONSTANT_Dynamic},
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    79
 * equipped with bootstrap methods which perform their resolution.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    80
 * Such a <em>dynamic constant</em> is originally in an unresolved state.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    81
 * Before the JVM can use a dynamically-computed constant, it must first be <em>resolved</em>.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    82
 * Dynamically-computed constant resolution is accomplished by calling a <em>bootstrap method</em>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    83
 * which is given the static information content of the constant,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    84
 * and which must produce a value of the constant's statically declared type.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    85
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    86
 * Each dynamically-computed constant statically specifies its own
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    87
 * bootstrap method as a constant pool reference.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    88
 * The constant pool reference also specifies the constant's name and field type descriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    89
 * just like {@code getstatic} and the other field reference instructions.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    90
 * (Roughly speaking, a dynamically-computed constant is to a dynamically-computed call site
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    91
 * as a {@code CONSTANT_Fieldref} is to a {@code CONSTANT_Methodref}.)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    92
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    93
 * <h2><a id="bsm"></a>Execution of bootstrap methods</h2>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    94
 * Resolving a dynamically-computed call site or constant
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    95
 * starts with resolving constants from the constant pool for the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    96
 * following items:
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    97
 * <ul>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    98
 * <li>the bootstrap method, a {@code CONSTANT_MethodHandle}</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
    99
 * <li>the {@code Class} or {@code MethodType} derived from
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   100
 * type component of the {@code CONSTANT_NameAndType} descriptor</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   101
 * <li>static arguments, if any (note that static arguments can themselves be
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   102
 * dynamically-computed constants)</li>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   103
 * </ul>
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   104
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   105
 * The bootstrap method is then invoked, as if by
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   106
 * {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke},
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   107
 * with the following arguments:
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   108
 * <ul>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   109
 * <li>a {@code MethodHandles.Lookup}, which is a lookup object on the <em>caller class</em>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   110
 * in which dynamically-computed constant or call site occurs</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   111
 * <li>a {@code String}, the name mentioned in the {@code CONSTANT_NameAndType}</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   112
 * <li>a {@code MethodType} or {@code Class}, the resolved type descriptor of the {@code CONSTANT_NameAndType}</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   113
 * <li>a {@code Class}, the resolved type descriptor of the constant, if it is a dynamic constant </li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   114
 * <li>the additional resolved static arguments, if any</li>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   115
 * </ul>
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   116
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   117
 * For a dynamically-computed call site, the returned result must be a non-null reference to a
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   118
 * {@link java.lang.invoke.CallSite CallSite}.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   119
 * The type of the call site's target must be exactly equal to the type
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   120
 * derived from the invocation's type descriptor and passed to
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   121
 * the bootstrap method. If these conditions are not met, a {@code BootstrapMethodError} is thrown.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   122
 * On success the call site then becomes permanently linked to the {@code invokedynamic}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   123
 * instruction.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   124
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   125
 * For a dynamically-computed constant, the result of the bootstrap method is cached
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   126
 * as the resolved constant value.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   127
 * <p>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   128
 * If an exception, {@code E} say, occurs during execution of the bootstrap method, then
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   129
 * resolution fails and terminates abnormally. {@code E} is rethrown if the type of
42103
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 36934
diff changeset
   130
 * {@code E} is {@code Error} or a subclass, otherwise a
8e1fe4345fd7 8166974: invokedynamic implementation should not wrap Errors
psandoz
parents: 36934
diff changeset
   131
 * {@code BootstrapMethodError} that wraps {@code E} is thrown.
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   132
 * If this happens, the same error will be thrown for all
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   133
 * subsequent attempts to execute the {@code invokedynamic} instruction or load the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   134
 * dynamically-computed constant.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   135
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   136
 * <h2>Timing of resolution</h2>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   137
 * An {@code invokedynamic} instruction is linked just before its first execution.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   138
 * A dynamically-computed constant is resolved just before the first time it is used
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   139
 * (by pushing it on the stack or linking it as a bootstrap method parameter).
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   140
 * The bootstrap method call implementing the linkage occurs within
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   141
 * a thread that is attempting a first execution or first use.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   142
 * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   143
 * If there are several such threads, the bootstrap method may be
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   144
 * invoked in several threads concurrently.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   145
 * Therefore, bootstrap methods which access global application
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   146
 * data must take the usual precautions against race conditions.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   147
 * In any case, every {@code invokedynamic} instruction is either
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   148
 * unlinked or linked to a unique {@code CallSite} object.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   149
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   150
 * In an application which requires {@code invokedynamic} instructions with individually
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   151
 * mutable behaviors, their bootstrap methods should produce distinct
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8349
diff changeset
   152
 * {@link java.lang.invoke.CallSite CallSite} objects, one for each linkage request.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   153
 * Alternatively, an application can link a single {@code CallSite} object
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   154
 * to several {@code invokedynamic} instructions, in which case
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   155
 * a change to the target method will become visible at each of
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   156
 * the instructions.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   157
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   158
 * If several threads simultaneously execute a bootstrap method for a single dynamically-computed
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   159
 * call site or constant, the JVM must choose one bootstrap method result and install it visibly to
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   160
 * all threads.  Any other bootstrap method calls are allowed to complete, but their
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   161
 * results are ignored.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   162
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   163
 * <p style="font-size:smaller;">
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8349
diff changeset
   164
 * <em>Discussion:</em>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   165
 * These rules do not enable the JVM to share call sites,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   166
 * or to issue &ldquo;causeless&rdquo; bootstrap method calls.
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   167
 * Every {@code invokedynamic} instruction transitions at most once from unlinked to linked,
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   168
 * just before its first invocation.
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8349
diff changeset
   169
 * There is no way to undo the effect of a completed bootstrap method call.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   170
 *
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   171
 * <h2>Types of bootstrap methods</h2>
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   172
 * For a dynamically-computed call site, the bootstrap method is invoked with parameter
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   173
 * types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   174
 * of any static arguments; the return type is {@code CallSite}. For a
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   175
 * dynamically-computed constant, the bootstrap method is invoked with parameter types
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   176
 * {@code MethodHandles.Lookup}, {@code String}, {@code Class}, and the types of any
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   177
 * static arguments; the return type is the type represented by the {@code Class}.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   178
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   179
 * Because {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke} allows for
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   180
 * adaptations between the invoked method type and the method handle's method type,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   181
 * there is flexibility in the declaration of the bootstrap method.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   182
 * For example, the first argument could be {@code Object}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   183
 * instead of {@code MethodHandles.Lookup}, and the return type
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   184
 * could also be {@code Object} instead of {@code CallSite}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   185
 * (Note that the types and number of the stacked arguments limit
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   186
 * the legal kinds of bootstrap methods to appropriately typed
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   187
 * static methods and constructors.)
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   188
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   189
 * If a pushed value is a primitive type, it may be converted to a reference by boxing conversion.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   190
 * If the bootstrap method is a variable arity method (its modifier bit {@code 0x0080} is set),
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   191
 * then some or all of the arguments specified here may be collected into a trailing array parameter.
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   192
 * (This is not a special rule, but rather a useful consequence of the interaction
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   193
 * between {@code CONSTANT_MethodHandle} constants, the modifier bit for variable arity methods,
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   194
 * and the {@link java.lang.invoke.MethodHandle#asVarargsCollector asVarargsCollector} transformation.)
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   195
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   196
 * Given these rules, here are examples of legal bootstrap method declarations for
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   197
 * dynamically-computed call sites, given various numbers {@code N} of extra arguments.
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   198
 * The first row (marked {@code *}) will work for any number of extra arguments.
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   199
 * <table class="plain" style="vertical-align:top">
45437
c48b8edad8dd 8181156: html5 issues in java.base javadoc
rriggs
parents: 45433
diff changeset
   200
 * <caption style="display:none">Static argument types</caption>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   201
 * <thead>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   202
 * <tr><th scope="col">N</th><th scope="col">Sample bootstrap method</th></tr>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   203
 * </thead>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   204
 * <tbody>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   205
 * <tr><th scope="row" style="font-weight:normal; vertical-align:top">*</th><td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   206
 *     <ul style="list-style:none; padding-left: 0; margin:0">
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   207
 *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   208
 *     <li><code>CallSite bootstrap(Object... args)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   209
 *     <li><code>CallSite bootstrap(Object caller, Object... nameAndTypeWithArgs)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   210
 *     </ul></td></tr>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   211
 * <tr><th scope="row" style="font-weight:normal; vertical-align:top">0</th><td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   212
 *     <ul style="list-style:none; padding-left: 0; margin:0">
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   213
 *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   214
 *     <li><code>CallSite bootstrap(Lookup caller, Object... nameAndType)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   215
 *     </ul></td></tr>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   216
 * <tr><th scope="row" style="font-weight:normal; vertical-align:top">1</th><td>
45437
c48b8edad8dd 8181156: html5 issues in java.base javadoc
rriggs
parents: 45433
diff changeset
   217
 *     <code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object arg)</code></td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   218
 * <tr><th scope="row" style="font-weight:normal; vertical-align:top">2</th><td>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   219
 *     <ul style="list-style:none; padding-left: 0; margin:0">
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   220
 *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   221
 *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String... args)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   222
 *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String x, int y)</code>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   223
 *     </ul></td></tr>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45437
diff changeset
   224
 * </tbody>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   225
 * </table>
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   226
 * The last example assumes that the extra arguments are of type
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   227
 * {@code String} and {@code Integer} (or {@code int}), respectively.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   228
 * The second-to-last example assumes that all extra arguments are of type
47250
a0f26f0da4f1 8185993: MethodHandle.invokeWithArguments jumbo-arity
jrose
parents: 47216
diff changeset
   229
 * {@code String}.
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7668
diff changeset
   230
 * The other examples work with all types of extra arguments.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   231
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   232
 * Since dynamically-computed constants can be provided as static arguments to bootstrap
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   233
 * methods, there are no limitations on the types of bootstrap arguments.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   234
 * However, arguments of type {@code boolean}, {@code byte}, {@code short}, or {@code char}
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   235
 * cannot be <em>directly</em> supplied by {@code CONSTANT_Integer}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   236
 * constant pool entries, since the {@code asType} conversions do
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   237
 * not perform the necessary narrowing primitive conversions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   238
 * <p>
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   239
 * In the above examples, the return type is always {@code CallSite},
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   240
 * but that is not a necessary feature of bootstrap methods.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   241
 * In the case of a dynamically-computed call site, the only requirement is that
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   242
 * the return type of the bootstrap method must be convertible
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   243
 * (using the {@code asType} conversions) to {@code CallSite}, which
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   244
 * means the bootstrap method return type might be {@code Object} or
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   245
 * {@code ConstantCallSite}.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   246
 * In the case of a dynamically-resolved constant, the return type of the bootstrap
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   247
 * method must be convertible to the type of the constant, as
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   248
 * represented by its field type descriptor.  For example, if the
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   249
 * dynamic constant has a field type descriptor of {@code "C"}
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   250
 * ({@code char}) then the bootstrap method return type could be
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   251
 * {@code Object}, {@code Character}, or {@code char}, but not
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents: 47250
diff changeset
   252
 * {@code int} or {@code Integer}.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   253
 *
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   254
 * @author John Rose, JSR 292 EG
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8349
diff changeset
   255
 * @since 1.7
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   256
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   257
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8349
diff changeset
   258
package java.lang.invoke;