1 /*
2 * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26 package java.lang.invoke;
27
28 import jdk.internal.access.JavaLangInvokeAccess;
29 import jdk.internal.access.SharedSecrets;
30 import jdk.internal.constant.ClassOrInterfaceDescImpl;
31 import jdk.internal.constant.ConstantUtils;
32 import jdk.internal.constant.MethodTypeDescImpl;
33 import jdk.internal.foreign.abi.NativeEntryPoint;
34 import jdk.internal.reflect.CallerSensitive;
35 import jdk.internal.reflect.Reflection;
36 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
37 import jdk.internal.vm.annotation.ForceInline;
38 import jdk.internal.vm.annotation.Hidden;
39 import jdk.internal.vm.annotation.Stable;
40 import sun.invoke.util.ValueConversions;
41 import sun.invoke.util.VerifyType;
42 import sun.invoke.util.Wrapper;
43
44 import java.lang.classfile.ClassBuilder;
45 import java.lang.classfile.ClassFile;
46 import java.lang.constant.ClassDesc;
47 import java.lang.foreign.MemoryLayout;
48 import java.lang.invoke.MethodHandles.Lookup;
49 import java.lang.reflect.Array;
50 import java.lang.reflect.Constructor;
51 import java.lang.reflect.Field;
52 import java.nio.ByteOrder;
53 import java.util.Arrays;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.Iterator;
57 import java.util.List;
58 import java.util.Map;
59 import java.util.Objects;
60 import java.util.concurrent.ConcurrentHashMap;
61 import java.util.function.Function;
62 import java.util.stream.Stream;
63
64 import static java.lang.classfile.ClassFile.*;
65 import static java.lang.constant.ConstantDescs.*;
66 import static java.lang.invoke.LambdaForm.*;
67 import static java.lang.invoke.MethodHandleNatives.Constants.MN_CALLER_SENSITIVE;
68 import static java.lang.invoke.MethodHandleNatives.Constants.MN_HIDDEN_MEMBER;
69 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
70 import static java.lang.invoke.MethodHandleStatics.*;
71 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
72
73 /**
74 * Trusted implementation code for MethodHandle.
75 * @author jrose
76 */
77 @AOTSafeClassInitializer
78 /*non-public*/
79 abstract class MethodHandleImpl {
80
81 /// Factory methods to create method handles:
82
83 static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, ArrayAccess access) {
84 if (arrayClass == Object[].class) {
85 return ArrayAccess.objectAccessor(access);
86 }
87 if (!arrayClass.isArray())
88 throw newIllegalArgumentException("not an array: "+arrayClass);
89 MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
90 int cacheIndex = ArrayAccess.cacheIndex(access);
91 MethodHandle mh = cache[cacheIndex];
92 if (mh != null) return mh;
93 mh = ArrayAccessor.getAccessor(arrayClass, access);
94 MethodType correctType = ArrayAccessor.correctType(arrayClass, access);
95 if (mh.type() != correctType) {
96 assert(mh.type().parameterType(0) == Object[].class);
97 /* if access == SET */ assert(access != ArrayAccess.SET || mh.type().parameterType(2) == Object.class);
98 /* if access == GET */ assert(access != ArrayAccess.GET ||
99 (mh.type().returnType() == Object.class &&
100 correctType.parameterType(0).getComponentType() == correctType.returnType()));
101 // safe to view non-strictly, because element type follows from array type
102 mh = mh.viewAsType(correctType, false);
103 }
104 mh = makeIntrinsic(mh, ArrayAccess.intrinsic(access));
105 // Atomically update accessor cache.
106 synchronized(cache) {
107 if (cache[cacheIndex] == null) {
108 cache[cacheIndex] = mh;
109 } else {
110 // Throw away newly constructed accessor and use cached version.
111 mh = cache[cacheIndex];
112 }
113 }
114 return mh;
115 }
116
117 enum ArrayAccess {
118 GET, SET, LENGTH;
119
120 // As ArrayAccess and ArrayAccessor have a circular dependency, the ArrayAccess properties cannot be stored in
121 // final fields.
122
123 static String opName(ArrayAccess a) {
124 return switch (a) {
125 case GET -> "getElement";
126 case SET -> "setElement";
127 case LENGTH -> "length";
128 default -> throw unmatchedArrayAccess(a);
129 };
130 }
131
132 static MethodHandle objectAccessor(ArrayAccess a) {
133 return switch (a) {
134 case GET -> ArrayAccessor.OBJECT_ARRAY_GETTER;
135 case SET -> ArrayAccessor.OBJECT_ARRAY_SETTER;
136 case LENGTH -> ArrayAccessor.OBJECT_ARRAY_LENGTH;
137 default -> throw unmatchedArrayAccess(a);
138 };
139 }
140
141 static int cacheIndex(ArrayAccess a) {
142 return switch (a) {
143 case GET -> ArrayAccessor.GETTER_INDEX;
144 case SET -> ArrayAccessor.SETTER_INDEX;
145 case LENGTH -> ArrayAccessor.LENGTH_INDEX;
146 default -> throw unmatchedArrayAccess(a);
147 };
148 }
149
150 static Intrinsic intrinsic(ArrayAccess a) {
151 return switch (a) {
152 case GET -> Intrinsic.ARRAY_LOAD;
153 case SET -> Intrinsic.ARRAY_STORE;
154 case LENGTH -> Intrinsic.ARRAY_LENGTH;
155 default -> throw unmatchedArrayAccess(a);
156 };
157 }
158 }
159
160 static InternalError unmatchedArrayAccess(ArrayAccess a) {
161 return newInternalError("should not reach here (unmatched ArrayAccess: " + a + ")");
162 }
163
164 @AOTSafeClassInitializer
165 static final class ArrayAccessor {
166 /// Support for array element and length access
167 static final int GETTER_INDEX = 0, SETTER_INDEX = 1, LENGTH_INDEX = 2, INDEX_LIMIT = 3;
168 static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
169 = new ClassValue<MethodHandle[]>() {
170 @Override
171 protected MethodHandle[] computeValue(Class<?> type) {
172 return new MethodHandle[INDEX_LIMIT];
173 }
174 };
175 static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER, OBJECT_ARRAY_LENGTH;
176 static {
177 MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
178 cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.GET), Intrinsic.ARRAY_LOAD);
179 cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.SET), Intrinsic.ARRAY_STORE);
180 cache[LENGTH_INDEX] = OBJECT_ARRAY_LENGTH = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.LENGTH), Intrinsic.ARRAY_LENGTH);
181
182 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
183 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
184 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_LENGTH.internalMemberName()));
185 }
186
187 static int getElementI(int[] a, int i) { return a[i]; }
188 static long getElementJ(long[] a, int i) { return a[i]; }
189 static float getElementF(float[] a, int i) { return a[i]; }
190 static double getElementD(double[] a, int i) { return a[i]; }
191 static boolean getElementZ(boolean[] a, int i) { return a[i]; }
192 static byte getElementB(byte[] a, int i) { return a[i]; }
193 static short getElementS(short[] a, int i) { return a[i]; }
194 static char getElementC(char[] a, int i) { return a[i]; }
195 static Object getElementL(Object[] a, int i) { return a[i]; }
196
197 static void setElementI(int[] a, int i, int x) { a[i] = x; }
198 static void setElementJ(long[] a, int i, long x) { a[i] = x; }
199 static void setElementF(float[] a, int i, float x) { a[i] = x; }
200 static void setElementD(double[] a, int i, double x) { a[i] = x; }
201 static void setElementZ(boolean[] a, int i, boolean x) { a[i] = x; }
202 static void setElementB(byte[] a, int i, byte x) { a[i] = x; }
203 static void setElementS(short[] a, int i, short x) { a[i] = x; }
204 static void setElementC(char[] a, int i, char x) { a[i] = x; }
205 static void setElementL(Object[] a, int i, Object x) { a[i] = x; }
206
207 static int lengthI(int[] a) { return a.length; }
208 static int lengthJ(long[] a) { return a.length; }
209 static int lengthF(float[] a) { return a.length; }
210 static int lengthD(double[] a) { return a.length; }
211 static int lengthZ(boolean[] a) { return a.length; }
212 static int lengthB(byte[] a) { return a.length; }
213 static int lengthS(short[] a) { return a.length; }
214 static int lengthC(char[] a) { return a.length; }
215 static int lengthL(Object[] a) { return a.length; }
216
217 static String name(Class<?> arrayClass, ArrayAccess access) {
218 Class<?> elemClass = arrayClass.getComponentType();
219 if (elemClass == null) throw newIllegalArgumentException("not an array", arrayClass);
220 return ArrayAccess.opName(access) + Wrapper.basicTypeChar(elemClass);
221 }
222 static MethodType type(Class<?> arrayClass, ArrayAccess access) {
223 Class<?> elemClass = arrayClass.getComponentType();
224 Class<?> arrayArgClass = arrayClass;
225 if (!elemClass.isPrimitive()) {
226 arrayArgClass = Object[].class;
227 elemClass = Object.class;
228 }
229 return switch (access) {
230 case GET -> MethodType.methodType(elemClass, arrayArgClass, int.class);
231 case SET -> MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
232 case LENGTH -> MethodType.methodType(int.class, arrayArgClass);
233 default -> throw unmatchedArrayAccess(access);
234 };
235 }
236 static MethodType correctType(Class<?> arrayClass, ArrayAccess access) {
237 Class<?> elemClass = arrayClass.getComponentType();
238 return switch (access) {
239 case GET -> MethodType.methodType(elemClass, arrayClass, int.class);
240 case SET -> MethodType.methodType(void.class, arrayClass, int.class, elemClass);
241 case LENGTH -> MethodType.methodType(int.class, arrayClass);
242 default -> throw unmatchedArrayAccess(access);
243 };
244 }
245 static MethodHandle getAccessor(Class<?> arrayClass, ArrayAccess access) {
246 String name = name(arrayClass, access);
247 MethodType type = type(arrayClass, access);
248 try {
249 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
250 } catch (ReflectiveOperationException ex) {
251 throw uncaughtException(ex);
252 }
253 }
254 }
255
256 /**
257 * Create a JVM-level adapter method handle to conform the given method
258 * handle to the similar newType, using only pairwise argument conversions.
259 * For each argument, convert incoming argument to the exact type needed.
260 * The argument conversions allowed are casting, boxing and unboxing,
261 * integral widening or narrowing, and floating point widening or narrowing.
262 * @param srcType required call type
263 * @param target original method handle
264 * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
265 * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
266 * @return an adapter to the original handle with the desired new type,
267 * or the original target if the types are already identical
268 * or null if the adaptation cannot be made
269 */
270 static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
271 boolean strict, boolean monobox) {
272 MethodType dstType = target.type();
273 if (srcType == dstType)
274 return target;
275 return makePairwiseConvertByEditor(target, srcType, strict, monobox);
276 }
277
278 private static int countNonNull(Object[] array) {
279 int count = 0;
280 if (array != null) {
281 for (Object x : array) {
282 if (x != null) ++count;
283 }
284 }
285 return count;
286 }
287
288 static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
289 boolean strict, boolean monobox) {
290 // In method types arguments start at index 0, while the LF
291 // editor have the MH receiver at position 0 - adjust appropriately.
292 final int MH_RECEIVER_OFFSET = 1;
293 Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
294 int convCount = countNonNull(convSpecs);
295 if (convCount == 0)
296 return target.viewAsType(srcType, strict);
297 MethodType basicSrcType = srcType.basicType();
298 MethodType midType = target.type().basicType();
299 BoundMethodHandle mh = target.rebind();
300
301 // Match each unique conversion to the positions at which it is to be applied
302 HashMap<Object, int[]> convSpecMap = HashMap.newHashMap(convCount);
303 for (int i = 0; i < convSpecs.length - MH_RECEIVER_OFFSET; i++) {
304 Object convSpec = convSpecs[i];
305 if (convSpec == null) continue;
306 int[] positions = convSpecMap.get(convSpec);
307 if (positions == null) {
308 positions = new int[] { i + MH_RECEIVER_OFFSET };
309 } else {
310 positions = Arrays.copyOf(positions, positions.length + 1);
311 positions[positions.length - 1] = i + MH_RECEIVER_OFFSET;
312 }
313 convSpecMap.put(convSpec, positions);
314 }
315 for (var entry : convSpecMap.entrySet()) {
316 Object convSpec = entry.getKey();
317
318 MethodHandle fn;
319 if (convSpec instanceof Class) {
320 fn = getConstantHandle(MH_cast).bindTo(convSpec);
321 } else {
322 fn = (MethodHandle) convSpec;
323 }
324 int[] positions = entry.getValue();
325 Class<?> newType = basicSrcType.parameterType(positions[0] - MH_RECEIVER_OFFSET);
326 BasicType newBasicType = BasicType.basicType(newType);
327 convCount -= positions.length;
328 if (convCount == 0) {
329 midType = srcType;
330 } else {
331 Class<?>[] ptypes = midType.ptypes().clone();
332 for (int pos : positions) {
333 ptypes[pos - 1] = newType;
334 }
335 midType = MethodType.methodType(midType.rtype(), ptypes, true);
336 }
337 LambdaForm form2;
338 if (positions.length > 1) {
339 form2 = mh.editor().filterRepeatedArgumentForm(newBasicType, positions);
340 } else {
341 form2 = mh.editor().filterArgumentForm(positions[0], newBasicType);
342 }
343 mh = mh.copyWithExtendL(midType, form2, fn);
344 }
345 Object convSpec = convSpecs[convSpecs.length - 1];
346 if (convSpec != null) {
347 MethodHandle fn;
348 if (convSpec instanceof Class) {
349 if (convSpec == void.class)
350 fn = null;
351 else
352 fn = getConstantHandle(MH_cast).bindTo(convSpec);
353 } else {
354 fn = (MethodHandle) convSpec;
355 }
356 Class<?> newType = basicSrcType.returnType();
357 assert(--convCount == 0);
358 midType = srcType;
359 if (fn != null) {
360 mh = mh.rebind(); // rebind if too complex
361 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
362 mh = mh.copyWithExtendL(midType, form2, fn);
363 } else {
364 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
365 mh = mh.copyWith(midType, form2);
366 }
367 }
368 assert(convCount == 0);
369 assert(mh.type().equals(srcType));
370 return mh;
371 }
372
373 static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
374 boolean strict, boolean monobox) {
375 final int INARG_COUNT = srcType.parameterCount();
376 Object[] convSpecs = null;
377 for (int i = 0; i <= INARG_COUNT; i++) {
378 boolean isRet = (i == INARG_COUNT);
379 Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
380 Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
381 if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
382 if (convSpecs == null) {
383 convSpecs = new Object[INARG_COUNT + 1];
384 }
385 convSpecs[i] = valueConversion(src, dst, strict, monobox);
386 }
387 }
388 return convSpecs;
389 }
390 static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
391 boolean strict) {
392 return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
393 }
394
395 /**
396 * Find a conversion function from the given source to the given destination.
397 * This conversion function will be used as a LF NamedFunction.
398 * Return a Class object if a simple cast is needed.
399 * Return void.class if void is involved.
400 */
401 static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
402 assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)); // caller responsibility
403 if (dst == void.class)
404 return dst;
405 MethodHandle fn;
406 if (src.isPrimitive()) {
407 if (src == void.class) {
408 return void.class; // caller must recognize this specially
409 } else if (dst.isPrimitive()) {
410 // Examples: int->byte, byte->int, boolean->int (!strict)
411 fn = ValueConversions.convertPrimitive(src, dst);
412 } else {
413 // Examples: int->Integer, boolean->Object, float->Number
414 Wrapper wsrc = Wrapper.forPrimitiveType(src);
415 fn = ValueConversions.boxExact(wsrc);
416 assert(fn.type().parameterType(0) == wsrc.primitiveType());
417 assert(fn.type().returnType() == wsrc.wrapperType());
418 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
419 // Corner case, such as int->Long, which will probably fail.
420 MethodType mt = MethodType.methodType(dst, src);
421 if (strict)
422 fn = fn.asType(mt);
423 else
424 fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
425 }
426 }
427 } else if (dst.isPrimitive()) {
428 Wrapper wdst = Wrapper.forPrimitiveType(dst);
429 if (monobox || src == wdst.wrapperType()) {
430 // Use a strongly-typed unboxer, if possible.
431 fn = ValueConversions.unboxExact(wdst, strict);
432 } else {
433 // Examples: Object->int, Number->int, Comparable->int, Byte->int
434 // must include additional conversions
435 // src must be examined at runtime, to detect Byte, Character, etc.
436 fn = (strict
437 ? ValueConversions.unboxWiden(wdst)
438 : ValueConversions.unboxCast(wdst));
439 }
440 } else {
441 // Simple reference conversion.
442 // Note: Do not check for a class hierarchy relation
443 // between src and dst. In all cases a 'null' argument
444 // will pass the cast conversion.
445 return dst;
446 }
447 assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
448 return fn;
449 }
450
451 static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
452 MethodType type = target.type();
453 int last = type.parameterCount() - 1;
454 if (type.parameterType(last) != arrayType)
455 target = target.asType(type.changeParameterType(last, arrayType));
456 target = target.asFixedArity(); // make sure this attribute is turned off
457 return new AsVarargsCollector(target, arrayType);
458 }
459
460 @AOTSafeClassInitializer
461 static final class AsVarargsCollector extends DelegatingMethodHandle {
462 private final MethodHandle target;
463 private final Class<?> arrayType;
464 private MethodHandle asCollectorCache;
465
466 AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
467 this(target.type(), target, arrayType);
468 }
469 AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
470 super(type, target);
471 this.target = target;
472 this.arrayType = arrayType;
473 }
474
475 @Override
476 public boolean isVarargsCollector() {
477 return true;
478 }
479
480 @Override
481 protected MethodHandle getTarget() {
482 return target;
483 }
484
485 @Override
486 public MethodHandle asFixedArity() {
487 return target;
488 }
489
490 @Override
491 MethodHandle setVarargs(MemberName member) {
492 if (member.isVarargs()) return this;
493 return asFixedArity();
494 }
495
496 @Override
497 public MethodHandle withVarargs(boolean makeVarargs) {
498 if (makeVarargs) return this;
499 return asFixedArity();
500 }
501
502 @Override
503 public MethodHandle asTypeUncached(MethodType newType) {
504 MethodType type = this.type();
505 int collectArg = type.parameterCount() - 1;
506 int newArity = newType.parameterCount();
507 if (newArity == collectArg+1 &&
508 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
509 // if arity and trailing parameter are compatible, do normal thing
510 return asFixedArity().asType(newType);
511 }
512 // check cache
513 MethodHandle acc = asCollectorCache;
514 if (acc != null && acc.type().parameterCount() == newArity)
515 return acc.asType(newType);
516 // build and cache a collector
517 int arrayLength = newArity - collectArg;
518 MethodHandle collector;
519 try {
520 collector = asFixedArity().asCollector(arrayType, arrayLength);
521 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
522 } catch (IllegalArgumentException ex) {
523 throw new WrongMethodTypeException("cannot build collector", ex);
524 }
525 asCollectorCache = collector;
526 return collector.asType(newType);
527 }
528
529 @Override
530 boolean viewAsTypeChecks(MethodType newType, boolean strict) {
531 super.viewAsTypeChecks(newType, true);
532 if (strict) return true;
533 // extra assertion for non-strict checks:
534 assert (type().lastParameterType().getComponentType()
535 .isAssignableFrom(
536 newType.lastParameterType().getComponentType()))
537 : Arrays.asList(this, newType);
538 return true;
539 }
540
541 @Override
542 public Object invokeWithArguments(Object... arguments) throws Throwable {
543 MethodType type = this.type();
544 int argc;
545 final int MAX_SAFE = 127; // 127 longs require 254 slots, which is safe to spread
546 if (arguments == null
547 || (argc = arguments.length) <= MAX_SAFE
548 || argc < type.parameterCount()) {
549 return super.invokeWithArguments(arguments);
550 }
551
552 // a jumbo invocation requires more explicit reboxing of the trailing arguments
553 int uncollected = type.parameterCount() - 1;
554 Class<?> elemType = arrayType.getComponentType();
555 int collected = argc - uncollected;
556 Object collArgs = (elemType == Object.class)
557 ? new Object[collected] : Array.newInstance(elemType, collected);
558 if (!elemType.isPrimitive()) {
559 // simple cast: just do some casting
560 try {
561 System.arraycopy(arguments, uncollected, collArgs, 0, collected);
562 } catch (ArrayStoreException ex) {
563 return super.invokeWithArguments(arguments);
564 }
565 } else {
566 // corner case of flat array requires reflection (or specialized copy loop)
567 MethodHandle arraySetter = MethodHandles.arrayElementSetter(arrayType);
568 try {
569 for (int i = 0; i < collected; i++) {
570 arraySetter.invoke(collArgs, i, arguments[uncollected + i]);
571 }
572 } catch (WrongMethodTypeException|ClassCastException ex) {
573 return super.invokeWithArguments(arguments);
574 }
575 }
576
577 // chop the jumbo list down to size and call in non-varargs mode
578 Object[] newArgs = new Object[uncollected + 1];
579 System.arraycopy(arguments, 0, newArgs, 0, uncollected);
580 newArgs[uncollected] = collArgs;
581 return asFixedArity().invokeWithArguments(newArgs);
582 }
583 }
584
585 static void checkSpreadArgument(Object av, int n) {
586 if (av == null && n == 0) {
587 return;
588 } else if (av == null) {
589 throw new NullPointerException("null array reference");
590 } else if (av instanceof Object[] array) {
591 int len = array.length;
592 if (len == n) return;
593 } else {
594 int len = java.lang.reflect.Array.getLength(av);
595 if (len == n) return;
596 }
597 // fall through to error:
598 throw newIllegalArgumentException("array is not of length "+n);
599 }
600
601 @Hidden
602 static MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
603 if (testResult) {
604 return target;
605 } else {
606 return fallback;
607 }
608 }
609
610 // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
611 @Hidden
612 @jdk.internal.vm.annotation.IntrinsicCandidate
613 static boolean profileBoolean(boolean result, int[] counters) {
614 // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
615 int idx = result ? 1 : 0;
616 try {
617 counters[idx] = Math.addExact(counters[idx], 1);
618 } catch (ArithmeticException e) {
619 // Avoid continuous overflow by halving the problematic count.
620 counters[idx] = counters[idx] / 2;
621 }
622 return result;
623 }
624
625 // Intrinsified by C2. Returns true if obj is a compile-time constant.
626 @Hidden
627 @jdk.internal.vm.annotation.IntrinsicCandidate
628 static boolean isCompileConstant(Object obj) {
629 return false;
630 }
631
632 static MethodHandle makeGuardWithTest(MethodHandle test,
633 MethodHandle target,
634 MethodHandle fallback) {
635 MethodType type = target.type();
636 assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
637 MethodType basicType = type.basicType();
638 LambdaForm form = makeGuardWithTestForm(basicType);
639 BoundMethodHandle mh;
640 try {
641 if (PROFILE_GWT) {
642 int[] counts = new int[2];
643 mh = (BoundMethodHandle)
644 BoundMethodHandle.speciesData_LLLL().factory().invokeBasic(type, form,
645 (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
646 } else {
647 mh = (BoundMethodHandle)
648 BoundMethodHandle.speciesData_LLL().factory().invokeBasic(type, form,
649 (Object) test, (Object) profile(target), (Object) profile(fallback));
650 }
651 } catch (Throwable ex) {
652 throw uncaughtException(ex);
653 }
654 assert(mh.type() == type);
655 return mh;
656 }
657
658
659 static MethodHandle profile(MethodHandle target) {
660 if (DONT_INLINE_THRESHOLD >= 0) {
661 return makeBlockInliningWrapper(target);
662 } else {
663 return target;
664 }
665 }
666
667 /**
668 * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
669 * Corresponding LambdaForm has @DontInline when compiled into bytecode.
670 */
671 static MethodHandle makeBlockInliningWrapper(MethodHandle target) {
672 LambdaForm lform;
673 if (DONT_INLINE_THRESHOLD > 0) {
674 lform = Makers.PRODUCE_BLOCK_INLINING_FORM.apply(target);
675 } else {
676 lform = Makers.PRODUCE_REINVOKER_FORM.apply(target);
677 }
678 return new CountingWrapper(target, lform,
679 Makers.PRODUCE_BLOCK_INLINING_FORM, Makers.PRODUCE_REINVOKER_FORM,
680 DONT_INLINE_THRESHOLD);
681 }
682
683 @AOTSafeClassInitializer
684 private static final class Makers {
685 /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
686 static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
687 @Override
688 public LambdaForm apply(MethodHandle target) {
689 return DelegatingMethodHandle.makeReinvokerForm(target,
690 MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, false,
691 DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
692 }
693 };
694
695 /** Constructs simple reinvoker lambda form for a particular method handle */
696 static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
697 @Override
698 public LambdaForm apply(MethodHandle target) {
699 return DelegatingMethodHandle.makeReinvokerForm(target,
700 MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
701 }
702 };
703
704 /** Maker of type-polymorphic varargs */
705 static final ClassValue<MethodHandle[]> TYPED_COLLECTORS = new ClassValue<MethodHandle[]>() {
706 @Override
707 protected MethodHandle[] computeValue(Class<?> type) {
708 return new MethodHandle[MAX_JVM_ARITY + 1];
709 }
710 };
711 }
712
713 /**
714 * Counting method handle. It has 2 states: counting and non-counting.
715 * It is in counting state for the first n invocations and then transitions to non-counting state.
716 * Behavior in counting and non-counting states is determined by lambda forms produced by
717 * countingFormProducer & nonCountingFormProducer respectively.
718 */
719 @AOTSafeClassInitializer
720 static final class CountingWrapper extends DelegatingMethodHandle {
721 private final MethodHandle target;
722 private int count;
723 private Function<MethodHandle, LambdaForm> countingFormProducer;
724 private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
725 private volatile boolean isCounting;
726
727 private CountingWrapper(MethodHandle target, LambdaForm lform,
728 Function<MethodHandle, LambdaForm> countingFromProducer,
729 Function<MethodHandle, LambdaForm> nonCountingFormProducer,
730 int count) {
731 super(target.type(), lform);
732 this.target = target;
733 this.count = count;
734 this.countingFormProducer = countingFromProducer;
735 this.nonCountingFormProducer = nonCountingFormProducer;
736 this.isCounting = (count > 0);
737 }
738
739 @Hidden
740 @Override
741 protected MethodHandle getTarget() {
742 return target;
743 }
744
745 @Override
746 public MethodHandle asTypeUncached(MethodType newType) {
747 MethodHandle newTarget = target.asType(newType);
748 MethodHandle wrapper;
749 if (isCounting) {
750 LambdaForm lform;
751 lform = countingFormProducer.apply(newTarget);
752 wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
753 } else {
754 wrapper = newTarget; // no need for a counting wrapper anymore
755 }
756 return wrapper;
757 }
758
759 boolean countDown() {
760 int c = count;
761 target.maybeCustomize(); // customize if counting happens for too long
762 if (c <= 1) {
763 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
764 if (isCounting) {
765 isCounting = false;
766 return true;
767 } else {
768 return false;
769 }
770 } else {
771 count = c - 1;
772 return false;
773 }
774 }
775
776 @Hidden
777 static void maybeStopCounting(Object o1) {
778 final CountingWrapper wrapper = (CountingWrapper) o1;
779 if (wrapper.countDown()) {
780 // Reached invocation threshold. Replace counting behavior with a non-counting one.
781 wrapper.updateForm(new Function<>() {
782 public LambdaForm apply(LambdaForm oldForm) {
783 LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
784 lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
785 return lform;
786 }});
787 }
788 }
789
790 static final NamedFunction NF_maybeStopCounting;
791 static {
792 Class<?> THIS_CLASS = CountingWrapper.class;
793 try {
794 NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
795 } catch (ReflectiveOperationException ex) {
796 throw newInternalError(ex);
797 }
798 }
799 }
800
801 static LambdaForm makeGuardWithTestForm(MethodType basicType) {
802 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
803 if (lform != null) return lform;
804 final int THIS_MH = 0; // the BMH_LLL
805 final int ARG_BASE = 1; // start of incoming arguments
806 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
807 int nameCursor = ARG_LIMIT;
808 final int GET_TEST = nameCursor++;
809 final int GET_TARGET = nameCursor++;
810 final int GET_FALLBACK = nameCursor++;
811 final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
812 final int CALL_TEST = nameCursor++;
813 final int PROFILE = (GET_COUNTERS != -1) ? nameCursor++ : -1;
814 final int TEST = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
815 final int SELECT_ALT = nameCursor++;
816 final int CALL_TARGET = nameCursor++;
817 assert(CALL_TARGET == SELECT_ALT+1); // must be true to trigger IBG.emitSelectAlternative
818
819 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
820
821 BoundMethodHandle.SpeciesData data =
822 (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
823 : BoundMethodHandle.speciesData_LLL();
824 names[THIS_MH] = names[THIS_MH].withConstraint(data);
825 names[GET_TEST] = new Name(data.getterFunction(0), names[THIS_MH]);
826 names[GET_TARGET] = new Name(data.getterFunction(1), names[THIS_MH]);
827 names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
828 if (GET_COUNTERS != -1) {
829 names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
830 }
831 Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
832
833 // call test
834 MethodType testType = basicType.changeReturnType(boolean.class).basicType();
835 invokeArgs[0] = names[GET_TEST];
836 names[CALL_TEST] = new Name(testType, invokeArgs);
837
838 // profile branch
839 if (PROFILE != -1) {
840 names[PROFILE] = new Name(getFunction(NF_profileBoolean), names[CALL_TEST], names[GET_COUNTERS]);
841 }
842 // call selectAlternative
843 names[SELECT_ALT] = new Name(new NamedFunction(
844 makeIntrinsic(getConstantHandle(MH_selectAlternative), Intrinsic.SELECT_ALTERNATIVE)),
845 names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
846
847 // call target or fallback
848 invokeArgs[0] = names[SELECT_ALT];
849 names[CALL_TARGET] = new Name(basicType, invokeArgs);
850
851 lform = LambdaForm.create(basicType.parameterCount() + 1, names, /*forceInline=*/true, Kind.GUARD);
852
853 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
854 }
855
856 /**
857 * The LambdaForm shape for catchException combinator is the following:
858 * <blockquote><pre>{@code
859 * guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
860 * t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
861 * t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
862 * t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
863 * t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
864 * t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
865 * t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
866 * t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
867 * t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
868 * }</pre></blockquote>
869 *
870 * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
871 * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
872 * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
873 *
874 * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
875 * among catchException combinators with the same basic type.
876 */
877 private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
878 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
879 if (lform != null) {
880 return lform;
881 }
882 final int THIS_MH = 0; // the BMH_LLLLL
883 final int ARG_BASE = 1; // start of incoming arguments
884 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
885
886 int nameCursor = ARG_LIMIT;
887 final int GET_TARGET = nameCursor++;
888 final int GET_CLASS = nameCursor++;
889 final int GET_CATCHER = nameCursor++;
890 final int GET_COLLECT_ARGS = nameCursor++;
891 final int GET_UNBOX_RESULT = nameCursor++;
892 final int BOXED_ARGS = nameCursor++;
893 final int TRY_CATCH = nameCursor++;
894 final int UNBOX_RESULT = nameCursor++;
895
896 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
897
898 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
899 names[THIS_MH] = names[THIS_MH].withConstraint(data);
900 names[GET_TARGET] = new Name(data.getterFunction(0), names[THIS_MH]);
901 names[GET_CLASS] = new Name(data.getterFunction(1), names[THIS_MH]);
902 names[GET_CATCHER] = new Name(data.getterFunction(2), names[THIS_MH]);
903 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
904 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
905
906 // FIXME: rework argument boxing/result unboxing logic for LF interpretation
907
908 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
909 MethodType collectArgsType = basicType.changeReturnType(Object.class);
910 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
911 Object[] args = new Object[invokeBasic.type().parameterCount()];
912 args[0] = names[GET_COLLECT_ARGS];
913 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
914 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH)), args);
915
916 // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
917 Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
918 names[TRY_CATCH] = new Name(getFunction(NF_guardWithCatch), gwcArgs);
919
920 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
921 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
922 Object[] unboxArgs = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
923 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
924
925 lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.GUARD_WITH_CATCH);
926
927 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
928 }
929
930 static MethodHandle makeGuardWithCatch(MethodHandle target,
931 Class<? extends Throwable> exType,
932 MethodHandle catcher) {
933 MethodType type = target.type();
934 LambdaForm form = makeGuardWithCatchForm(type.basicType());
935
936 // Prepare auxiliary method handles used during LambdaForm interpretation.
937 // Box arguments and wrap them into Object[]: ValueConversions.array().
938 MethodType varargsType = type.changeReturnType(Object[].class);
939 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
940 MethodHandle unboxResult = unboxResultHandle(type.returnType());
941
942 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
943 BoundMethodHandle mh;
944 try {
945 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) exType,
946 (Object) catcher, (Object) collectArgs, (Object) unboxResult);
947 } catch (Throwable ex) {
948 throw uncaughtException(ex);
949 }
950 assert(mh.type() == type);
951 return mh;
952 }
953
954 /**
955 * Intrinsified during LambdaForm compilation
956 * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
957 */
958 @Hidden
959 static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
960 Object... av) throws Throwable {
961 // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
962 try {
963 return target.asFixedArity().invokeWithArguments(av);
964 } catch (Throwable t) {
965 if (!exType.isInstance(t)) throw t;
966 return catcher.asFixedArity().invokeWithArguments(prepend(av, t));
967 }
968 }
969
970 /** Prepend elements to an array. */
971 @Hidden
972 private static Object[] prepend(Object[] array, Object... elems) {
973 int nArray = array.length;
974 int nElems = elems.length;
975 Object[] newArray = new Object[nArray + nElems];
976 System.arraycopy(elems, 0, newArray, 0, nElems);
977 System.arraycopy(array, 0, newArray, nElems, nArray);
978 return newArray;
979 }
980
981 static MethodHandle throwException(MethodType type) {
982 assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
983 int arity = type.parameterCount();
984 if (arity > 1) {
985 MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
986 mh = MethodHandles.dropArgumentsTrusted(mh, 1, Arrays.copyOfRange(type.ptypes(), 1, arity));
987 return mh;
988 }
989 return makePairwiseConvert(getFunction(NF_throwException).resolvedHandle(), type, false, true);
990 }
991
992 static <T extends Throwable> Void throwException(T t) throws T { throw t; }
993
994 static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
995 static MethodHandle fakeMethodHandleInvoke(MemberName method) {
996 assert(method.isMethodHandleInvoke());
997 int idx = switch (method.getName()) {
998 case "invoke" -> 0;
999 case "invokeExact" -> 1;
1000 default -> throw new InternalError(method.getName());
1001 };
1002 MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
1003 if (mh != null) return mh;
1004 MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
1005 MethodHandle.class, Object[].class);
1006 mh = throwException(type);
1007 mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
1008 if (!method.getInvocationType().equals(mh.type()))
1009 throw new InternalError(method.toString());
1010 mh = mh.withInternalMemberName(method, false);
1011 mh = mh.withVarargs(true);
1012 assert(method.isVarargs());
1013 FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
1014 return mh;
1015 }
1016 static MethodHandle fakeVarHandleInvoke(MemberName method) {
1017 // TODO caching, is it necessary?
1018 MethodType type = MethodType.methodType(method.getMethodType().returnType(),
1019 UnsupportedOperationException.class,
1020 VarHandle.class, Object[].class);
1021 MethodHandle mh = throwException(type);
1022 mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke VarHandle"));
1023 if (!method.getInvocationType().equals(mh.type()))
1024 throw new InternalError(method.toString());
1025 mh = mh.withInternalMemberName(method, false);
1026 mh = mh.asVarargsCollector(Object[].class);
1027 assert(method.isVarargs());
1028 return mh;
1029 }
1030
1031 /**
1032 * Create an alias for the method handle which, when called,
1033 * appears to be called from the same class loader and protection domain
1034 * as hostClass.
1035 * This is an expensive no-op unless the method which is called
1036 * is sensitive to its caller. A small number of system methods
1037 * are in this category, including Class.forName and Method.invoke.
1038 */
1039 static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1040 return BindCaller.bindCaller(mh, hostClass);
1041 }
1042
1043 // Put the whole mess into its own nested class.
1044 // That way we can lazily load the code and set up the constants.
1045 @AOTSafeClassInitializer
1046 private static class BindCaller {
1047
1048 private static final ClassDesc CD_Object_array = ConstantUtils.CD_Object_array;
1049 private static final MethodType INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1050 private static final MethodType REFLECT_INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object.class, Object[].class);
1051
1052 static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1053 // Code in the boot layer should now be careful while creating method handles or
1054 // functional interface instances created from method references to @CallerSensitive methods,
1055 // it needs to be ensured the handles or interface instances are kept safe and are not passed
1056 // from the boot layer to untrusted code.
1057 if (hostClass == null
1058 || (hostClass.isArray() ||
1059 hostClass.isPrimitive() ||
1060 hostClass.getName().startsWith("java.lang.invoke."))) {
1061 throw new InternalError(); // does not happen, and should not anyway
1062 }
1063
1064 MemberName member = mh.internalMemberName();
1065 if (member != null) {
1066 // Look up the CSM adapter method with the same method name
1067 // but with an additional caller class parameter. If present,
1068 // bind the adapter's method handle with the lookup class as
1069 // the caller class argument
1070 MemberName csmAdapter = IMPL_LOOKUP.resolveOrNull(member.getReferenceKind(),
1071 new MemberName(member.getDeclaringClass(),
1072 member.getName(),
1073 member.getMethodType().appendParameterTypes(Class.class),
1074 member.getReferenceKind()));
1075 if (csmAdapter != null) {
1076 assert !csmAdapter.isCallerSensitive();
1077 MethodHandle dmh = DirectMethodHandle.make(csmAdapter);
1078 dmh = MethodHandles.insertArguments(dmh, dmh.type().parameterCount() - 1, hostClass);
1079 dmh = new WrappedMember(dmh, mh.type(), member, mh.isInvokeSpecial(), hostClass);
1080 return dmh;
1081 }
1082 }
1083
1084 // If no adapter method for CSM with an additional Class parameter
1085 // is present, then inject an invoker class that is the caller
1086 // invoking the method handle of the CSM
1087 try {
1088 return bindCallerWithInjectedInvoker(mh, hostClass);
1089 } catch (ReflectiveOperationException ex) {
1090 throw uncaughtException(ex);
1091 }
1092 }
1093
1094 private static MethodHandle bindCallerWithInjectedInvoker(MethodHandle mh, Class<?> hostClass)
1095 throws ReflectiveOperationException
1096 {
1097 // For simplicity, convert mh to a varargs-like method.
1098 MethodHandle vamh = prepareForInvoker(mh);
1099 // Cache the result of makeInjectedInvoker once per argument class.
1100 MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass).invoker();
1101 return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
1102 }
1103
1104 private static Class<?> makeInjectedInvoker(Class<?> targetClass) {
1105 /*
1106 * The invoker class defined to the same class loader as the lookup class
1107 * but in an unnamed package so that the class bytes can be cached and
1108 * reused for any @CSM.
1109 *
1110 * @CSM must be public and exported if called by any module.
1111 */
1112 String name = targetClass.getName() + "$$InjectedInvoker";
1113 if (targetClass.isHidden()) {
1114 // use the original class name
1115 name = name.replace('/', '_');
1116 }
1117 name = name.replace('.', '/');
1118 Class<?> invokerClass = new Lookup(targetClass)
1119 .makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE, dumper(), NESTMATE_CLASS)
1120 .defineClass(true, targetClass);
1121 assert checkInjectedInvoker(targetClass, invokerClass);
1122 return invokerClass;
1123 }
1124
1125 private static ClassValue<InjectedInvokerHolder> CV_makeInjectedInvoker = new ClassValue<>() {
1126 @Override
1127 protected InjectedInvokerHolder computeValue(Class<?> hostClass) {
1128 return new InjectedInvokerHolder(makeInjectedInvoker(hostClass));
1129 }
1130 };
1131
1132 /*
1133 * Returns a method handle of an invoker class injected for reflection
1134 * implementation use with the following signature:
1135 * reflect_invoke_V(MethodHandle mh, Object target, Object[] args)
1136 *
1137 * Method::invoke on a caller-sensitive method will call
1138 * MethodAccessorImpl::invoke(Object, Object[]) through reflect_invoke_V
1139 * target.csm(args)
1140 * NativeMethodAccessorImpl::invoke(target, args)
1141 * MethodAccessImpl::invoke(target, args)
1142 * InjectedInvoker::reflect_invoke_V(vamh, target, args);
1143 * method::invoke(target, args)
1144 * p.Foo::m
1145 *
1146 * An injected invoker class is a hidden class which has the same
1147 * defining class loader, runtime package, and protection domain
1148 * as the given caller class.
1149 */
1150 static MethodHandle reflectiveInvoker(Class<?> caller) {
1151 return BindCaller.CV_makeInjectedInvoker.get(caller).reflectInvoker();
1152 }
1153
1154 @AOTSafeClassInitializer
1155 private static final class InjectedInvokerHolder {
1156 private final Class<?> invokerClass;
1157 // lazily resolved and cached DMH(s) of invoke_V methods
1158 private MethodHandle invoker;
1159 private MethodHandle reflectInvoker;
1160
1161 private InjectedInvokerHolder(Class<?> invokerClass) {
1162 this.invokerClass = invokerClass;
1163 }
1164
1165 private MethodHandle invoker() {
1166 var mh = invoker;
1167 if (mh == null) {
1168 try {
1169 invoker = mh = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1170 } catch (Error | RuntimeException ex) {
1171 throw ex;
1172 } catch (Throwable ex) {
1173 throw new InternalError(ex);
1174 }
1175 }
1176 return mh;
1177 }
1178
1179 private MethodHandle reflectInvoker() {
1180 var mh = reflectInvoker;
1181 if (mh == null) {
1182 try {
1183 reflectInvoker = mh = IMPL_LOOKUP.findStatic(invokerClass, "reflect_invoke_V", REFLECT_INVOKER_MT);
1184 } catch (Error | RuntimeException ex) {
1185 throw ex;
1186 } catch (Throwable ex) {
1187 throw new InternalError(ex);
1188 }
1189 }
1190 return mh;
1191 }
1192 }
1193
1194 // Adapt mh so that it can be called directly from an injected invoker:
1195 private static MethodHandle prepareForInvoker(MethodHandle mh) {
1196 mh = mh.asFixedArity();
1197 MethodType mt = mh.type();
1198 int arity = mt.parameterCount();
1199 MethodHandle vamh = mh.asType(mt.generic());
1200 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames
1201 vamh = vamh.asSpreader(Object[].class, arity);
1202 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames
1203 return vamh;
1204 }
1205
1206 // Undo the adapter effect of prepareForInvoker:
1207 private static MethodHandle restoreToType(MethodHandle vamh,
1208 MethodHandle original,
1209 Class<?> hostClass) {
1210 MethodType type = original.type();
1211 MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
1212 MemberName member = original.internalMemberName();
1213 mh = mh.asType(type);
1214 mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
1215 return mh;
1216 }
1217
1218 private static boolean checkInjectedInvoker(Class<?> hostClass, Class<?> invokerClass) {
1219 assert (hostClass.getClassLoader() == invokerClass.getClassLoader()) : hostClass.getName()+" (CL)";
1220 assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
1221 try {
1222 // Test the invoker to ensure that it really injects into the right place.
1223 MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1224 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
1225 return (boolean)invoker.invoke(vamh, new Object[]{ invokerClass });
1226 } catch (Error|RuntimeException ex) {
1227 throw ex;
1228 } catch (Throwable ex) {
1229 throw new InternalError(ex);
1230 }
1231 }
1232
1233 private static final MethodHandle MH_checkCallerClass;
1234 static {
1235 final Class<?> THIS_CLASS = BindCaller.class;
1236 assert(checkCallerClass(THIS_CLASS));
1237 try {
1238 MH_checkCallerClass = IMPL_LOOKUP
1239 .findStatic(THIS_CLASS, "checkCallerClass",
1240 MethodType.methodType(boolean.class, Class.class));
1241 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS));
1242 } catch (Throwable ex) {
1243 throw new InternalError(ex);
1244 }
1245 }
1246
1247 @CallerSensitive
1248 @ForceInline // to ensure Reflection.getCallerClass optimization
1249 private static boolean checkCallerClass(Class<?> expected) {
1250 // This method is called via MH_checkCallerClass and so it's correct to ask for the immediate caller here.
1251 Class<?> actual = Reflection.getCallerClass();
1252 if (actual != expected)
1253 throw new InternalError("found " + actual.getName() + ", expected " + expected.getName());
1254 return true;
1255 }
1256
1257 private static final byte[] INJECTED_INVOKER_TEMPLATE = generateInvokerTemplate();
1258
1259 /** Produces byte code for a class that is used as an injected invoker. */
1260 private static byte[] generateInvokerTemplate() {
1261 // private static class InjectedInvoker {
1262 // /* this is used to wrap DMH(s) of caller-sensitive methods */
1263 // @Hidden
1264 // static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
1265 // return vamh.invokeExact(args);
1266 // }
1267 // /* this is used in caller-sensitive reflective method accessor */
1268 // @Hidden
1269 // static Object reflect_invoke_V(MethodHandle vamh, Object target, Object[] args) throws Throwable {
1270 // return vamh.invokeExact(target, args);
1271 // }
1272 // }
1273 // }
1274 return ClassFile.of().build(ClassOrInterfaceDescImpl.ofValidated("LInjectedInvoker;"), clb -> clb
1275 .withFlags(ACC_PRIVATE | ACC_SUPER)
1276 .withMethodBody(
1277 "invoke_V",
1278 MethodTypeDescImpl.ofValidated(CD_Object, CD_MethodHandle, CD_Object_array),
1279 ACC_STATIC,
1280 cob -> cob.aload(0)
1281 .aload(1)
1282 .invokevirtual(CD_MethodHandle, "invokeExact", MethodTypeDescImpl.ofValidated(CD_Object, CD_Object_array))
1283 .areturn())
1284 .withMethodBody(
1285 "reflect_invoke_V",
1286 MethodTypeDescImpl.ofValidated(CD_Object, CD_MethodHandle, CD_Object, CD_Object_array),
1287 ACC_STATIC,
1288 cob -> cob.aload(0)
1289 .aload(1)
1290 .aload(2)
1291 .invokevirtual(CD_MethodHandle, "invokeExact", MethodTypeDescImpl.ofValidated(CD_Object, CD_Object, CD_Object_array))
1292 .areturn()));
1293 }
1294 }
1295
1296 /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
1297 @AOTSafeClassInitializer
1298 static final class WrappedMember extends DelegatingMethodHandle {
1299 private final MethodHandle target;
1300 private final MemberName member;
1301 private final Class<?> callerClass;
1302 private final boolean isInvokeSpecial;
1303
1304 private WrappedMember(MethodHandle target, MethodType type,
1305 MemberName member, boolean isInvokeSpecial,
1306 Class<?> callerClass) {
1307 super(type, target);
1308 this.target = target;
1309 this.member = member;
1310 this.callerClass = callerClass;
1311 this.isInvokeSpecial = isInvokeSpecial;
1312 }
1313
1314 @Override
1315 MemberName internalMemberName() {
1316 return member;
1317 }
1318 @Override
1319 Class<?> internalCallerClass() {
1320 return callerClass;
1321 }
1322 @Override
1323 boolean isInvokeSpecial() {
1324 return isInvokeSpecial;
1325 }
1326 @Override
1327 protected MethodHandle getTarget() {
1328 return target;
1329 }
1330 @Override
1331 public MethodHandle asTypeUncached(MethodType newType) {
1332 // This MH is an alias for target, except for the MemberName
1333 // Drop the MemberName if there is any conversion.
1334 return target.asType(newType);
1335 }
1336 }
1337
1338 static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1339 if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
1340 return target;
1341 return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
1342 }
1343
1344 /** Intrinsic IDs */
1345 /*non-public*/
1346 enum Intrinsic {
1347 SELECT_ALTERNATIVE,
1348 GUARD_WITH_CATCH,
1349 TRY_FINALLY,
1350 TABLE_SWITCH,
1351 LOOP,
1352 ARRAY_LOAD,
1353 ARRAY_STORE,
1354 ARRAY_LENGTH,
1355 IDENTITY,
1356 NONE // no intrinsic associated
1357 }
1358
1359 /** Mark arbitrary method handle as intrinsic.
1360 * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
1361 @AOTSafeClassInitializer
1362 static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
1363 private final MethodHandle target;
1364 private final Intrinsic intrinsicName;
1365 private final Object intrinsicData;
1366
1367 IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
1368 this(target, intrinsicName, null);
1369 }
1370
1371 IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1372 super(target.type(), target);
1373 this.target = target;
1374 this.intrinsicName = intrinsicName;
1375 this.intrinsicData = intrinsicData;
1376 }
1377
1378 @Override
1379 protected MethodHandle getTarget() {
1380 return target;
1381 }
1382
1383 @Override
1384 Intrinsic intrinsicName() {
1385 return intrinsicName;
1386 }
1387
1388 @Override
1389 Object intrinsicData() {
1390 return intrinsicData;
1391 }
1392
1393 @Override
1394 public MethodHandle asTypeUncached(MethodType newType) {
1395 // This MH is an alias for target, except for the intrinsic name
1396 // Drop the name if there is any conversion.
1397 return target.asType(newType);
1398 }
1399
1400 @Override
1401 String internalProperties() {
1402 return super.internalProperties() +
1403 "\n& Intrinsic="+intrinsicName;
1404 }
1405
1406 @Override
1407 public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
1408 if (intrinsicName == Intrinsic.IDENTITY) {
1409 MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength);
1410 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
1411 return newArray.asType(resultType);
1412 }
1413 return super.asCollector(arrayType, arrayLength);
1414 }
1415 }
1416
1417 static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
1418 return makeIntrinsic(target, intrinsicName, null);
1419 }
1420
1421 static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1422 if (intrinsicName == target.intrinsicName())
1423 return target;
1424 return new IntrinsicMethodHandle(target, intrinsicName, intrinsicData);
1425 }
1426
1427 static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
1428 return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
1429 }
1430
1431 private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];
1432
1433 /** Return a method handle that takes the indicated number of Object
1434 * arguments and returns an Object array of them, as if for varargs.
1435 */
1436 static MethodHandle varargsArray(int nargs) {
1437 MethodHandle mh = ARRAYS[nargs];
1438 if (mh != null) {
1439 return mh;
1440 }
1441 mh = makeCollector(Object[].class, nargs);
1442 assert(assertCorrectArity(mh, nargs));
1443 return ARRAYS[nargs] = mh;
1444 }
1445
1446 /** Return a method handle that takes the indicated number of
1447 * typed arguments and returns an array of them.
1448 * The type argument is the array type.
1449 */
1450 static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
1451 Class<?> elemType = arrayType.getComponentType();
1452 if (elemType == null) throw new IllegalArgumentException("not an array: "+arrayType);
1453 if (nargs >= MAX_JVM_ARITY/2 - 1) {
1454 int slots = nargs;
1455 final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1; // 1 for receiver MH
1456 if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
1457 slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
1458 if (slots > MAX_ARRAY_SLOTS)
1459 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
1460 }
1461 if (elemType == Object.class)
1462 return varargsArray(nargs);
1463 // other cases: primitive arrays, subtypes of Object[]
1464 MethodHandle cache[] = Makers.TYPED_COLLECTORS.get(elemType);
1465 MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
1466 if (mh != null) return mh;
1467 mh = makeCollector(arrayType, nargs);
1468 assert(assertCorrectArity(mh, nargs));
1469 if (nargs < cache.length)
1470 cache[nargs] = mh;
1471 return mh;
1472 }
1473
1474 private static boolean assertCorrectArity(MethodHandle mh, int arity) {
1475 assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
1476 return true;
1477 }
1478
1479 static final int MAX_JVM_ARITY = 255; // limit imposed by the JVM
1480
1481 /*non-public*/
1482 static void assertSame(Object mh1, Object mh2) {
1483 if (mh1 != mh2) {
1484 String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
1485 mh1, ((MethodHandle)mh1).form,
1486 mh2, ((MethodHandle)mh2).form);
1487 throw newInternalError(msg);
1488 }
1489 }
1490
1491 // Local constant functions:
1492
1493 /* non-public */
1494 static final byte NF_checkSpreadArgument = 0,
1495 NF_guardWithCatch = 1,
1496 NF_throwException = 2,
1497 NF_tryFinally = 3,
1498 NF_loop = 4,
1499 NF_profileBoolean = 5,
1500 NF_tableSwitch = 6,
1501 NF_LIMIT = 7;
1502
1503 private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
1504
1505 static NamedFunction getFunction(byte func) {
1506 NamedFunction nf = NFS[func];
1507 if (nf != null) {
1508 return nf;
1509 }
1510 return NFS[func] = createFunction(func);
1511 }
1512
1513 private static NamedFunction createFunction(byte func) {
1514 try {
1515 return switch (func) {
1516 case NF_checkSpreadArgument -> new NamedFunction(MethodHandleImpl.class
1517 .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
1518 case NF_guardWithCatch -> new NamedFunction(MethodHandleImpl.class
1519 .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
1520 MethodHandle.class, Object[].class));
1521 case NF_tryFinally -> new NamedFunction(MethodHandleImpl.class
1522 .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class));
1523 case NF_loop -> new NamedFunction(MethodHandleImpl.class
1524 .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class));
1525 case NF_throwException -> new NamedFunction(MethodHandleImpl.class
1526 .getDeclaredMethod("throwException", Throwable.class));
1527 case NF_profileBoolean -> new NamedFunction(MethodHandleImpl.class
1528 .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
1529 case NF_tableSwitch -> new NamedFunction(MethodHandleImpl.class
1530 .getDeclaredMethod("tableSwitch", int.class, MethodHandle.class, CasesHolder.class, Object[].class));
1531 default -> throw new InternalError("Undefined function: " + func);
1532 };
1533 } catch (ReflectiveOperationException ex) {
1534 throw newInternalError(ex);
1535 }
1536 }
1537
1538 static {
1539 SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
1540 @Override
1541 public Class<?> getDeclaringClass(Object rmname) {
1542 ResolvedMethodName method = (ResolvedMethodName)rmname;
1543 return method.declaringClass();
1544 }
1545
1546 @Override
1547 public MethodType getMethodType(String descriptor, ClassLoader loader) {
1548 return MethodType.fromDescriptor(descriptor, loader);
1549 }
1550
1551 public boolean isCallerSensitive(int flags) {
1552 return (flags & MN_CALLER_SENSITIVE) == MN_CALLER_SENSITIVE;
1553 }
1554
1555 public boolean isHiddenMember(int flags) {
1556 return (flags & MN_HIDDEN_MEMBER) == MN_HIDDEN_MEMBER;
1557 }
1558
1559 @Override
1560 public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1561 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1562 }
1563
1564 @Override
1565 public VarHandle memorySegmentViewHandle(Class<?> carrier, MemoryLayout enclosing, long alignmentMask, ByteOrder order, boolean constantOffset, long offset) {
1566 return VarHandles.memorySegmentViewHandle(carrier, enclosing, alignmentMask, constantOffset, offset, order);
1567 }
1568
1569 @Override
1570 public MethodHandle nativeMethodHandle(NativeEntryPoint nep) {
1571 return NativeMethodHandle.make(nep);
1572 }
1573
1574 @Override
1575 public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1576 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1577 }
1578
1579 @Override
1580 public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1581 return VarHandles.filterCoordinates(target, pos, filters);
1582 }
1583
1584 @Override
1585 public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1586 return VarHandles.dropCoordinates(target, pos, valueTypes);
1587 }
1588
1589 @Override
1590 public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1591 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1592 }
1593
1594 @Override
1595 public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1596 return VarHandles.collectCoordinates(target, pos, filter);
1597 }
1598
1599 @Override
1600 public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1601 return VarHandles.insertCoordinates(target, pos, values);
1602 }
1603
1604
1605 @Override
1606 public MethodHandle unreflectConstructor(Constructor<?> ctor) throws IllegalAccessException {
1607 return IMPL_LOOKUP.unreflectConstructor(ctor);
1608 }
1609
1610 @Override
1611 public MethodHandle unreflectField(Field field, boolean isSetter) throws IllegalAccessException {
1612 return isSetter ? IMPL_LOOKUP.unreflectSetter(field) : IMPL_LOOKUP.unreflectGetter(field);
1613 }
1614
1615 @Override
1616 public MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1617 try {
1618 return IMPL_LOOKUP.findVirtual(defc, name, type);
1619 } catch (NoSuchMethodException e) {
1620 return null;
1621 }
1622 }
1623
1624 @Override
1625 public MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1626 try {
1627 return IMPL_LOOKUP.findStatic(defc, name, type);
1628 } catch (NoSuchMethodException e) {
1629 return null;
1630 }
1631 }
1632
1633 @Override
1634 public MethodHandle reflectiveInvoker(Class<?> caller) {
1635 Objects.requireNonNull(caller);
1636 return BindCaller.reflectiveInvoker(caller);
1637 }
1638
1639 @Override
1640 public Class<?>[] exceptionTypes(MethodHandle handle) {
1641 return VarHandles.exceptionTypes(handle);
1642 }
1643
1644 @Override
1645 public MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException {
1646 return IMPL_LOOKUP.serializableConstructor(decl, ctorToCall);
1647 }
1648
1649 @Override
1650 public CallSite metafactoryInternal(Lookup caller, String interfaceMethodName, MethodType factoryType,
1651 MethodType interfaceMethodType, MethodHandle implementation,
1652 MethodType dynamicMethodType, Function<ClassBuilder, Object> finisher) throws LambdaConversionException {
1653 return LambdaMetafactory.metafactoryInternal(caller, interfaceMethodName, factoryType,
1654 interfaceMethodType, implementation, dynamicMethodType, finisher);
1655 }
1656
1657 @Override
1658 public CallSite altMetafactoryInternal(Lookup caller, String interfaceMethodName, MethodType factoryType,
1659 Function<ClassBuilder, Object> finisher, Object... args) throws LambdaConversionException {
1660 return LambdaMetafactory.altMetafactoryInternal(caller, interfaceMethodName, factoryType, finisher, args);
1661 }
1662 });
1663 }
1664
1665 /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1666 private static MethodHandle unboxResultHandle(Class<?> returnType) {
1667 if (returnType.isPrimitive()) {
1668 if (returnType == void.class) {
1669 return ValueConversions.ignore();
1670 } else {
1671 Wrapper w = Wrapper.forPrimitiveType(returnType);
1672 return ValueConversions.unboxExact(w);
1673 }
1674 } else {
1675 return MethodHandles.identity(Object.class);
1676 }
1677 }
1678
1679 /**
1680 * Assembles a loop method handle from the given handles and type information.
1681 *
1682 * @param tloop the return type of the loop.
1683 * @param targs types of the arguments to be passed to the loop.
1684 * @param init sanitized array of initializers for loop-local variables.
1685 * @param step sanitized array of loop bodies.
1686 * @param pred sanitized array of predicates.
1687 * @param fini sanitized array of loop finalizers.
1688 *
1689 * @return a handle that, when invoked, will execute the loop.
1690 */
1691 static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step,
1692 List<MethodHandle> pred, List<MethodHandle> fini) {
1693 MethodType type = MethodType.methodType(tloop, targs);
1694 BasicType[] initClauseTypes =
1695 init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new);
1696 LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
1697
1698 // Prepare auxiliary method handles used during LambdaForm interpretation.
1699 // Box arguments and wrap them into Object[]: ValueConversions.array().
1700 MethodType varargsType = type.changeReturnType(Object[].class);
1701 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1702 MethodHandle unboxResult = unboxResultHandle(tloop);
1703
1704 LoopClauses clauseData =
1705 new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)});
1706 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1707 BoundMethodHandle mh;
1708 try {
1709 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData,
1710 (Object) collectArgs, (Object) unboxResult);
1711 } catch (Throwable ex) {
1712 throw uncaughtException(ex);
1713 }
1714 assert(mh.type() == type);
1715 return mh;
1716 }
1717
1718 private static MethodHandle[] toArray(List<MethodHandle> l) {
1719 return l.toArray(new MethodHandle[0]);
1720 }
1721
1722 /**
1723 * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are
1724 * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one
1725 * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by
1726 * {@code t12}):
1727 * <blockquote><pre>{@code
1728 * loop=Lambda(a0:L,a1:L)=>{
1729 * t2:L=BoundMethodHandle$Species_L3.argL0(a0:L); // LoopClauses holding init, step, pred, fini handles
1730 * t3:L=BoundMethodHandle$Species_L3.argL1(a0:L); // helper handle to box the arguments into an Object[]
1731 * t4:L=BoundMethodHandle$Species_L3.argL2(a0:L); // helper handle to unbox the result
1732 * t5:L=MethodHandle.invokeBasic(t3:L,a1:L); // box the arguments into an Object[]
1733 * t6:L=MethodHandleImpl.loop(null,t2:L,t3:L); // call the loop executor
1734 * t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L} // unbox the result; return the result
1735 * }</pre></blockquote>
1736 * <p>
1737 * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method
1738 * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps
1739 * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary
1740 * ({@code ValueConversions.unbox()}).
1741 * <p>
1742 * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1743 * forms among loop combinators with the same basic type.
1744 * <p>
1745 * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to
1746 * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is
1747 * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int)
1748 * bytecode generation}.
1749 */
1750 private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
1751 final int THIS_MH = 0; // the BMH_LLL
1752 final int ARG_BASE = 1; // start of incoming arguments
1753 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
1754
1755 int nameCursor = ARG_LIMIT;
1756 final int GET_CLAUSE_DATA = nameCursor++;
1757 final int GET_COLLECT_ARGS = nameCursor++;
1758 final int GET_UNBOX_RESULT = nameCursor++;
1759 final int BOXED_ARGS = nameCursor++;
1760 final int LOOP = nameCursor++;
1761 final int UNBOX_RESULT = nameCursor++;
1762
1763 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
1764 if (lform == null) {
1765 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
1766
1767 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1768 names[THIS_MH] = names[THIS_MH].withConstraint(data);
1769 names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]);
1770 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]);
1771 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]);
1772
1773 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
1774 MethodType collectArgsType = basicType.changeReturnType(Object.class);
1775 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
1776 Object[] args = new Object[invokeBasic.type().parameterCount()];
1777 args[0] = names[GET_COLLECT_ARGS];
1778 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
1779 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.LOOP)), args);
1780
1781 // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L);
1782 Object[] lArgs =
1783 new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor
1784 names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
1785 names[LOOP] = new Name(getFunction(NF_loop), lArgs);
1786
1787 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
1788 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
1789 Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]};
1790 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
1791
1792 lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
1793 LambdaForm.create(basicType.parameterCount() + 1, names, Kind.LOOP));
1794 }
1795
1796 // BOXED_ARGS is the index into the names array where the loop idiom starts
1797 return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
1798 }
1799
1800 @AOTSafeClassInitializer
1801 static class LoopClauses {
1802 @Stable final MethodHandle[][] clauses;
1803 LoopClauses(MethodHandle[][] clauses) {
1804 assert clauses.length == 4;
1805 this.clauses = clauses;
1806 }
1807 @Override
1808 public String toString() {
1809 StringBuilder sb = new StringBuilder("LoopClauses -- ");
1810 for (int i = 0; i < 4; ++i) {
1811 if (i > 0) {
1812 sb.append(" ");
1813 }
1814 sb.append('<').append(i).append(">: ");
1815 MethodHandle[] hs = clauses[i];
1816 for (int j = 0; j < hs.length; ++j) {
1817 if (j > 0) {
1818 sb.append(" ");
1819 }
1820 sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
1821 }
1822 }
1823 sb.append(" --\n");
1824 return sb.toString();
1825 }
1826 }
1827
1828 /**
1829 * Intrinsified during LambdaForm compilation
1830 * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
1831 */
1832 @Hidden
1833 static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable {
1834 final MethodHandle[] init = clauseData.clauses[0];
1835 final MethodHandle[] step = clauseData.clauses[1];
1836 final MethodHandle[] pred = clauseData.clauses[2];
1837 final MethodHandle[] fini = clauseData.clauses[3];
1838 int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count();
1839 int nArgs = init[0].type().parameterCount();
1840 Object[] varsAndArgs = new Object[varSize + nArgs];
1841 for (int i = 0, v = 0; i < init.length; ++i) {
1842 MethodHandle ih = init[i];
1843 if (ih.type().returnType() == void.class) {
1844 ih.invokeWithArguments(av);
1845 } else {
1846 varsAndArgs[v++] = ih.invokeWithArguments(av);
1847 }
1848 }
1849 System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
1850 final int nSteps = step.length;
1851 for (; ; ) {
1852 for (int i = 0, v = 0; i < nSteps; ++i) {
1853 MethodHandle p = pred[i];
1854 MethodHandle s = step[i];
1855 MethodHandle f = fini[i];
1856 if (s.type().returnType() == void.class) {
1857 s.invokeWithArguments(varsAndArgs);
1858 } else {
1859 varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
1860 }
1861 if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
1862 return f.invokeWithArguments(varsAndArgs);
1863 }
1864 }
1865 }
1866 }
1867
1868 /**
1869 * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1870 * MethodHandle) counting loops}.
1871 *
1872 * @param limit the upper bound of the parameter, statically bound at loop creation time.
1873 * @param counter the counter parameter, passed in during loop execution.
1874 *
1875 * @return whether the counter has reached the limit.
1876 */
1877 static boolean countedLoopPredicate(int limit, int counter) {
1878 return counter < limit;
1879 }
1880
1881 /**
1882 * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1883 * MethodHandle) counting loops} to increment the counter.
1884 *
1885 * @param limit the upper bound of the loop counter (ignored).
1886 * @param counter the loop counter.
1887 *
1888 * @return the loop counter incremented by 1.
1889 */
1890 static int countedLoopStep(int limit, int counter) {
1891 return counter + 1;
1892 }
1893
1894 /**
1895 * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1896 *
1897 * @param it the {@link Iterable} over which the loop iterates.
1898 *
1899 * @return an {@link Iterator} over the argument's elements.
1900 */
1901 static Iterator<?> initIterator(Iterable<?> it) {
1902 return it.iterator();
1903 }
1904
1905 /**
1906 * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1907 *
1908 * @param it the iterator to be checked.
1909 *
1910 * @return {@code true} iff there are more elements to iterate over.
1911 */
1912 static boolean iteratePredicate(Iterator<?> it) {
1913 return it.hasNext();
1914 }
1915
1916 /**
1917 * This method is bound as the step for retrieving the current value from the iterator in {@linkplain
1918 * MethodHandles#iteratedLoop iterating loops}.
1919 *
1920 * @param it the iterator.
1921 *
1922 * @return the next element from the iterator.
1923 */
1924 static Object iterateNext(Iterator<?> it) {
1925 return it.next();
1926 }
1927
1928 /**
1929 * Makes a {@code try-finally} handle that conforms to the type constraints.
1930 *
1931 * @param target the target to execute in a {@code try-finally} block.
1932 * @param cleanup the cleanup to execute in the {@code finally} block.
1933 * @param rtype the result type of the entire construct.
1934 * @param argTypes the types of the arguments.
1935 *
1936 * @return a handle on the constructed {@code try-finally} block.
1937 */
1938 static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, Class<?>[] argTypes) {
1939 MethodType type = MethodType.methodType(rtype, argTypes);
1940 LambdaForm form = makeTryFinallyForm(type.basicType());
1941
1942 // Prepare auxiliary method handles used during LambdaForm interpretation.
1943 // Box arguments and wrap them into Object[]: ValueConversions.array().
1944 MethodType varargsType = type.changeReturnType(Object[].class);
1945 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1946 MethodHandle unboxResult = unboxResultHandle(rtype);
1947
1948 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
1949 BoundMethodHandle mh;
1950 try {
1951 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup,
1952 (Object) collectArgs, (Object) unboxResult);
1953 } catch (Throwable ex) {
1954 throw uncaughtException(ex);
1955 }
1956 assert(mh.type() == type);
1957 return mh;
1958 }
1959
1960 /**
1961 * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in
1962 * {@code a1}, and a reference return type, with the return value represented by {@code t8}):
1963 * <blockquote><pre>{@code
1964 * tryFinally=Lambda(a0:L,a1:L)=>{
1965 * t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L); // target method handle
1966 * t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L); // cleanup method handle
1967 * t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L); // helper handle to box the arguments into an Object[]
1968 * t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L); // helper handle to unbox the result
1969 * t6:L=MethodHandle.invokeBasic(t4:L,a1:L); // box the arguments into an Object[]
1970 * t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor
1971 * t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L} // unbox the result; return the result
1972 * }</pre></blockquote>
1973 * <p>
1974 * {@code argL0} and {@code argL1} are the target and cleanup method handles.
1975 * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into
1976 * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary
1977 * ({@code ValueConversions.unbox()}).
1978 * <p>
1979 * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1980 * forms among tryFinally combinators with the same basic type.
1981 */
1982 private static LambdaForm makeTryFinallyForm(MethodType basicType) {
1983 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
1984 if (lform != null) {
1985 return lform;
1986 }
1987 final int THIS_MH = 0; // the BMH_LLLL
1988 final int ARG_BASE = 1; // start of incoming arguments
1989 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
1990
1991 int nameCursor = ARG_LIMIT;
1992 final int GET_TARGET = nameCursor++;
1993 final int GET_CLEANUP = nameCursor++;
1994 final int GET_COLLECT_ARGS = nameCursor++;
1995 final int GET_UNBOX_RESULT = nameCursor++;
1996 final int BOXED_ARGS = nameCursor++;
1997 final int TRY_FINALLY = nameCursor++;
1998 final int UNBOX_RESULT = nameCursor++;
1999
2000 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2001
2002 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2003 names[THIS_MH] = names[THIS_MH].withConstraint(data);
2004 names[GET_TARGET] = new Name(data.getterFunction(0), names[THIS_MH]);
2005 names[GET_CLEANUP] = new Name(data.getterFunction(1), names[THIS_MH]);
2006 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]);
2007 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]);
2008
2009 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
2010 MethodType collectArgsType = basicType.changeReturnType(Object.class);
2011 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2012 Object[] args = new Object[invokeBasic.type().parameterCount()];
2013 args[0] = names[GET_COLLECT_ARGS];
2014 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
2015 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY)), args);
2016
2017 // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L);
2018 Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]};
2019 names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs);
2020
2021 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
2022 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2023 Object[] unboxArgs = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]};
2024 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
2025
2026 lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TRY_FINALLY);
2027
2028 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
2029 }
2030
2031 /**
2032 * Intrinsified during LambdaForm compilation
2033 * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
2034 */
2035 @Hidden
2036 static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable {
2037 Throwable t = null;
2038 Object r = null;
2039 try {
2040 r = target.invokeWithArguments(av);
2041 } catch (Throwable thrown) {
2042 t = thrown;
2043 throw t;
2044 } finally {
2045 Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r);
2046 r = cleanup.invokeWithArguments(args);
2047 }
2048 return r;
2049 }
2050
2051 // see varargsArray method for chaching/package-private version of this
2052 private static MethodHandle makeCollector(Class<?> arrayType, int parameterCount) {
2053 MethodType type = MethodType.methodType(arrayType, Collections.nCopies(parameterCount, arrayType.componentType()));
2054 MethodHandle newArray = MethodHandles.arrayConstructor(arrayType);
2055
2056 LambdaForm form = makeCollectorForm(type.basicType(), arrayType);
2057
2058 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2059 BoundMethodHandle mh;
2060 try {
2061 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) newArray);
2062 } catch (Throwable ex) {
2063 throw uncaughtException(ex);
2064 }
2065 assert(mh.type() == type);
2066 return mh;
2067 }
2068
2069 private static LambdaForm makeCollectorForm(MethodType basicType, Class<?> arrayType) {
2070 int parameterCount = basicType.parameterCount();
2071
2072 // Only share the lambda form for empty arrays and reference types.
2073 // Sharing based on the basic type alone doesn't work because
2074 // we need a separate lambda form for byte/short/char/int which
2075 // are all erased to int otherwise.
2076 // Other caching for primitive types happens at the MethodHandle level (see varargsArray).
2077 boolean isReferenceType = !arrayType.componentType().isPrimitive();
2078 boolean isSharedLambdaForm = parameterCount == 0 || isReferenceType;
2079 if (isSharedLambdaForm) {
2080 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_COLLECTOR);
2081 if (lform != null) {
2082 return lform;
2083 }
2084 }
2085
2086 // use erased accessor for reference types
2087 MethodHandle storeFunc = isReferenceType
2088 ? ArrayAccessor.OBJECT_ARRAY_SETTER
2089 : makeArrayElementAccessor(arrayType, ArrayAccess.SET);
2090
2091 final int THIS_MH = 0; // the BMH_L
2092 final int ARG_BASE = 1; // start of incoming arguments
2093 final int ARG_LIMIT = ARG_BASE + parameterCount;
2094
2095 int nameCursor = ARG_LIMIT;
2096 final int GET_NEW_ARRAY = nameCursor++;
2097 final int CALL_NEW_ARRAY = nameCursor++;
2098 final int STORE_ELEMENT_BASE = nameCursor;
2099 final int STORE_ELEMENT_LIMIT = STORE_ELEMENT_BASE + parameterCount;
2100 nameCursor = STORE_ELEMENT_LIMIT;
2101
2102 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2103
2104 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2105 names[THIS_MH] = names[THIS_MH].withConstraint(data);
2106 names[GET_NEW_ARRAY] = new Name(data.getterFunction(0), names[THIS_MH]);
2107
2108 MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(Object.class, int.class));
2109 names[CALL_NEW_ARRAY] = new Name(new NamedFunction(invokeBasic), names[GET_NEW_ARRAY], parameterCount);
2110 for (int storeIndex = 0,
2111 storeNameCursor = STORE_ELEMENT_BASE,
2112 argCursor = ARG_BASE;
2113 storeNameCursor < STORE_ELEMENT_LIMIT;
2114 storeIndex++, storeNameCursor++, argCursor++){
2115
2116 names[storeNameCursor] = new Name(new NamedFunction(makeIntrinsic(storeFunc, Intrinsic.ARRAY_STORE)),
2117 names[CALL_NEW_ARRAY], storeIndex, names[argCursor]);
2118 }
2119
2120 LambdaForm lform = LambdaForm.create(basicType.parameterCount() + 1, names, CALL_NEW_ARRAY, Kind.COLLECTOR);
2121 if (isSharedLambdaForm) {
2122 lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_COLLECTOR, lform);
2123 }
2124 return lform;
2125 }
2126
2127 // use a wrapper because we need this array to be @Stable
2128 @AOTSafeClassInitializer
2129 static class CasesHolder {
2130 @Stable
2131 final MethodHandle[] cases;
2132
2133 public CasesHolder(MethodHandle[] cases) {
2134 this.cases = cases;
2135 }
2136 }
2137
2138 static MethodHandle makeTableSwitch(MethodType type, MethodHandle defaultCase, MethodHandle[] caseActions) {
2139 MethodType varargsType = type.changeReturnType(Object[].class);
2140 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
2141
2142 MethodHandle unboxResult = unboxResultHandle(type.returnType());
2143
2144 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2145 LambdaForm form = makeTableSwitchForm(type.basicType(), data, caseActions.length);
2146 BoundMethodHandle mh;
2147 CasesHolder caseHolder = new CasesHolder(caseActions);
2148 try {
2149 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) defaultCase, (Object) collectArgs,
2150 (Object) unboxResult, (Object) caseHolder);
2151 } catch (Throwable ex) {
2152 throw uncaughtException(ex);
2153 }
2154 assert(mh.type() == type);
2155 return mh;
2156 }
2157
2158 @AOTSafeClassInitializer
2159 private static class TableSwitchCacheKey {
2160 private static final Map<TableSwitchCacheKey, LambdaForm> CACHE = new ConcurrentHashMap<>();
2161
2162 private final MethodType basicType;
2163 private final int numberOfCases;
2164
2165 public TableSwitchCacheKey(MethodType basicType, int numberOfCases) {
2166 this.basicType = basicType;
2167 this.numberOfCases = numberOfCases;
2168 }
2169
2170 @Override
2171 public boolean equals(Object o) {
2172 if (this == o) return true;
2173 if (o == null || getClass() != o.getClass()) return false;
2174 TableSwitchCacheKey that = (TableSwitchCacheKey) o;
2175 return numberOfCases == that.numberOfCases && Objects.equals(basicType, that.basicType);
2176 }
2177 @Override
2178 public int hashCode() {
2179 return Objects.hash(basicType, numberOfCases);
2180 }
2181 }
2182
2183 private static LambdaForm makeTableSwitchForm(MethodType basicType, BoundMethodHandle.SpeciesData data,
2184 int numCases) {
2185 // We need to cache based on the basic type X number of cases,
2186 // since the number of cases is used when generating bytecode.
2187 // This also means that we can't use the cache in MethodTypeForm,
2188 // which only uses the basic type as a key.
2189 TableSwitchCacheKey key = new TableSwitchCacheKey(basicType, numCases);
2190 LambdaForm lform = TableSwitchCacheKey.CACHE.get(key);
2191 if (lform != null) {
2192 return lform;
2193 }
2194
2195 final int THIS_MH = 0;
2196 final int ARG_BASE = 1; // start of incoming arguments
2197 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
2198 final int ARG_SWITCH_ON = ARG_BASE;
2199 assert ARG_SWITCH_ON < ARG_LIMIT;
2200
2201 int nameCursor = ARG_LIMIT;
2202 final int GET_COLLECT_ARGS = nameCursor++;
2203 final int GET_DEFAULT_CASE = nameCursor++;
2204 final int GET_UNBOX_RESULT = nameCursor++;
2205 final int GET_CASES = nameCursor++;
2206 final int BOXED_ARGS = nameCursor++;
2207 final int TABLE_SWITCH = nameCursor++;
2208 final int UNBOXED_RESULT = nameCursor++;
2209
2210 int fieldCursor = 0;
2211 final int FIELD_DEFAULT_CASE = fieldCursor++;
2212 final int FIELD_COLLECT_ARGS = fieldCursor++;
2213 final int FIELD_UNBOX_RESULT = fieldCursor++;
2214 final int FIELD_CASES = fieldCursor++;
2215
2216 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2217
2218 names[THIS_MH] = names[THIS_MH].withConstraint(data);
2219 names[GET_DEFAULT_CASE] = new Name(data.getterFunction(FIELD_DEFAULT_CASE), names[THIS_MH]);
2220 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(FIELD_COLLECT_ARGS), names[THIS_MH]);
2221 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(FIELD_UNBOX_RESULT), names[THIS_MH]);
2222 names[GET_CASES] = new Name(data.getterFunction(FIELD_CASES), names[THIS_MH]);
2223
2224 {
2225 MethodType collectArgsType = basicType.changeReturnType(Object.class);
2226 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2227 Object[] args = new Object[invokeBasic.type().parameterCount()];
2228 args[0] = names[GET_COLLECT_ARGS];
2229 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
2230 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TABLE_SWITCH, numCases)), args);
2231 }
2232
2233 {
2234 Object[] tfArgs = new Object[]{
2235 names[ARG_SWITCH_ON], names[GET_DEFAULT_CASE], names[GET_CASES], names[BOXED_ARGS]};
2236 names[TABLE_SWITCH] = new Name(getFunction(NF_tableSwitch), tfArgs);
2237 }
2238
2239 {
2240 MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2241 Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[TABLE_SWITCH]};
2242 names[UNBOXED_RESULT] = new Name(invokeBasic, unboxArgs);
2243 }
2244
2245 lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TABLE_SWITCH);
2246 LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2247 return prev != null ? prev : lform;
2248 }
2249
2250 @Hidden
2251 static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2252 MethodHandle[] caseActions = holder.cases;
2253 MethodHandle selectedCase;
2254 if (input < 0 || input >= caseActions.length) {
2255 selectedCase = defaultCase;
2256 } else {
2257 selectedCase = caseActions[input];
2258 }
2259 return selectedCase.invokeWithArguments(args);
2260 }
2261
2262 // type is validated, value is not
2263 static MethodHandle makeConstantReturning(Class<?> type, Object value) {
2264 var callType = MethodType.methodType(type);
2265 var basicType = BasicType.basicType(type);
2266 var form = constantForm(basicType);
2267
2268 if (type.isPrimitive()) {
2269 assert type != void.class;
2270 var wrapper = Wrapper.forPrimitiveType(type);
2271 var v = wrapper.convert(value, type); // throws CCE
2272 return switch (wrapper) {
2273 case INT -> BoundMethodHandle.bindSingleI(callType, form, (int) v);
2274 case LONG -> BoundMethodHandle.bindSingleJ(callType, form, (long) v);
2275 case FLOAT -> BoundMethodHandle.bindSingleF(callType, form, (float) v);
2276 case DOUBLE -> BoundMethodHandle.bindSingleD(callType, form, (double) v);
2277 default -> BoundMethodHandle.bindSingleI(callType, form, ValueConversions.widenSubword(v));
2278 };
2279 }
2280
2281 var v = type.cast(value); // throws CCE
2282 return BoundMethodHandle.bindSingleL(callType, form, v);
2283 }
2284
2285 // Indexes into constant method handles:
2286 static final int
2287 MH_cast = 0,
2288 MH_selectAlternative = 1,
2289 MH_countedLoopPred = 2,
2290 MH_countedLoopStep = 3,
2291 MH_initIterator = 4,
2292 MH_iteratePred = 5,
2293 MH_iterateNext = 6,
2294 MH_Array_newInstance = 7,
2295 MH_VarHandles_handleCheckedExceptions = 8,
2296 MH_LIMIT = 9;
2297
2298 static MethodHandle getConstantHandle(int idx) {
2299 MethodHandle handle = HANDLES[idx];
2300 if (handle != null) {
2301 return handle;
2302 }
2303 return setCachedHandle(idx, makeConstantHandle(idx));
2304 }
2305
2306 private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2307 // Simulate a CAS, to avoid racy duplication of results.
2308 MethodHandle prev = HANDLES[idx];
2309 if (prev != null) {
2310 return prev;
2311 }
2312 HANDLES[idx] = method;
2313 return method;
2314 }
2315
2316 // Local constant method handles:
2317 private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
2318
2319 private static MethodHandle makeConstantHandle(int idx) {
2320 try {
2321 switch (idx) {
2322 case MH_cast:
2323 return IMPL_LOOKUP.findVirtual(Class.class, "cast",
2324 MethodType.methodType(Object.class, Object.class));
2325 case MH_selectAlternative:
2326 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2327 MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2328 case MH_countedLoopPred:
2329 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2330 MethodType.methodType(boolean.class, int.class, int.class));
2331 case MH_countedLoopStep:
2332 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2333 MethodType.methodType(int.class, int.class, int.class));
2334 case MH_initIterator:
2335 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2336 MethodType.methodType(Iterator.class, Iterable.class));
2337 case MH_iteratePred:
2338 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2339 MethodType.methodType(boolean.class, Iterator.class));
2340 case MH_iterateNext:
2341 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2342 MethodType.methodType(Object.class, Iterator.class));
2343 case MH_Array_newInstance:
2344 return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2345 MethodType.methodType(Object.class, Class.class, int.class));
2346 case MH_VarHandles_handleCheckedExceptions:
2347 return IMPL_LOOKUP.findStatic(VarHandles.class, "handleCheckedExceptions",
2348 MethodType.methodType(void.class, Throwable.class));
2349 }
2350 } catch (ReflectiveOperationException ex) {
2351 throw newInternalError(ex);
2352 }
2353 throw newInternalError("Unknown function index: " + idx);
2354 }
2355 }