John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 1 | /* |
Shilpi Rastogi | ff97659 | 2016-02-10 11:04:13 +0100 | [diff] [blame] | 2 | * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. Oracle designates this |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 8 | * particular file as subject to the "Classpath" exception as provided |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 9 | * by Oracle in the LICENSE file that accompanied this code. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | * or visit www.oracle.com if you need additional information or have any |
| 23 | * questions. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
John R Rose | 3718824 | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 26 | package java.lang.invoke; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 27 | |
Paul Sandoz | c464a9d | 2015-12-03 11:17:31 +0100 | [diff] [blame] | 28 | import jdk.internal.vm.annotation.DontInline; |
| 29 | import jdk.internal.vm.annotation.ForceInline; |
| 30 | import jdk.internal.vm.annotation.Stable; |
| 31 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 32 | import java.lang.reflect.Array; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 33 | import java.util.Arrays; |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 34 | |
Christian Thalinger | 01d0ba6 | 2012-10-19 17:04:35 -0700 | [diff] [blame] | 35 | import static java.lang.invoke.MethodHandleStatics.*; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 36 | import static java.lang.invoke.MethodHandleNatives.Constants.*; |
John R Rose | 3718824 | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 37 | import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 38 | import static java.lang.invoke.LambdaForm.*; |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 39 | import static java.lang.invoke.LambdaForm.Kind.*; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Construction and caching of often-used invokers. |
| 43 | * @author jrose |
| 44 | */ |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame] | 45 | class Invokers { |
Pavel Rappo | 7075a99 | 2014-08-11 21:03:59 +0100 | [diff] [blame] | 46 | // exact type (sans leading target MH) for the outgoing call |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 47 | private final MethodType targetType; |
| 48 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 49 | // Cached adapter information: |
| 50 | private final @Stable MethodHandle[] invokers = new MethodHandle[INV_LIMIT]; |
| 51 | // Indexes into invokers: |
| 52 | static final int |
| 53 | INV_EXACT = 0, // MethodHandles.exactInvoker |
| 54 | INV_GENERIC = 1, // MethodHandles.invoker (generic invocation) |
| 55 | INV_BASIC = 2, // MethodHandles.basicInvoker |
| 56 | INV_LIMIT = 3; |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 57 | |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 58 | /** Compute and cache information common to all collecting adapters |
| 59 | * that implement members of the erasure-family of the given erased type. |
| 60 | */ |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 61 | /*non-public*/ Invokers(MethodType targetType) { |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 62 | this.targetType = targetType; |
| 63 | } |
| 64 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame] | 65 | /*non-public*/ MethodHandle exactInvoker() { |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 66 | MethodHandle invoker = cachedInvoker(INV_EXACT); |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 67 | if (invoker != null) return invoker; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 68 | invoker = makeExactOrGeneralInvoker(true); |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 69 | return setCachedInvoker(INV_EXACT, invoker); |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 72 | /*non-public*/ MethodHandle genericInvoker() { |
| 73 | MethodHandle invoker = cachedInvoker(INV_GENERIC); |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 74 | if (invoker != null) return invoker; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 75 | invoker = makeExactOrGeneralInvoker(false); |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 76 | return setCachedInvoker(INV_GENERIC, invoker); |
| 77 | } |
| 78 | |
| 79 | /*non-public*/ MethodHandle basicInvoker() { |
| 80 | MethodHandle invoker = cachedInvoker(INV_BASIC); |
| 81 | if (invoker != null) return invoker; |
| 82 | MethodType basicType = targetType.basicType(); |
| 83 | if (basicType != targetType) { |
| 84 | // double cache; not used significantly |
| 85 | return setCachedInvoker(INV_BASIC, basicType.invokers().basicInvoker()); |
| 86 | } |
| 87 | invoker = basicType.form().cachedMethodHandle(MethodTypeForm.MH_BASIC_INV); |
| 88 | if (invoker == null) { |
| 89 | MemberName method = invokeBasicMethod(basicType); |
| 90 | invoker = DirectMethodHandle.make(method); |
| 91 | assert(checkInvoker(invoker)); |
| 92 | invoker = basicType.form().setCachedMethodHandle(MethodTypeForm.MH_BASIC_INV, invoker); |
| 93 | } |
| 94 | return setCachedInvoker(INV_BASIC, invoker); |
| 95 | } |
| 96 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 97 | /*non-public*/ MethodHandle varHandleMethodInvoker(VarHandle.AccessMode ak) { |
| 98 | // TODO cache invoker |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 99 | return makeVarHandleMethodInvoker(ak, false); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /*non-public*/ MethodHandle varHandleMethodExactInvoker(VarHandle.AccessMode ak) { |
| 103 | // TODO cache invoker |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 104 | return makeVarHandleMethodInvoker(ak, true); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 105 | } |
| 106 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 107 | private MethodHandle cachedInvoker(int idx) { |
| 108 | return invokers[idx]; |
| 109 | } |
| 110 | |
| 111 | private synchronized MethodHandle setCachedInvoker(int idx, final MethodHandle invoker) { |
| 112 | // Simulate a CAS, to avoid racy duplication of results. |
| 113 | MethodHandle prev = invokers[idx]; |
| 114 | if (prev != null) return prev; |
| 115 | return invokers[idx] = invoker; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 118 | private MethodHandle makeExactOrGeneralInvoker(boolean isExact) { |
| 119 | MethodType mtype = targetType; |
| 120 | MethodType invokerType = mtype.invokerType(); |
| 121 | int which = (isExact ? MethodTypeForm.LF_EX_INVOKER : MethodTypeForm.LF_GEN_INVOKER); |
| 122 | LambdaForm lform = invokeHandleForm(mtype, false, which); |
| 123 | MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, mtype); |
| 124 | String whichName = (isExact ? "invokeExact" : "invoke"); |
John Rose | fb7b2ac | 2014-09-10 19:19:49 +0400 | [diff] [blame] | 125 | invoker = invoker.withInternalMemberName(MemberName.makeMethodHandleInvoke(whichName, mtype), false); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 126 | assert(checkInvoker(invoker)); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 127 | maybeCompileToBytecode(invoker); |
John R Rose | 025d0ae | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 128 | return invoker; |
| 129 | } |
| 130 | |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 131 | private MethodHandle makeVarHandleMethodInvoker(VarHandle.AccessMode ak, boolean isExact) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 132 | MethodType mtype = targetType; |
| 133 | MethodType invokerType = mtype.insertParameterTypes(0, VarHandle.class); |
| 134 | |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 135 | LambdaForm lform = varHandleMethodInvokerHandleForm(ak, mtype, isExact); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 136 | VarHandle.AccessDescriptor ad = new VarHandle.AccessDescriptor(mtype, ak.at.ordinal(), ak.ordinal()); |
| 137 | MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, ad); |
| 138 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 139 | invoker = invoker.withInternalMemberName(MemberName.makeVarHandleMethodInvoke(ak.methodName(), mtype), false); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 140 | assert(checkVarHandleInvoker(invoker)); |
| 141 | |
| 142 | maybeCompileToBytecode(invoker); |
| 143 | return invoker; |
| 144 | } |
| 145 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 146 | /** If the target type seems to be common enough, eagerly compile the invoker to bytecodes. */ |
| 147 | private void maybeCompileToBytecode(MethodHandle invoker) { |
| 148 | final int EAGER_COMPILE_ARITY_LIMIT = 10; |
| 149 | if (targetType == targetType.erase() && |
| 150 | targetType.parameterCount() < EAGER_COMPILE_ARITY_LIMIT) { |
| 151 | invoker.form.compileToBytecode(); |
| 152 | } |
| 153 | } |
| 154 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 155 | // This next one is called from LambdaForm.NamedFunction.<init>. |
| 156 | /*non-public*/ static MemberName invokeBasicMethod(MethodType basicType) { |
| 157 | assert(basicType == basicType.basicType()); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 158 | try { |
| 159 | //Lookup.findVirtual(MethodHandle.class, name, type); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 160 | return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 161 | } catch (ReflectiveOperationException ex) { |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 162 | throw newInternalError("JVM cannot find invoker for "+basicType, ex); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | private boolean checkInvoker(MethodHandle invoker) { |
| 167 | assert(targetType.invokerType().equals(invoker.type())) |
| 168 | : java.util.Arrays.asList(targetType, targetType.invokerType(), invoker); |
| 169 | assert(invoker.internalMemberName() == null || |
| 170 | invoker.internalMemberName().getMethodType().equals(targetType)); |
| 171 | assert(!invoker.isVarargsCollector()); |
| 172 | return true; |
| 173 | } |
| 174 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 175 | private boolean checkVarHandleInvoker(MethodHandle invoker) { |
| 176 | MethodType invokerType = targetType.insertParameterTypes(0, VarHandle.class); |
| 177 | assert(invokerType.equals(invoker.type())) |
| 178 | : java.util.Arrays.asList(targetType, invokerType, invoker); |
| 179 | assert(invoker.internalMemberName() == null || |
| 180 | invoker.internalMemberName().getMethodType().equals(targetType)); |
| 181 | assert(!invoker.isVarargsCollector()); |
| 182 | return true; |
| 183 | } |
| 184 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 185 | /** |
| 186 | * Find or create an invoker which passes unchanged a given number of arguments |
| 187 | * and spreads the rest from a trailing array argument. |
| 188 | * The invoker target type is the post-spread type {@code (TYPEOF(uarg*), TYPEOF(sarg*))=>RT}. |
| 189 | * All the {@code sarg}s must have a common type {@code C}. (If there are none, {@code Object} is assumed.} |
| 190 | * @param leadingArgCount the number of unchanged (non-spread) arguments |
| 191 | * @return {@code invoker.invokeExact(mh, uarg*, C[]{sarg*}) := (RT)mh.invoke(uarg*, sarg*)} |
| 192 | */ |
John R Rose | 025d0ae | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 193 | /*non-public*/ MethodHandle spreadInvoker(int leadingArgCount) { |
John R Rose | 025d0ae | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 194 | int spreadArgCount = targetType.parameterCount() - leadingArgCount; |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 195 | MethodType postSpreadType = targetType; |
| 196 | Class<?> argArrayType = impliedRestargType(postSpreadType, leadingArgCount); |
| 197 | if (postSpreadType.parameterSlotCount() <= MethodType.MAX_MH_INVOKER_ARITY) { |
| 198 | return genericInvoker().asSpreader(argArrayType, spreadArgCount); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 199 | } |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 200 | // Cannot build a generic invoker here of type ginvoker.invoke(mh, a*[254]). |
| 201 | // Instead, factor sinvoker.invoke(mh, a) into ainvoker.invoke(filter(mh), a) |
| 202 | // where filter(mh) == mh.asSpreader(Object[], spreadArgCount) |
| 203 | MethodType preSpreadType = postSpreadType |
| 204 | .replaceParameterTypes(leadingArgCount, postSpreadType.parameterCount(), argArrayType); |
| 205 | MethodHandle arrayInvoker = MethodHandles.invoker(preSpreadType); |
| 206 | MethodHandle makeSpreader = MethodHandles.insertArguments(Lazy.MH_asSpreader, 1, argArrayType, spreadArgCount); |
| 207 | return MethodHandles.filterArgument(arrayInvoker, 0, makeSpreader); |
John R Rose | 025d0ae | 2011-05-26 17:37:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 210 | private static Class<?> impliedRestargType(MethodType restargType, int fromPos) { |
| 211 | if (restargType.isGeneric()) return Object[].class; // can be nothing else |
| 212 | int maxPos = restargType.parameterCount(); |
| 213 | if (fromPos >= maxPos) return Object[].class; // reasonable default |
| 214 | Class<?> argType = restargType.parameterType(fromPos); |
| 215 | for (int i = fromPos+1; i < maxPos; i++) { |
| 216 | if (argType != restargType.parameterType(i)) |
| 217 | throw newIllegalArgumentException("need homogeneous rest arguments", restargType); |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 218 | } |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 219 | if (argType == Object.class) return Object[].class; |
| 220 | return Array.newInstance(argType, 0).getClass(); |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 223 | public String toString() { |
| 224 | return "Invokers"+targetType; |
| 225 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 226 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 227 | static MemberName methodHandleInvokeLinkerMethod(String name, |
| 228 | MethodType mtype, |
| 229 | Object[] appendixResult) { |
| 230 | int which; |
| 231 | switch (name) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 232 | case "invokeExact": which = MethodTypeForm.LF_EX_LINKER; break; |
| 233 | case "invoke": which = MethodTypeForm.LF_GEN_LINKER; break; |
| 234 | default: throw new InternalError("not invoker: "+name); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 235 | } |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 236 | LambdaForm lform; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 237 | if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) { |
| 238 | lform = invokeHandleForm(mtype, false, which); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 239 | appendixResult[0] = mtype; |
| 240 | } else { |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 241 | lform = invokeHandleForm(mtype, true, which); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 242 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 243 | return lform.vmentry; |
| 244 | } |
| 245 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 246 | // argument count to account for trailing "appendix value" (typically the mtype) |
| 247 | private static final int MH_LINKER_ARG_APPENDED = 1; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 248 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 249 | /** Returns an adapter for invokeExact or generic invoke, as a MH or constant pool linker. |
| 250 | * If !customized, caller is responsible for supplying, during adapter execution, |
| 251 | * a copy of the exact mtype. This is because the adapter might be generalized to |
| 252 | * a basic type. |
| 253 | * @param mtype the caller's method type (either basic or full-custom) |
| 254 | * @param customized whether to use a trailing appendix argument (to carry the mtype) |
| 255 | * @param which bit-encoded 0x01 whether it is a CP adapter ("linker") or MHs.invoker value ("invoker"); |
| 256 | * 0x02 whether it is for invokeExact or generic invoke |
| 257 | */ |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 258 | static LambdaForm invokeHandleForm(MethodType mtype, boolean customized, int which) { |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 259 | boolean isCached; |
| 260 | if (!customized) { |
| 261 | mtype = mtype.basicType(); // normalize Z to I, String to Object, etc. |
| 262 | isCached = true; |
| 263 | } else { |
| 264 | isCached = false; // maybe cache if mtype == mtype.basicType() |
| 265 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 266 | boolean isLinker, isGeneric; |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 267 | Kind kind; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 268 | switch (which) { |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 269 | case MethodTypeForm.LF_EX_LINKER: isLinker = true; isGeneric = false; kind = EXACT_LINKER; break; |
| 270 | case MethodTypeForm.LF_EX_INVOKER: isLinker = false; isGeneric = false; kind = EXACT_INVOKER; break; |
| 271 | case MethodTypeForm.LF_GEN_LINKER: isLinker = true; isGeneric = true; kind = GENERIC_LINKER; break; |
| 272 | case MethodTypeForm.LF_GEN_INVOKER: isLinker = false; isGeneric = true; kind = GENERIC_INVOKER; break; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 273 | default: throw new InternalError(); |
| 274 | } |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 275 | LambdaForm lform; |
| 276 | if (isCached) { |
| 277 | lform = mtype.form().cachedLambdaForm(which); |
| 278 | if (lform != null) return lform; |
| 279 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 280 | // exactInvokerForm (Object,Object)Object |
| 281 | // link with java.lang.invoke.MethodHandle.invokeBasic(MethodHandle,Object,Object)Object/invokeSpecial |
| 282 | final int THIS_MH = 0; |
| 283 | final int CALL_MH = THIS_MH + (isLinker ? 0 : 1); |
| 284 | final int ARG_BASE = CALL_MH + 1; |
| 285 | final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount(); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 286 | final int INARG_LIMIT = OUTARG_LIMIT + (isLinker && !customized ? 1 : 0); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 287 | int nameCursor = OUTARG_LIMIT; |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 288 | final int MTYPE_ARG = customized ? -1 : nameCursor++; // might be last in-argument |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 289 | final int CHECK_TYPE = nameCursor++; |
Vladimir Ivanov | bfc5193 | 2015-01-29 10:27:30 -0800 | [diff] [blame] | 290 | final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 291 | final int LINKER_CALL = nameCursor++; |
| 292 | MethodType invokerFormType = mtype.invokerType(); |
| 293 | if (isLinker) { |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 294 | if (!customized) |
| 295 | invokerFormType = invokerFormType.appendParameterTypes(MemberName.class); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 296 | } else { |
| 297 | invokerFormType = invokerFormType.invokerType(); |
| 298 | } |
| 299 | Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 300 | assert(names.length == nameCursor) |
| 301 | : Arrays.asList(mtype, customized, which, nameCursor, names.length); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 302 | if (MTYPE_ARG >= INARG_LIMIT) { |
| 303 | assert(names[MTYPE_ARG] == null); |
John Rose | afe2dd8 | 2014-09-10 19:19:51 +0400 | [diff] [blame] | 304 | BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L(); |
| 305 | names[THIS_MH] = names[THIS_MH].withConstraint(speciesData); |
| 306 | NamedFunction getter = speciesData.getterFunction(0); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 307 | names[MTYPE_ARG] = new Name(getter, names[THIS_MH]); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 308 | // else if isLinker, then MTYPE is passed in from the caller (e.g., the JVM) |
| 309 | } |
| 310 | |
| 311 | // Make the final call. If isGeneric, then prepend the result of type checking. |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 312 | MethodType outCallType = mtype.basicType(); |
| 313 | Object[] outArgs = Arrays.copyOfRange(names, CALL_MH, OUTARG_LIMIT, Object[].class); |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 314 | Object mtypeArg = (customized ? mtype : names[MTYPE_ARG]); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 315 | if (!isGeneric) { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 316 | names[CHECK_TYPE] = new Name(getFunction(NF_checkExactType), names[CALL_MH], mtypeArg); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 317 | // mh.invokeExact(a*):R => checkExactType(mh, TYPEOF(a*:R)); mh.invokeBasic(a*) |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 318 | } else { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 319 | names[CHECK_TYPE] = new Name(getFunction(NF_checkGenericType), names[CALL_MH], mtypeArg); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 320 | // mh.invokeGeneric(a*):R => checkGenericType(mh, TYPEOF(a*:R)).invokeBasic(a*) |
| 321 | outArgs[0] = names[CHECK_TYPE]; |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 322 | } |
Vladimir Ivanov | bfc5193 | 2015-01-29 10:27:30 -0800 | [diff] [blame] | 323 | if (CHECK_CUSTOM != -1) { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 324 | names[CHECK_CUSTOM] = new Name(getFunction(NF_checkCustomized), outArgs[0]); |
Vladimir Ivanov | bfc5193 | 2015-01-29 10:27:30 -0800 | [diff] [blame] | 325 | } |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 326 | names[LINKER_CALL] = new Name(outCallType, outArgs); |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 327 | if (customized) { |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 328 | lform = new LambdaForm(INARG_LIMIT, names); |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 329 | } else { |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 330 | lform = new LambdaForm(INARG_LIMIT, names, kind); |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 331 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 332 | if (isLinker) |
| 333 | lform.compileToBytecode(); // JVM needs a real methodOop |
John R Rose | 9d9d787 | 2012-08-17 13:42:25 -0700 | [diff] [blame] | 334 | if (isCached) |
| 335 | lform = mtype.form().setCachedLambdaForm(which, lform); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 336 | return lform; |
| 337 | } |
| 338 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 339 | |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 340 | static MemberName varHandleInvokeLinkerMethod(VarHandle.AccessMode ak, MethodType mtype) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 341 | LambdaForm lform; |
| 342 | if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) { |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 343 | lform = varHandleMethodGenericLinkerHandleForm(ak, mtype); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 344 | } else { |
| 345 | // TODO |
| 346 | throw newInternalError("Unsupported parameter slot count " + mtype.parameterSlotCount()); |
| 347 | } |
| 348 | return lform.vmentry; |
| 349 | } |
| 350 | |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 351 | private static LambdaForm varHandleMethodGenericLinkerHandleForm(VarHandle.AccessMode ak, |
| 352 | MethodType mtype) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 353 | // TODO Cache form? |
| 354 | |
| 355 | final int THIS_VH = 0; |
| 356 | final int ARG_BASE = THIS_VH + 1; |
| 357 | final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); |
| 358 | int nameCursor = ARG_LIMIT; |
| 359 | final int VAD_ARG = nameCursor++; |
| 360 | final int CHECK_TYPE = nameCursor++; |
| 361 | final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1; |
| 362 | final int LINKER_CALL = nameCursor++; |
| 363 | |
| 364 | Name[] names = new Name[LINKER_CALL + 1]; |
| 365 | names[THIS_VH] = argument(THIS_VH, BasicType.basicType(Object.class)); |
| 366 | for (int i = 0; i < mtype.parameterCount(); i++) { |
| 367 | names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i))); |
| 368 | } |
| 369 | names[VAD_ARG] = new Name(ARG_LIMIT, BasicType.basicType(Object.class)); |
| 370 | |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 371 | names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleGenericType), names[THIS_VH], names[VAD_ARG]); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 372 | |
| 373 | Object[] outArgs = new Object[ARG_LIMIT + 1]; |
| 374 | outArgs[0] = names[CHECK_TYPE]; |
| 375 | for (int i = 0; i < ARG_LIMIT; i++) { |
| 376 | outArgs[i + 1] = names[i]; |
| 377 | } |
| 378 | |
| 379 | if (CHECK_CUSTOM != -1) { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 380 | names[CHECK_CUSTOM] = new Name(getFunction(NF_checkCustomized), outArgs[0]); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class) |
| 384 | .basicType(); |
| 385 | names[LINKER_CALL] = new Name(outCallType, outArgs); |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 386 | LambdaForm lform = new LambdaForm(ARG_LIMIT + 1, names, VARHANDLE_LINKER); |
| 387 | if (LambdaForm.debugNames()) { |
| 388 | String name = ak.methodName() + ":VarHandle_invoke_MT_" + |
| 389 | shortenSignature(basicTypeSignature(mtype)); |
| 390 | LambdaForm.associateWithDebugName(lform, name); |
| 391 | } |
Paul Sandoz | 3744518 | 2016-05-05 11:39:08 -0700 | [diff] [blame] | 392 | lform.compileToBytecode(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 393 | return lform; |
| 394 | } |
| 395 | |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 396 | private static LambdaForm varHandleMethodInvokerHandleForm(VarHandle.AccessMode ak, |
| 397 | MethodType mtype, boolean isExact) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 398 | // TODO Cache form? |
| 399 | |
| 400 | final int THIS_MH = 0; |
| 401 | final int CALL_VH = THIS_MH + 1; |
| 402 | final int ARG_BASE = CALL_VH + 1; |
| 403 | final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); |
| 404 | int nameCursor = ARG_LIMIT; |
| 405 | final int VAD_ARG = nameCursor++; |
| 406 | final int CHECK_TYPE = nameCursor++; |
| 407 | final int LINKER_CALL = nameCursor++; |
| 408 | |
| 409 | Name[] names = new Name[LINKER_CALL + 1]; |
| 410 | names[THIS_MH] = argument(THIS_MH, BasicType.basicType(Object.class)); |
| 411 | names[CALL_VH] = argument(CALL_VH, BasicType.basicType(Object.class)); |
| 412 | for (int i = 0; i < mtype.parameterCount(); i++) { |
| 413 | names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i))); |
| 414 | } |
| 415 | |
| 416 | BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L(); |
| 417 | names[THIS_MH] = names[THIS_MH].withConstraint(speciesData); |
| 418 | |
| 419 | NamedFunction getter = speciesData.getterFunction(0); |
| 420 | names[VAD_ARG] = new Name(getter, names[THIS_MH]); |
| 421 | |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 422 | if (isExact) { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 423 | names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleExactType), names[CALL_VH], names[VAD_ARG]); |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 424 | } else { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 425 | names[CHECK_TYPE] = new Name(getFunction(NF_checkVarHandleGenericType), names[CALL_VH], names[VAD_ARG]); |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 426 | } |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 427 | Object[] outArgs = new Object[ARG_LIMIT]; |
| 428 | outArgs[0] = names[CHECK_TYPE]; |
| 429 | for (int i = 1; i < ARG_LIMIT; i++) { |
| 430 | outArgs[i] = names[i]; |
| 431 | } |
| 432 | |
| 433 | MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class) |
| 434 | .basicType(); |
| 435 | names[LINKER_CALL] = new Name(outCallType, outArgs); |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 436 | Kind kind = isExact ? VARHANDLE_EXACT_INVOKER : VARHANDLE_INVOKER; |
| 437 | LambdaForm lform = new LambdaForm(ARG_LIMIT, names, kind); |
| 438 | if (LambdaForm.debugNames()) { |
| 439 | String name = ak.methodName() + |
| 440 | (isExact ? ":VarHandle_exactInvoker_" : ":VarHandle_invoker_") + |
| 441 | shortenSignature(basicTypeSignature(mtype)); |
| 442 | LambdaForm.associateWithDebugName(lform, name); |
| 443 | } |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 444 | lform.prepare(); |
| 445 | return lform; |
| 446 | } |
| 447 | |
| 448 | /*non-public*/ static |
| 449 | @ForceInline |
Paul Sandoz | 3744518 | 2016-05-05 11:39:08 -0700 | [diff] [blame] | 450 | MethodHandle checkVarHandleGenericType(VarHandle handle, VarHandle.AccessDescriptor ad) { |
| 451 | // Test for exact match on invoker types |
| 452 | // TODO match with erased types and add cast of return value to lambda form |
| 453 | MethodHandle mh = handle.getMethodHandle(ad.mode); |
| 454 | if (mh.type() == ad.symbolicMethodTypeInvoker) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 455 | return mh; |
| 456 | } |
| 457 | else { |
Paul Sandoz | 3744518 | 2016-05-05 11:39:08 -0700 | [diff] [blame] | 458 | return mh.asType(ad.symbolicMethodTypeInvoker); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | /*non-public*/ static |
| 463 | @ForceInline |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 464 | MethodHandle checkVarHandleExactType(VarHandle handle, VarHandle.AccessDescriptor ad) { |
| 465 | MethodHandle mh = handle.getMethodHandle(ad.mode); |
| 466 | MethodType mt = mh.type(); |
| 467 | if (mt != ad.symbolicMethodTypeInvoker) { |
| 468 | throw newWrongMethodTypeException(mt, ad.symbolicMethodTypeInvoker); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 469 | } |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 470 | return mh; |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 473 | /*non-public*/ static |
| 474 | WrongMethodTypeException newWrongMethodTypeException(MethodType actual, MethodType expected) { |
| 475 | // FIXME: merge with JVM logic for throwing WMTE |
| 476 | return new WrongMethodTypeException("expected "+expected+" but found "+actual); |
| 477 | } |
| 478 | |
| 479 | /** Static definition of MethodHandle.invokeExact checking code. */ |
| 480 | /*non-public*/ static |
| 481 | @ForceInline |
Shilpi Rastogi | ff97659 | 2016-02-10 11:04:13 +0100 | [diff] [blame] | 482 | void checkExactType(MethodHandle mh, MethodType expected) { |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 483 | MethodType actual = mh.type(); |
| 484 | if (actual != expected) |
| 485 | throw newWrongMethodTypeException(expected, actual); |
| 486 | } |
| 487 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 488 | /** Static definition of MethodHandle.invokeGeneric checking code. |
| 489 | * Directly returns the type-adjusted MH to invoke, as follows: |
| 490 | * {@code (R)MH.invoke(a*) => MH.asType(TYPEOF(a*:R)).invokeBasic(a*)} |
| 491 | */ |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 492 | /*non-public*/ static |
| 493 | @ForceInline |
Shilpi Rastogi | ff97659 | 2016-02-10 11:04:13 +0100 | [diff] [blame] | 494 | MethodHandle checkGenericType(MethodHandle mh, MethodType expected) { |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 495 | return mh.asType(expected); |
| 496 | /* Maybe add more paths here. Possible optimizations: |
| 497 | * for (R)MH.invoke(a*), |
| 498 | * let MT0 = TYPEOF(a*:R), MT1 = MH.type |
| 499 | * |
| 500 | * if MT0==MT1 or MT1 can be safely called by MT0 |
| 501 | * => MH.invokeBasic(a*) |
| 502 | * if MT1 can be safely called by MT0[R := Object] |
| 503 | * => MH.invokeBasic(a*) & checkcast(R) |
| 504 | * if MT1 can be safely called by MT0[* := Object] |
| 505 | * => checkcast(A)* & MH.invokeBasic(a*) & checkcast(R) |
| 506 | * if a big adapter BA can be pulled out of (MT0,MT1) |
| 507 | * => BA.invokeBasic(MT0,MH,a*) |
| 508 | * if a local adapter LA can cached on static CS0 = new GICS(MT0) |
| 509 | * => CS0.LA.invokeBasic(MH,a*) |
| 510 | * else |
| 511 | * => MH.asType(MT0).invokeBasic(A*) |
| 512 | */ |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static MemberName linkToCallSiteMethod(MethodType mtype) { |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 516 | LambdaForm lform = callSiteForm(mtype, false); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 517 | return lform.vmentry; |
| 518 | } |
| 519 | |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 520 | static MemberName linkToTargetMethod(MethodType mtype) { |
| 521 | LambdaForm lform = callSiteForm(mtype, true); |
| 522 | return lform.vmentry; |
| 523 | } |
| 524 | |
| 525 | // skipCallSite is true if we are optimizing a ConstantCallSite |
| 526 | private static LambdaForm callSiteForm(MethodType mtype, boolean skipCallSite) { |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 527 | mtype = mtype.basicType(); // normalize Z to I, String to Object, etc. |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 528 | final int which = (skipCallSite ? MethodTypeForm.LF_MH_LINKER : MethodTypeForm.LF_CS_LINKER); |
| 529 | LambdaForm lform = mtype.form().cachedLambdaForm(which); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 530 | if (lform != null) return lform; |
| 531 | // exactInvokerForm (Object,Object)Object |
| 532 | // link with java.lang.invoke.MethodHandle.invokeBasic(MethodHandle,Object,Object)Object/invokeSpecial |
| 533 | final int ARG_BASE = 0; |
| 534 | final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount(); |
| 535 | final int INARG_LIMIT = OUTARG_LIMIT + 1; |
| 536 | int nameCursor = OUTARG_LIMIT; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 537 | final int APPENDIX_ARG = nameCursor++; // the last in-argument |
| 538 | final int CSITE_ARG = skipCallSite ? -1 : APPENDIX_ARG; |
| 539 | final int CALL_MH = skipCallSite ? APPENDIX_ARG : nameCursor++; // result of getTarget |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 540 | final int LINKER_CALL = nameCursor++; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 541 | MethodType invokerFormType = mtype.appendParameterTypes(skipCallSite ? MethodHandle.class : CallSite.class); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 542 | Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType); |
| 543 | assert(names.length == nameCursor); |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 544 | assert(names[APPENDIX_ARG] != null); |
| 545 | if (!skipCallSite) |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 546 | names[CALL_MH] = new Name(getFunction(NF_getCallSiteTarget), names[CSITE_ARG]); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 547 | // (site.)invokedynamic(a*):R => mh = site.getTarget(); mh.invokeBasic(a*) |
| 548 | final int PREPEND_MH = 0, PREPEND_COUNT = 1; |
| 549 | Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, OUTARG_LIMIT + PREPEND_COUNT, Object[].class); |
| 550 | // prepend MH argument: |
| 551 | System.arraycopy(outArgs, 0, outArgs, PREPEND_COUNT, outArgs.length - PREPEND_COUNT); |
| 552 | outArgs[PREPEND_MH] = names[CALL_MH]; |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 553 | names[LINKER_CALL] = new Name(mtype, outArgs); |
Claes Redestad | 8d5f5b9 | 2017-02-22 11:03:33 +0100 | [diff] [blame] | 554 | lform = new LambdaForm(INARG_LIMIT, names, |
| 555 | (skipCallSite ? LINK_TO_TARGET_METHOD : LINK_TO_CALL_SITE)); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 556 | lform.compileToBytecode(); // JVM needs a real methodOop |
John R Rose | 62ee211 | 2013-10-05 05:30:38 -0700 | [diff] [blame] | 557 | lform = mtype.form().setCachedLambdaForm(which, lform); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 558 | return lform; |
| 559 | } |
| 560 | |
| 561 | /** Static definition of MethodHandle.invokeGeneric checking code. */ |
| 562 | /*non-public*/ static |
| 563 | @ForceInline |
Shilpi Rastogi | ff97659 | 2016-02-10 11:04:13 +0100 | [diff] [blame] | 564 | MethodHandle getCallSiteTarget(CallSite site) { |
| 565 | return site.getTarget(); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Vladimir Ivanov | bfc5193 | 2015-01-29 10:27:30 -0800 | [diff] [blame] | 568 | /*non-public*/ static |
| 569 | @ForceInline |
Shilpi Rastogi | ff97659 | 2016-02-10 11:04:13 +0100 | [diff] [blame] | 570 | void checkCustomized(MethodHandle mh) { |
Vladimir Ivanov | 71020f9 | 2015-03-20 11:42:31 -0700 | [diff] [blame] | 571 | if (MethodHandleImpl.isCompileConstant(mh)) return; |
Vladimir Ivanov | bfc5193 | 2015-01-29 10:27:30 -0800 | [diff] [blame] | 572 | if (mh.form.customized == null) { |
| 573 | maybeCustomize(mh); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /*non-public*/ static |
| 578 | @DontInline |
| 579 | void maybeCustomize(MethodHandle mh) { |
| 580 | byte count = mh.customizationCount; |
| 581 | if (count >= CUSTOMIZE_THRESHOLD) { |
| 582 | mh.customize(); |
| 583 | } else { |
| 584 | mh.customizationCount = (byte)(count+1); |
| 585 | } |
| 586 | } |
| 587 | |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 588 | // Local constant functions: |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 589 | private static final byte NF_checkExactType = 0, |
| 590 | NF_checkGenericType = 1, |
| 591 | NF_getCallSiteTarget = 2, |
| 592 | NF_checkCustomized = 3, |
| 593 | NF_checkVarHandleGenericType = 4, |
| 594 | NF_checkVarHandleExactType = 5, |
| 595 | NF_LIMIT = 6; |
| 596 | |
| 597 | private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT]; |
| 598 | |
| 599 | private static NamedFunction getFunction(byte func) { |
| 600 | NamedFunction nf = NFS[func]; |
| 601 | if (nf != null) { |
| 602 | return nf; |
| 603 | } |
| 604 | NFS[func] = nf = createFunction(func); |
| 605 | // Each nf must be statically invocable or we get tied up in our bootstraps. |
| 606 | assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf)); |
| 607 | return nf; |
| 608 | } |
| 609 | |
| 610 | private static NamedFunction createFunction(byte func) { |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 611 | try { |
Claes Redestad | 9ab899d | 2017-04-11 11:24:12 +0200 | [diff] [blame] | 612 | switch (func) { |
| 613 | case NF_checkExactType: |
| 614 | return new NamedFunction(Invokers.class |
| 615 | .getDeclaredMethod("checkExactType", MethodHandle.class, MethodType.class)); |
| 616 | case NF_checkGenericType: |
| 617 | return new NamedFunction(Invokers.class |
| 618 | .getDeclaredMethod("checkGenericType", MethodHandle.class, MethodType.class)); |
| 619 | case NF_getCallSiteTarget: |
| 620 | return new NamedFunction(Invokers.class |
| 621 | .getDeclaredMethod("getCallSiteTarget", CallSite.class)); |
| 622 | case NF_checkCustomized: |
| 623 | return new NamedFunction(Invokers.class |
| 624 | .getDeclaredMethod("checkCustomized", MethodHandle.class)); |
| 625 | case NF_checkVarHandleGenericType: |
| 626 | return new NamedFunction(Invokers.class |
| 627 | .getDeclaredMethod("checkVarHandleGenericType", VarHandle.class, VarHandle.AccessDescriptor.class)); |
| 628 | case NF_checkVarHandleExactType: |
| 629 | return new NamedFunction(Invokers.class |
| 630 | .getDeclaredMethod("checkVarHandleExactType", VarHandle.class, VarHandle.AccessDescriptor.class)); |
| 631 | default: |
| 632 | throw newInternalError("Unknown function: " + func); |
| 633 | } |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 634 | } catch (ReflectiveOperationException ex) { |
Christian Thalinger | 01d0ba6 | 2012-10-19 17:04:35 -0700 | [diff] [blame] | 635 | throw newInternalError(ex); |
Christian Thalinger | 360d512 | 2012-07-24 10:47:44 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
John Rose | 6a177f4 | 2014-09-10 19:19:48 +0400 | [diff] [blame] | 639 | private static class Lazy { |
| 640 | private static final MethodHandle MH_asSpreader; |
| 641 | |
| 642 | static { |
| 643 | try { |
| 644 | MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader", |
| 645 | MethodType.methodType(MethodHandle.class, Class.class, int.class)); |
| 646 | } catch (ReflectiveOperationException ex) { |
| 647 | throw newInternalError(ex); |
| 648 | } |
| 649 | } |
| 650 | } |
Claes Redestad | 60754ca | 2016-08-24 16:11:21 +0200 | [diff] [blame] | 651 | |
| 652 | static { |
| 653 | // The Holder class will contain pre-generated Invokers resolved |
| 654 | // speculatively using MemberName.getFactory().resolveOrNull. However, that |
| 655 | // doesn't initialize the class, which subtly breaks inlining etc. By forcing |
| 656 | // initialization of the Holder class we avoid these issues. |
| 657 | UNSAFE.ensureClassInitialized(Holder.class); |
| 658 | } |
| 659 | |
| 660 | /* Placeholder class for Invokers generated ahead of time */ |
| 661 | final class Holder {} |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 662 | } |