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