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