blob: 0a97622fe8460a99704b715a8b46a656a0d133af [file] [log] [blame]
John R Rosefb6164c2009-05-05 22:40:09 -07001/*
Shilpi Rastogiff976592016-02-10 11:04:13 +01002 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
John R Rosefb6164c2009-05-05 22:40:09 -07003 * 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'Hairfe008ae2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
John R Rosefb6164c2009-05-05 22:40:09 -07008 * particular file as subject to the "Classpath" exception as provided
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
John R Rosefb6164c2009-05-05 22:40:09 -070010 *
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'Hairfe008ae2010-05-25 15:58:33 -070021 * 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 Rosefb6164c2009-05-05 22:40:09 -070024 */
25
John R Rose37188242011-03-23 23:02:31 -070026package java.lang.invoke;
John R Rosefb6164c2009-05-05 22:40:09 -070027
Paul Sandozc464a9d2015-12-03 11:17:31 +010028import jdk.internal.vm.annotation.DontInline;
29import jdk.internal.vm.annotation.ForceInline;
30import jdk.internal.vm.annotation.Stable;
31
John Rose6a177f42014-09-10 19:19:48 +040032import java.lang.reflect.Array;
Christian Thalinger360d5122012-07-24 10:47:44 -070033import java.util.Arrays;
John Rose6a177f42014-09-10 19:19:48 +040034
Christian Thalinger01d0ba62012-10-19 17:04:35 -070035import static java.lang.invoke.MethodHandleStatics.*;
Christian Thalinger360d5122012-07-24 10:47:44 -070036import static java.lang.invoke.MethodHandleNatives.Constants.*;
John R Rose37188242011-03-23 23:02:31 -070037import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
Christian Thalinger360d5122012-07-24 10:47:44 -070038import static java.lang.invoke.LambdaForm.*;
Claes Redestad60754ca2016-08-24 16:11:21 +020039import static java.lang.invoke.LambdaForm.Kind.*;
John R Rosefb6164c2009-05-05 22:40:09 -070040
41/**
42 * Construction and caching of often-used invokers.
43 * @author jrose
44 */
John R Rosee7ebd472011-03-18 00:03:24 -070045class Invokers {
Pavel Rappo7075a992014-08-11 21:03:59 +010046 // exact type (sans leading target MH) for the outgoing call
John R Rosefb6164c2009-05-05 22:40:09 -070047 private final MethodType targetType;
48
John Rose6a177f42014-09-10 19:19:48 +040049 // 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 Rose4f508ab2010-10-30 21:08:23 -070057
John R Rosefb6164c2009-05-05 22:40:09 -070058 /** 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 Roseeedbeda2011-02-11 01:26:24 -080061 /*non-public*/ Invokers(MethodType targetType) {
John R Rosefb6164c2009-05-05 22:40:09 -070062 this.targetType = targetType;
63 }
64
John R Rosee7ebd472011-03-18 00:03:24 -070065 /*non-public*/ MethodHandle exactInvoker() {
John Rose6a177f42014-09-10 19:19:48 +040066 MethodHandle invoker = cachedInvoker(INV_EXACT);
John R Rosefb6164c2009-05-05 22:40:09 -070067 if (invoker != null) return invoker;
John R Rose62ee2112013-10-05 05:30:38 -070068 invoker = makeExactOrGeneralInvoker(true);
John Rose6a177f42014-09-10 19:19:48 +040069 return setCachedInvoker(INV_EXACT, invoker);
John R Rosefb6164c2009-05-05 22:40:09 -070070 }
71
John Rose6a177f42014-09-10 19:19:48 +040072 /*non-public*/ MethodHandle genericInvoker() {
73 MethodHandle invoker = cachedInvoker(INV_GENERIC);
John R Rosefb6164c2009-05-05 22:40:09 -070074 if (invoker != null) return invoker;
John R Rose62ee2112013-10-05 05:30:38 -070075 invoker = makeExactOrGeneralInvoker(false);
John Rose6a177f42014-09-10 19:19:48 +040076 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 Sandoz9fb30a32016-03-24 11:21:21 +010097 /*non-public*/ MethodHandle varHandleMethodInvoker(VarHandle.AccessMode ak) {
98 // TODO cache invoker
Paul Sandoz44afe202016-05-17 12:06:41 +020099 return makeVarHandleMethodInvoker(ak, false);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100100 }
101
102 /*non-public*/ MethodHandle varHandleMethodExactInvoker(VarHandle.AccessMode ak) {
103 // TODO cache invoker
Paul Sandoz44afe202016-05-17 12:06:41 +0200104 return makeVarHandleMethodInvoker(ak, true);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100105 }
106
John Rose6a177f42014-09-10 19:19:48 +0400107 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 Rosefb6164c2009-05-05 22:40:09 -0700116 }
117
John R Rose62ee2112013-10-05 05:30:38 -0700118 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 Rosefb7b2ac2014-09-10 19:19:49 +0400125 invoker = invoker.withInternalMemberName(MemberName.makeMethodHandleInvoke(whichName, mtype), false);
Christian Thalinger360d5122012-07-24 10:47:44 -0700126 assert(checkInvoker(invoker));
John R Rose62ee2112013-10-05 05:30:38 -0700127 maybeCompileToBytecode(invoker);
John R Rose025d0ae2011-05-26 17:37:36 -0700128 return invoker;
129 }
130
Paul Sandoz44afe202016-05-17 12:06:41 +0200131 private MethodHandle makeVarHandleMethodInvoker(VarHandle.AccessMode ak, boolean isExact) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100132 MethodType mtype = targetType;
133 MethodType invokerType = mtype.insertParameterTypes(0, VarHandle.class);
134
Paul Sandoz44afe202016-05-17 12:06:41 +0200135 LambdaForm lform = varHandleMethodInvokerHandleForm(ak.methodName(), mtype, isExact);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100136 VarHandle.AccessDescriptor ad = new VarHandle.AccessDescriptor(mtype, ak.at.ordinal(), ak.ordinal());
137 MethodHandle invoker = BoundMethodHandle.bindSingle(invokerType, lform, ad);
138
Paul Sandoza7aff442016-04-13 15:05:48 +0200139 invoker = invoker.withInternalMemberName(MemberName.makeVarHandleMethodInvoke(ak.methodName(), mtype), false);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100140 assert(checkVarHandleInvoker(invoker));
141
142 maybeCompileToBytecode(invoker);
143 return invoker;
144 }
145
John R Rose62ee2112013-10-05 05:30:38 -0700146 /** 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 Rose62ee2112013-10-05 05:30:38 -0700155 // This next one is called from LambdaForm.NamedFunction.<init>.
156 /*non-public*/ static MemberName invokeBasicMethod(MethodType basicType) {
157 assert(basicType == basicType.basicType());
Christian Thalinger360d5122012-07-24 10:47:44 -0700158 try {
159 //Lookup.findVirtual(MethodHandle.class, name, type);
John R Rose62ee2112013-10-05 05:30:38 -0700160 return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType);
Christian Thalinger360d5122012-07-24 10:47:44 -0700161 } catch (ReflectiveOperationException ex) {
John R Rose62ee2112013-10-05 05:30:38 -0700162 throw newInternalError("JVM cannot find invoker for "+basicType, ex);
Christian Thalinger360d5122012-07-24 10:47:44 -0700163 }
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 Sandoz9fb30a32016-03-24 11:21:21 +0100175 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 Rose6a177f42014-09-10 19:19:48 +0400185 /**
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 Rose025d0ae2011-05-26 17:37:36 -0700193 /*non-public*/ MethodHandle spreadInvoker(int leadingArgCount) {
John R Rose025d0ae2011-05-26 17:37:36 -0700194 int spreadArgCount = targetType.parameterCount() - leadingArgCount;
John Rose6a177f42014-09-10 19:19:48 +0400195 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 Rose9d9d7872012-08-17 13:42:25 -0700199 }
John Rose6a177f42014-09-10 19:19:48 +0400200 // 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 Rose025d0ae2011-05-26 17:37:36 -0700208 }
209
John Rose6a177f42014-09-10 19:19:48 +0400210 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 Rose4f508ab2010-10-30 21:08:23 -0700218 }
John Rose6a177f42014-09-10 19:19:48 +0400219 if (argType == Object.class) return Object[].class;
220 return Array.newInstance(argType, 0).getClass();
John R Rose4f508ab2010-10-30 21:08:23 -0700221 }
222
John R Rosefb6164c2009-05-05 22:40:09 -0700223 public String toString() {
224 return "Invokers"+targetType;
225 }
Christian Thalinger360d5122012-07-24 10:47:44 -0700226
John R Rose62ee2112013-10-05 05:30:38 -0700227 static MemberName methodHandleInvokeLinkerMethod(String name,
228 MethodType mtype,
229 Object[] appendixResult) {
230 int which;
231 switch (name) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100232 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 Rose62ee2112013-10-05 05:30:38 -0700235 }
John R Rose9d9d7872012-08-17 13:42:25 -0700236 LambdaForm lform;
John R Rose62ee2112013-10-05 05:30:38 -0700237 if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
238 lform = invokeHandleForm(mtype, false, which);
John R Rose9d9d7872012-08-17 13:42:25 -0700239 appendixResult[0] = mtype;
240 } else {
John R Rose62ee2112013-10-05 05:30:38 -0700241 lform = invokeHandleForm(mtype, true, which);
John R Rose9d9d7872012-08-17 13:42:25 -0700242 }
Christian Thalinger360d5122012-07-24 10:47:44 -0700243 return lform.vmentry;
244 }
245
John R Rose62ee2112013-10-05 05:30:38 -0700246 // argument count to account for trailing "appendix value" (typically the mtype)
247 private static final int MH_LINKER_ARG_APPENDED = 1;
Christian Thalinger360d5122012-07-24 10:47:44 -0700248
John R Rose62ee2112013-10-05 05:30:38 -0700249 /** 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 Redestad60754ca2016-08-24 16:11:21 +0200258 static LambdaForm invokeHandleForm(MethodType mtype, boolean customized, int which) {
John R Rose9d9d7872012-08-17 13:42:25 -0700259 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 Thalinger360d5122012-07-24 10:47:44 -0700266 boolean isLinker, isGeneric;
Claes Redestad60754ca2016-08-24 16:11:21 +0200267 Kind kind;
Christian Thalinger360d5122012-07-24 10:47:44 -0700268 switch (which) {
Claes Redestad60754ca2016-08-24 16:11:21 +0200269 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 Thalinger360d5122012-07-24 10:47:44 -0700273 default: throw new InternalError();
274 }
John R Rose9d9d7872012-08-17 13:42:25 -0700275 LambdaForm lform;
276 if (isCached) {
277 lform = mtype.form().cachedLambdaForm(which);
278 if (lform != null) return lform;
279 }
Christian Thalinger360d5122012-07-24 10:47:44 -0700280 // 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 Rose9d9d7872012-08-17 13:42:25 -0700286 final int INARG_LIMIT = OUTARG_LIMIT + (isLinker && !customized ? 1 : 0);
Christian Thalinger360d5122012-07-24 10:47:44 -0700287 int nameCursor = OUTARG_LIMIT;
John R Rose9d9d7872012-08-17 13:42:25 -0700288 final int MTYPE_ARG = customized ? -1 : nameCursor++; // might be last in-argument
Christian Thalinger360d5122012-07-24 10:47:44 -0700289 final int CHECK_TYPE = nameCursor++;
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800290 final int CHECK_CUSTOM = (CUSTOMIZE_THRESHOLD >= 0) ? nameCursor++ : -1;
Christian Thalinger360d5122012-07-24 10:47:44 -0700291 final int LINKER_CALL = nameCursor++;
292 MethodType invokerFormType = mtype.invokerType();
293 if (isLinker) {
John R Rose9d9d7872012-08-17 13:42:25 -0700294 if (!customized)
295 invokerFormType = invokerFormType.appendParameterTypes(MemberName.class);
Christian Thalinger360d5122012-07-24 10:47:44 -0700296 } else {
297 invokerFormType = invokerFormType.invokerType();
298 }
299 Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
John R Rose9d9d7872012-08-17 13:42:25 -0700300 assert(names.length == nameCursor)
301 : Arrays.asList(mtype, customized, which, nameCursor, names.length);
Christian Thalinger360d5122012-07-24 10:47:44 -0700302 if (MTYPE_ARG >= INARG_LIMIT) {
303 assert(names[MTYPE_ARG] == null);
John Roseafe2dd82014-09-10 19:19:51 +0400304 BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
305 names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
306 NamedFunction getter = speciesData.getterFunction(0);
John R Rose62ee2112013-10-05 05:30:38 -0700307 names[MTYPE_ARG] = new Name(getter, names[THIS_MH]);
Christian Thalinger360d5122012-07-24 10:47:44 -0700308 // 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 Rose62ee2112013-10-05 05:30:38 -0700312 MethodType outCallType = mtype.basicType();
313 Object[] outArgs = Arrays.copyOfRange(names, CALL_MH, OUTARG_LIMIT, Object[].class);
John R Rose9d9d7872012-08-17 13:42:25 -0700314 Object mtypeArg = (customized ? mtype : names[MTYPE_ARG]);
Christian Thalinger360d5122012-07-24 10:47:44 -0700315 if (!isGeneric) {
John R Rose9d9d7872012-08-17 13:42:25 -0700316 names[CHECK_TYPE] = new Name(NF_checkExactType, names[CALL_MH], mtypeArg);
Christian Thalinger360d5122012-07-24 10:47:44 -0700317 // mh.invokeExact(a*):R => checkExactType(mh, TYPEOF(a*:R)); mh.invokeBasic(a*)
Christian Thalinger360d5122012-07-24 10:47:44 -0700318 } else {
John R Rose9d9d7872012-08-17 13:42:25 -0700319 names[CHECK_TYPE] = new Name(NF_checkGenericType, names[CALL_MH], mtypeArg);
John R Rose62ee2112013-10-05 05:30:38 -0700320 // mh.invokeGeneric(a*):R => checkGenericType(mh, TYPEOF(a*:R)).invokeBasic(a*)
321 outArgs[0] = names[CHECK_TYPE];
Christian Thalinger360d5122012-07-24 10:47:44 -0700322 }
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800323 if (CHECK_CUSTOM != -1) {
Vladimir Ivanovad2c8372015-04-21 21:06:06 +0300324 names[CHECK_CUSTOM] = new Name(NF_checkCustomized, outArgs[0]);
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800325 }
John R Rose62ee2112013-10-05 05:30:38 -0700326 names[LINKER_CALL] = new Name(outCallType, outArgs);
Claes Redestad60754ca2016-08-24 16:11:21 +0200327 if (customized) {
328 lform = new LambdaForm(kind.defaultLambdaName, INARG_LIMIT, names);
329 } else {
330 lform = new LambdaForm(kind.defaultLambdaName, INARG_LIMIT, names, kind);
331 }
Christian Thalinger360d5122012-07-24 10:47:44 -0700332 if (isLinker)
333 lform.compileToBytecode(); // JVM needs a real methodOop
John R Rose9d9d7872012-08-17 13:42:25 -0700334 if (isCached)
335 lform = mtype.form().setCachedLambdaForm(which, lform);
Christian Thalinger360d5122012-07-24 10:47:44 -0700336 return lform;
337 }
338
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100339
340 static MemberName varHandleInvokeLinkerMethod(String name,
341 MethodType mtype) {
342 LambdaForm lform;
343 if (mtype.parameterSlotCount() <= MethodType.MAX_MH_ARITY - MH_LINKER_ARG_APPENDED) {
344 lform = varHandleMethodGenericLinkerHandleForm(name, mtype);
345 } else {
346 // TODO
347 throw newInternalError("Unsupported parameter slot count " + mtype.parameterSlotCount());
348 }
349 return lform.vmentry;
350 }
351
352 private static LambdaForm varHandleMethodGenericLinkerHandleForm(String name, MethodType mtype) {
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
371 names[CHECK_TYPE] = new Name(NF_checkVarHandleGenericType, names[THIS_VH], names[VAD_ARG]);
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) {
380 names[CHECK_CUSTOM] = new Name(NF_checkCustomized, outArgs[0]);
381 }
382
383 MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
384 .basicType();
385 names[LINKER_CALL] = new Name(outCallType, outArgs);
386 LambdaForm lform = new LambdaForm(name + ":VarHandle_invoke_MT_" + shortenSignature(basicTypeSignature(mtype)),
387 ARG_LIMIT + 1, names);
388
Paul Sandoz37445182016-05-05 11:39:08 -0700389 lform.compileToBytecode();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100390 return lform;
391 }
392
Paul Sandoz44afe202016-05-17 12:06:41 +0200393 private static LambdaForm varHandleMethodInvokerHandleForm(String name, MethodType mtype, boolean isExact) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100394 // TODO Cache form?
395
396 final int THIS_MH = 0;
397 final int CALL_VH = THIS_MH + 1;
398 final int ARG_BASE = CALL_VH + 1;
399 final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
400 int nameCursor = ARG_LIMIT;
401 final int VAD_ARG = nameCursor++;
402 final int CHECK_TYPE = nameCursor++;
403 final int LINKER_CALL = nameCursor++;
404
405 Name[] names = new Name[LINKER_CALL + 1];
406 names[THIS_MH] = argument(THIS_MH, BasicType.basicType(Object.class));
407 names[CALL_VH] = argument(CALL_VH, BasicType.basicType(Object.class));
408 for (int i = 0; i < mtype.parameterCount(); i++) {
409 names[ARG_BASE + i] = argument(ARG_BASE + i, BasicType.basicType(mtype.parameterType(i)));
410 }
411
412 BoundMethodHandle.SpeciesData speciesData = BoundMethodHandle.speciesData_L();
413 names[THIS_MH] = names[THIS_MH].withConstraint(speciesData);
414
415 NamedFunction getter = speciesData.getterFunction(0);
416 names[VAD_ARG] = new Name(getter, names[THIS_MH]);
417
Paul Sandoz44afe202016-05-17 12:06:41 +0200418 if (isExact) {
419 names[CHECK_TYPE] = new Name(NF_checkVarHandleExactType, names[CALL_VH], names[VAD_ARG]);
420 } else {
421 names[CHECK_TYPE] = new Name(NF_checkVarHandleGenericType, names[CALL_VH], names[VAD_ARG]);
422 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100423 Object[] outArgs = new Object[ARG_LIMIT];
424 outArgs[0] = names[CHECK_TYPE];
425 for (int i = 1; i < ARG_LIMIT; i++) {
426 outArgs[i] = names[i];
427 }
428
429 MethodType outCallType = mtype.insertParameterTypes(0, VarHandle.class)
430 .basicType();
431 names[LINKER_CALL] = new Name(outCallType, outArgs);
Paul Sandoz44afe202016-05-17 12:06:41 +0200432 String debugName = isExact ? ":VarHandle_exactInvoker" : ":VarHandle_invoker";
433 LambdaForm lform = new LambdaForm(name + debugName + shortenSignature(basicTypeSignature(mtype)),
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100434 ARG_LIMIT, names);
435
436 lform.prepare();
437 return lform;
438 }
439
440 /*non-public*/ static
441 @ForceInline
Paul Sandoz37445182016-05-05 11:39:08 -0700442 MethodHandle checkVarHandleGenericType(VarHandle handle, VarHandle.AccessDescriptor ad) {
443 // Test for exact match on invoker types
444 // TODO match with erased types and add cast of return value to lambda form
445 MethodHandle mh = handle.getMethodHandle(ad.mode);
446 if (mh.type() == ad.symbolicMethodTypeInvoker) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100447 return mh;
448 }
449 else {
Paul Sandoz37445182016-05-05 11:39:08 -0700450 return mh.asType(ad.symbolicMethodTypeInvoker);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100451 }
452 }
453
454 /*non-public*/ static
455 @ForceInline
Paul Sandoz44afe202016-05-17 12:06:41 +0200456 MethodHandle checkVarHandleExactType(VarHandle handle, VarHandle.AccessDescriptor ad) {
457 MethodHandle mh = handle.getMethodHandle(ad.mode);
458 MethodType mt = mh.type();
459 if (mt != ad.symbolicMethodTypeInvoker) {
460 throw newWrongMethodTypeException(mt, ad.symbolicMethodTypeInvoker);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100461 }
Paul Sandoz44afe202016-05-17 12:06:41 +0200462 return mh;
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100463 }
464
Christian Thalinger360d5122012-07-24 10:47:44 -0700465 /*non-public*/ static
466 WrongMethodTypeException newWrongMethodTypeException(MethodType actual, MethodType expected) {
467 // FIXME: merge with JVM logic for throwing WMTE
468 return new WrongMethodTypeException("expected "+expected+" but found "+actual);
469 }
470
471 /** Static definition of MethodHandle.invokeExact checking code. */
472 /*non-public*/ static
473 @ForceInline
Shilpi Rastogiff976592016-02-10 11:04:13 +0100474 void checkExactType(MethodHandle mh, MethodType expected) {
Christian Thalinger360d5122012-07-24 10:47:44 -0700475 MethodType actual = mh.type();
476 if (actual != expected)
477 throw newWrongMethodTypeException(expected, actual);
478 }
479
John R Rose62ee2112013-10-05 05:30:38 -0700480 /** Static definition of MethodHandle.invokeGeneric checking code.
481 * Directly returns the type-adjusted MH to invoke, as follows:
482 * {@code (R)MH.invoke(a*) => MH.asType(TYPEOF(a*:R)).invokeBasic(a*)}
483 */
Christian Thalinger360d5122012-07-24 10:47:44 -0700484 /*non-public*/ static
485 @ForceInline
Shilpi Rastogiff976592016-02-10 11:04:13 +0100486 MethodHandle checkGenericType(MethodHandle mh, MethodType expected) {
John R Rose62ee2112013-10-05 05:30:38 -0700487 return mh.asType(expected);
488 /* Maybe add more paths here. Possible optimizations:
489 * for (R)MH.invoke(a*),
490 * let MT0 = TYPEOF(a*:R), MT1 = MH.type
491 *
492 * if MT0==MT1 or MT1 can be safely called by MT0
493 * => MH.invokeBasic(a*)
494 * if MT1 can be safely called by MT0[R := Object]
495 * => MH.invokeBasic(a*) & checkcast(R)
496 * if MT1 can be safely called by MT0[* := Object]
497 * => checkcast(A)* & MH.invokeBasic(a*) & checkcast(R)
498 * if a big adapter BA can be pulled out of (MT0,MT1)
499 * => BA.invokeBasic(MT0,MH,a*)
500 * if a local adapter LA can cached on static CS0 = new GICS(MT0)
501 * => CS0.LA.invokeBasic(MH,a*)
502 * else
503 * => MH.asType(MT0).invokeBasic(A*)
504 */
Christian Thalinger360d5122012-07-24 10:47:44 -0700505 }
506
507 static MemberName linkToCallSiteMethod(MethodType mtype) {
John R Rose62ee2112013-10-05 05:30:38 -0700508 LambdaForm lform = callSiteForm(mtype, false);
Christian Thalinger360d5122012-07-24 10:47:44 -0700509 return lform.vmentry;
510 }
511
John R Rose62ee2112013-10-05 05:30:38 -0700512 static MemberName linkToTargetMethod(MethodType mtype) {
513 LambdaForm lform = callSiteForm(mtype, true);
514 return lform.vmentry;
515 }
516
517 // skipCallSite is true if we are optimizing a ConstantCallSite
518 private static LambdaForm callSiteForm(MethodType mtype, boolean skipCallSite) {
Christian Thalinger360d5122012-07-24 10:47:44 -0700519 mtype = mtype.basicType(); // normalize Z to I, String to Object, etc.
John R Rose62ee2112013-10-05 05:30:38 -0700520 final int which = (skipCallSite ? MethodTypeForm.LF_MH_LINKER : MethodTypeForm.LF_CS_LINKER);
521 LambdaForm lform = mtype.form().cachedLambdaForm(which);
Christian Thalinger360d5122012-07-24 10:47:44 -0700522 if (lform != null) return lform;
523 // exactInvokerForm (Object,Object)Object
524 // link with java.lang.invoke.MethodHandle.invokeBasic(MethodHandle,Object,Object)Object/invokeSpecial
525 final int ARG_BASE = 0;
526 final int OUTARG_LIMIT = ARG_BASE + mtype.parameterCount();
527 final int INARG_LIMIT = OUTARG_LIMIT + 1;
528 int nameCursor = OUTARG_LIMIT;
John R Rose62ee2112013-10-05 05:30:38 -0700529 final int APPENDIX_ARG = nameCursor++; // the last in-argument
530 final int CSITE_ARG = skipCallSite ? -1 : APPENDIX_ARG;
531 final int CALL_MH = skipCallSite ? APPENDIX_ARG : nameCursor++; // result of getTarget
Christian Thalinger360d5122012-07-24 10:47:44 -0700532 final int LINKER_CALL = nameCursor++;
John R Rose62ee2112013-10-05 05:30:38 -0700533 MethodType invokerFormType = mtype.appendParameterTypes(skipCallSite ? MethodHandle.class : CallSite.class);
Christian Thalinger360d5122012-07-24 10:47:44 -0700534 Name[] names = arguments(nameCursor - INARG_LIMIT, invokerFormType);
535 assert(names.length == nameCursor);
John R Rose62ee2112013-10-05 05:30:38 -0700536 assert(names[APPENDIX_ARG] != null);
537 if (!skipCallSite)
538 names[CALL_MH] = new Name(NF_getCallSiteTarget, names[CSITE_ARG]);
Christian Thalinger360d5122012-07-24 10:47:44 -0700539 // (site.)invokedynamic(a*):R => mh = site.getTarget(); mh.invokeBasic(a*)
540 final int PREPEND_MH = 0, PREPEND_COUNT = 1;
541 Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, OUTARG_LIMIT + PREPEND_COUNT, Object[].class);
542 // prepend MH argument:
543 System.arraycopy(outArgs, 0, outArgs, PREPEND_COUNT, outArgs.length - PREPEND_COUNT);
544 outArgs[PREPEND_MH] = names[CALL_MH];
John R Rose62ee2112013-10-05 05:30:38 -0700545 names[LINKER_CALL] = new Name(mtype, outArgs);
546 lform = new LambdaForm((skipCallSite ? "linkToTargetMethod" : "linkToCallSite"), INARG_LIMIT, names);
Christian Thalinger360d5122012-07-24 10:47:44 -0700547 lform.compileToBytecode(); // JVM needs a real methodOop
John R Rose62ee2112013-10-05 05:30:38 -0700548 lform = mtype.form().setCachedLambdaForm(which, lform);
Christian Thalinger360d5122012-07-24 10:47:44 -0700549 return lform;
550 }
551
552 /** Static definition of MethodHandle.invokeGeneric checking code. */
553 /*non-public*/ static
554 @ForceInline
Shilpi Rastogiff976592016-02-10 11:04:13 +0100555 MethodHandle getCallSiteTarget(CallSite site) {
556 return site.getTarget();
Christian Thalinger360d5122012-07-24 10:47:44 -0700557 }
558
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800559 /*non-public*/ static
560 @ForceInline
Shilpi Rastogiff976592016-02-10 11:04:13 +0100561 void checkCustomized(MethodHandle mh) {
Vladimir Ivanov71020f92015-03-20 11:42:31 -0700562 if (MethodHandleImpl.isCompileConstant(mh)) return;
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800563 if (mh.form.customized == null) {
564 maybeCustomize(mh);
565 }
566 }
567
568 /*non-public*/ static
569 @DontInline
570 void maybeCustomize(MethodHandle mh) {
571 byte count = mh.customizationCount;
572 if (count >= CUSTOMIZE_THRESHOLD) {
573 mh.customize();
574 } else {
575 mh.customizationCount = (byte)(count+1);
576 }
577 }
578
Christian Thalinger360d5122012-07-24 10:47:44 -0700579 // Local constant functions:
John Roseda56d3f2014-09-10 19:19:47 +0400580 private static final NamedFunction
581 NF_checkExactType,
582 NF_checkGenericType,
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800583 NF_getCallSiteTarget,
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100584 NF_checkCustomized,
585 NF_checkVarHandleGenericType,
Paul Sandoz44afe202016-05-17 12:06:41 +0200586 NF_checkVarHandleExactType;
Christian Thalinger360d5122012-07-24 10:47:44 -0700587 static {
588 try {
John Roseda56d3f2014-09-10 19:19:47 +0400589 NamedFunction nfs[] = {
590 NF_checkExactType = new NamedFunction(Invokers.class
Shilpi Rastogiff976592016-02-10 11:04:13 +0100591 .getDeclaredMethod("checkExactType", MethodHandle.class, MethodType.class)),
John Roseda56d3f2014-09-10 19:19:47 +0400592 NF_checkGenericType = new NamedFunction(Invokers.class
Shilpi Rastogiff976592016-02-10 11:04:13 +0100593 .getDeclaredMethod("checkGenericType", MethodHandle.class, MethodType.class)),
John Roseda56d3f2014-09-10 19:19:47 +0400594 NF_getCallSiteTarget = new NamedFunction(Invokers.class
Shilpi Rastogiff976592016-02-10 11:04:13 +0100595 .getDeclaredMethod("getCallSiteTarget", CallSite.class)),
Vladimir Ivanovbfc51932015-01-29 10:27:30 -0800596 NF_checkCustomized = new NamedFunction(Invokers.class
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100597 .getDeclaredMethod("checkCustomized", MethodHandle.class)),
598 NF_checkVarHandleGenericType = new NamedFunction(Invokers.class
599 .getDeclaredMethod("checkVarHandleGenericType", VarHandle.class, VarHandle.AccessDescriptor.class)),
600 NF_checkVarHandleExactType = new NamedFunction(Invokers.class
601 .getDeclaredMethod("checkVarHandleExactType", VarHandle.class, VarHandle.AccessDescriptor.class)),
John Roseda56d3f2014-09-10 19:19:47 +0400602 };
Claes Redestad30545d62015-11-17 11:51:45 +0100603 // Each nf must be statically invocable or we get tied up in our bootstraps.
604 assert(InvokerBytecodeGenerator.isStaticallyInvocable(nfs));
Christian Thalinger360d5122012-07-24 10:47:44 -0700605 } catch (ReflectiveOperationException ex) {
Christian Thalinger01d0ba62012-10-19 17:04:35 -0700606 throw newInternalError(ex);
Christian Thalinger360d5122012-07-24 10:47:44 -0700607 }
608 }
609
John Rose6a177f42014-09-10 19:19:48 +0400610 private static class Lazy {
611 private static final MethodHandle MH_asSpreader;
612
613 static {
614 try {
615 MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader",
616 MethodType.methodType(MethodHandle.class, Class.class, int.class));
617 } catch (ReflectiveOperationException ex) {
618 throw newInternalError(ex);
619 }
620 }
621 }
Claes Redestad60754ca2016-08-24 16:11:21 +0200622
623 static {
624 // The Holder class will contain pre-generated Invokers resolved
625 // speculatively using MemberName.getFactory().resolveOrNull. However, that
626 // doesn't initialize the class, which subtly breaks inlining etc. By forcing
627 // initialization of the Holder class we avoid these issues.
628 UNSAFE.ensureClassInitialized(Holder.class);
629 }
630
631 /* Placeholder class for Invokers generated ahead of time */
632 final class Holder {}
John R Rosefb6164c2009-05-05 22:40:09 -0700633}