1 /* 2 * Copyright (c) 2008, 2023, 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.MethodTypeDescImpl; 31 import jdk.internal.constant.ReferenceClassDescImpl; 32 import jdk.internal.foreign.abi.NativeEntryPoint; 33 import jdk.internal.reflect.CallerSensitive; 34 import jdk.internal.reflect.Reflection; 35 import jdk.internal.vm.annotation.ForceInline; 36 import jdk.internal.vm.annotation.Hidden; 37 import jdk.internal.vm.annotation.Stable; 38 import sun.invoke.empty.Empty; 39 import sun.invoke.util.ValueConversions; 40 import sun.invoke.util.VerifyType; 41 import sun.invoke.util.Wrapper; 42 43 import java.lang.classfile.ClassFile; 44 import java.lang.constant.ClassDesc; 45 import java.lang.invoke.MethodHandles.Lookup; 46 import java.lang.reflect.Array; 47 import java.lang.reflect.Constructor; 48 import java.lang.reflect.Field; 49 import java.nio.ByteOrder; 50 import java.util.Arrays; 51 import java.util.Collections; 52 import java.util.HashMap; 53 import java.util.Iterator; 54 import java.util.List; 55 import java.util.Map; 56 import java.util.Objects; 57 import java.util.Set; 58 import java.util.concurrent.ConcurrentHashMap; 59 import java.util.function.Function; 60 import java.util.stream.Stream; 61 62 import static java.lang.classfile.ClassFile.*; 63 import static java.lang.constant.ConstantDescs.*; 64 import static java.lang.invoke.LambdaForm.*; 65 import static java.lang.invoke.MethodHandleNatives.Constants.MN_CALLER_SENSITIVE; 66 import static java.lang.invoke.MethodHandleNatives.Constants.MN_HIDDEN_MEMBER; 67 import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS; 68 import static java.lang.invoke.MethodHandleStatics.*; 69 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; 70 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE; 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 = ReferenceClassDescImpl.ofValidated("[Ljava/lang/Object;"); 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 try { 1213 assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)"; 1214 } catch (SecurityException ex) { 1215 // Self-check was blocked by security manager. This is OK. 1216 } 1217 try { 1218 // Test the invoker to ensure that it really injects into the right place. 1219 MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT); 1220 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass); 1221 return (boolean)invoker.invoke(vamh, new Object[]{ invokerClass }); 1222 } catch (Error|RuntimeException ex) { 1223 throw ex; 1224 } catch (Throwable ex) { 1225 throw new InternalError(ex); 1226 } 1227 } 1228 1229 private static final MethodHandle MH_checkCallerClass; 1230 static { 1231 final Class<?> THIS_CLASS = BindCaller.class; 1232 assert(checkCallerClass(THIS_CLASS)); 1233 try { 1234 MH_checkCallerClass = IMPL_LOOKUP 1235 .findStatic(THIS_CLASS, "checkCallerClass", 1236 MethodType.methodType(boolean.class, Class.class)); 1237 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS)); 1238 } catch (Throwable ex) { 1239 throw new InternalError(ex); 1240 } 1241 } 1242 1243 @CallerSensitive 1244 @ForceInline // to ensure Reflection.getCallerClass optimization 1245 private static boolean checkCallerClass(Class<?> expected) { 1246 // This method is called via MH_checkCallerClass and so it's correct to ask for the immediate caller here. 1247 Class<?> actual = Reflection.getCallerClass(); 1248 if (actual != expected) 1249 throw new InternalError("found " + actual.getName() + ", expected " + expected.getName()); 1250 return true; 1251 } 1252 1253 private static final byte[] INJECTED_INVOKER_TEMPLATE = generateInvokerTemplate(); 1254 1255 /** Produces byte code for a class that is used as an injected invoker. */ 1256 private static byte[] generateInvokerTemplate() { 1257 // private static class InjectedInvoker { 1258 // /* this is used to wrap DMH(s) of caller-sensitive methods */ 1259 // @Hidden 1260 // static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable { 1261 // return vamh.invokeExact(args); 1262 // } 1263 // /* this is used in caller-sensitive reflective method accessor */ 1264 // @Hidden 1265 // static Object reflect_invoke_V(MethodHandle vamh, Object target, Object[] args) throws Throwable { 1266 // return vamh.invokeExact(target, args); 1267 // } 1268 // } 1269 // } 1270 return ClassFile.of().build(ReferenceClassDescImpl.ofValidated("LInjectedInvoker;"), clb -> clb 1271 .withFlags(ACC_PRIVATE | ACC_SUPER) 1272 .withMethodBody( 1273 "invoke_V", 1274 MethodTypeDescImpl.ofValidated(CD_Object, CD_MethodHandle, CD_Object_array), 1275 ACC_STATIC, 1276 cob -> cob.aload(0) 1277 .aload(1) 1278 .invokevirtual(CD_MethodHandle, "invokeExact", MethodTypeDescImpl.ofValidated(CD_Object, CD_Object_array)) 1279 .areturn()) 1280 .withMethodBody( 1281 "reflect_invoke_V", 1282 MethodTypeDescImpl.ofValidated(CD_Object, CD_MethodHandle, CD_Object, CD_Object_array), 1283 ACC_STATIC, 1284 cob -> cob.aload(0) 1285 .aload(1) 1286 .aload(2) 1287 .invokevirtual(CD_MethodHandle, "invokeExact", MethodTypeDescImpl.ofValidated(CD_Object, CD_Object, CD_Object_array)) 1288 .areturn())); 1289 } 1290 } 1291 1292 /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */ 1293 static final class WrappedMember extends DelegatingMethodHandle { 1294 private final MethodHandle target; 1295 private final MemberName member; 1296 private final Class<?> callerClass; 1297 private final boolean isInvokeSpecial; 1298 1299 private WrappedMember(MethodHandle target, MethodType type, 1300 MemberName member, boolean isInvokeSpecial, 1301 Class<?> callerClass) { 1302 super(type, target); 1303 this.target = target; 1304 this.member = member; 1305 this.callerClass = callerClass; 1306 this.isInvokeSpecial = isInvokeSpecial; 1307 } 1308 1309 @Override 1310 MemberName internalMemberName() { 1311 return member; 1312 } 1313 @Override 1314 Class<?> internalCallerClass() { 1315 return callerClass; 1316 } 1317 @Override 1318 boolean isInvokeSpecial() { 1319 return isInvokeSpecial; 1320 } 1321 @Override 1322 protected MethodHandle getTarget() { 1323 return target; 1324 } 1325 @Override 1326 public MethodHandle asTypeUncached(MethodType newType) { 1327 // This MH is an alias for target, except for the MemberName 1328 // Drop the MemberName if there is any conversion. 1329 return target.asType(newType); 1330 } 1331 } 1332 1333 static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) { 1334 if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial()) 1335 return target; 1336 return new WrappedMember(target, target.type(), member, isInvokeSpecial, null); 1337 } 1338 1339 /** Intrinsic IDs */ 1340 /*non-public*/ 1341 enum Intrinsic { 1342 SELECT_ALTERNATIVE, 1343 GUARD_WITH_CATCH, 1344 TRY_FINALLY, 1345 TABLE_SWITCH, 1346 LOOP, 1347 ARRAY_LOAD, 1348 ARRAY_STORE, 1349 ARRAY_LENGTH, 1350 IDENTITY, 1351 ZERO, 1352 NONE // no intrinsic associated 1353 } 1354 1355 /** Mark arbitrary method handle as intrinsic. 1356 * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */ 1357 static final class IntrinsicMethodHandle extends DelegatingMethodHandle { 1358 private final MethodHandle target; 1359 private final Intrinsic intrinsicName; 1360 private final Object intrinsicData; 1361 1362 IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) { 1363 this(target, intrinsicName, null); 1364 } 1365 1366 IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) { 1367 super(target.type(), target); 1368 this.target = target; 1369 this.intrinsicName = intrinsicName; 1370 this.intrinsicData = intrinsicData; 1371 } 1372 1373 @Override 1374 protected MethodHandle getTarget() { 1375 return target; 1376 } 1377 1378 @Override 1379 Intrinsic intrinsicName() { 1380 return intrinsicName; 1381 } 1382 1383 @Override 1384 Object intrinsicData() { 1385 return intrinsicData; 1386 } 1387 1388 @Override 1389 public MethodHandle asTypeUncached(MethodType newType) { 1390 // This MH is an alias for target, except for the intrinsic name 1391 // Drop the name if there is any conversion. 1392 return target.asType(newType); 1393 } 1394 1395 @Override 1396 String internalProperties() { 1397 return super.internalProperties() + 1398 "\n& Intrinsic="+intrinsicName; 1399 } 1400 1401 @Override 1402 public MethodHandle asCollector(Class<?> arrayType, int arrayLength) { 1403 if (intrinsicName == Intrinsic.IDENTITY) { 1404 MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength); 1405 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength); 1406 return newArray.asType(resultType); 1407 } 1408 return super.asCollector(arrayType, arrayLength); 1409 } 1410 } 1411 1412 static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) { 1413 return makeIntrinsic(target, intrinsicName, null); 1414 } 1415 1416 static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) { 1417 if (intrinsicName == target.intrinsicName()) 1418 return target; 1419 return new IntrinsicMethodHandle(target, intrinsicName, intrinsicData); 1420 } 1421 1422 static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) { 1423 return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName); 1424 } 1425 1426 private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1]; 1427 1428 /** Return a method handle that takes the indicated number of Object 1429 * arguments and returns an Object array of them, as if for varargs. 1430 */ 1431 static MethodHandle varargsArray(int nargs) { 1432 MethodHandle mh = ARRAYS[nargs]; 1433 if (mh != null) { 1434 return mh; 1435 } 1436 mh = makeCollector(Object[].class, nargs); 1437 assert(assertCorrectArity(mh, nargs)); 1438 return ARRAYS[nargs] = mh; 1439 } 1440 1441 /** Return a method handle that takes the indicated number of 1442 * typed arguments and returns an array of them. 1443 * The type argument is the array type. 1444 */ 1445 static MethodHandle varargsArray(Class<?> arrayType, int nargs) { 1446 Class<?> elemType = arrayType.getComponentType(); 1447 if (elemType == null) throw new IllegalArgumentException("not an array: "+arrayType); 1448 if (nargs >= MAX_JVM_ARITY/2 - 1) { 1449 int slots = nargs; 1450 final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1; // 1 for receiver MH 1451 if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive()) 1452 slots *= Wrapper.forPrimitiveType(elemType).stackSlots(); 1453 if (slots > MAX_ARRAY_SLOTS) 1454 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs); 1455 } 1456 if (elemType == Object.class) 1457 return varargsArray(nargs); 1458 // other cases: primitive arrays, subtypes of Object[] 1459 MethodHandle cache[] = Makers.TYPED_COLLECTORS.get(elemType); 1460 MethodHandle mh = nargs < cache.length ? cache[nargs] : null; 1461 if (mh != null) return mh; 1462 mh = makeCollector(arrayType, nargs); 1463 assert(assertCorrectArity(mh, nargs)); 1464 if (nargs < cache.length) 1465 cache[nargs] = mh; 1466 return mh; 1467 } 1468 1469 private static boolean assertCorrectArity(MethodHandle mh, int arity) { 1470 assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh; 1471 return true; 1472 } 1473 1474 static final int MAX_JVM_ARITY = 255; // limit imposed by the JVM 1475 1476 /*non-public*/ 1477 static void assertSame(Object mh1, Object mh2) { 1478 if (mh1 != mh2) { 1479 String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)", 1480 mh1, ((MethodHandle)mh1).form, 1481 mh2, ((MethodHandle)mh2).form); 1482 throw newInternalError(msg); 1483 } 1484 } 1485 1486 // Local constant functions: 1487 1488 /* non-public */ 1489 static final byte NF_checkSpreadArgument = 0, 1490 NF_guardWithCatch = 1, 1491 NF_throwException = 2, 1492 NF_tryFinally = 3, 1493 NF_loop = 4, 1494 NF_profileBoolean = 5, 1495 NF_tableSwitch = 6, 1496 NF_LIMIT = 7; 1497 1498 private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT]; 1499 1500 static NamedFunction getFunction(byte func) { 1501 NamedFunction nf = NFS[func]; 1502 if (nf != null) { 1503 return nf; 1504 } 1505 return NFS[func] = createFunction(func); 1506 } 1507 1508 private static NamedFunction createFunction(byte func) { 1509 try { 1510 return switch (func) { 1511 case NF_checkSpreadArgument -> new NamedFunction(MethodHandleImpl.class 1512 .getDeclaredMethod("checkSpreadArgument", Object.class, int.class)); 1513 case NF_guardWithCatch -> new NamedFunction(MethodHandleImpl.class 1514 .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class, 1515 MethodHandle.class, Object[].class)); 1516 case NF_tryFinally -> new NamedFunction(MethodHandleImpl.class 1517 .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class)); 1518 case NF_loop -> new NamedFunction(MethodHandleImpl.class 1519 .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class)); 1520 case NF_throwException -> new NamedFunction(MethodHandleImpl.class 1521 .getDeclaredMethod("throwException", Throwable.class)); 1522 case NF_profileBoolean -> new NamedFunction(MethodHandleImpl.class 1523 .getDeclaredMethod("profileBoolean", boolean.class, int[].class)); 1524 case NF_tableSwitch -> new NamedFunction(MethodHandleImpl.class 1525 .getDeclaredMethod("tableSwitch", int.class, MethodHandle.class, CasesHolder.class, Object[].class)); 1526 default -> throw new InternalError("Undefined function: " + func); 1527 }; 1528 } catch (ReflectiveOperationException ex) { 1529 throw newInternalError(ex); 1530 } 1531 } 1532 1533 static { 1534 SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() { 1535 @Override 1536 public Class<?> getDeclaringClass(Object rmname) { 1537 ResolvedMethodName method = (ResolvedMethodName)rmname; 1538 return method.declaringClass(); 1539 } 1540 1541 @Override 1542 public MethodType getMethodType(String descriptor, ClassLoader loader) { 1543 return MethodType.fromDescriptor(descriptor, loader); 1544 } 1545 1546 public boolean isCallerSensitive(int flags) { 1547 return (flags & MN_CALLER_SENSITIVE) == MN_CALLER_SENSITIVE; 1548 } 1549 1550 public boolean isHiddenMember(int flags) { 1551 return (flags & MN_HIDDEN_MEMBER) == MN_HIDDEN_MEMBER; 1552 } 1553 1554 public boolean isNullRestrictedField(MethodHandle mh) { 1555 var memberName = mh.internalMemberName(); 1556 assert memberName.isField(); 1557 return memberName.isNullRestricted(); 1558 } 1559 1560 @Override 1561 public Map<String, byte[]> generateHolderClasses(Stream<String> traces) { 1562 return GenerateJLIClassesHelper.generateHolderClasses(traces); 1563 } 1564 1565 @Override 1566 public VarHandle memorySegmentViewHandle(Class<?> carrier, long alignmentMask, ByteOrder order) { 1567 return VarHandles.memorySegmentViewHandle(carrier, alignmentMask, order); 1568 } 1569 1570 @Override 1571 public MethodHandle nativeMethodHandle(NativeEntryPoint nep) { 1572 return NativeMethodHandle.make(nep); 1573 } 1574 1575 @Override 1576 public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) { 1577 return VarHandles.filterValue(target, filterToTarget, filterFromTarget); 1578 } 1579 1580 @Override 1581 public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) { 1582 return VarHandles.filterCoordinates(target, pos, filters); 1583 } 1584 1585 @Override 1586 public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) { 1587 return VarHandles.dropCoordinates(target, pos, valueTypes); 1588 } 1589 1590 @Override 1591 public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) { 1592 return VarHandles.permuteCoordinates(target, newCoordinates, reorder); 1593 } 1594 1595 @Override 1596 public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) { 1597 return VarHandles.collectCoordinates(target, pos, filter); 1598 } 1599 1600 @Override 1601 public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) { 1602 return VarHandles.insertCoordinates(target, pos, values); 1603 } 1604 1605 1606 @Override 1607 public MethodHandle unreflectConstructor(Constructor<?> ctor) throws IllegalAccessException { 1608 return IMPL_LOOKUP.unreflectConstructor(ctor); 1609 } 1610 1611 @Override 1612 public MethodHandle unreflectField(Field field, boolean isSetter) throws IllegalAccessException { 1613 return isSetter ? IMPL_LOOKUP.unreflectSetter(field) : IMPL_LOOKUP.unreflectGetter(field); 1614 } 1615 1616 @Override 1617 public MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws IllegalAccessException { 1618 try { 1619 return IMPL_LOOKUP.findVirtual(defc, name, type); 1620 } catch (NoSuchMethodException e) { 1621 return null; 1622 } 1623 } 1624 1625 @Override 1626 public MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws IllegalAccessException { 1627 try { 1628 return IMPL_LOOKUP.findStatic(defc, name, type); 1629 } catch (NoSuchMethodException e) { 1630 return null; 1631 } 1632 } 1633 1634 @Override 1635 public MethodHandle reflectiveInvoker(Class<?> caller) { 1636 Objects.requireNonNull(caller); 1637 return BindCaller.reflectiveInvoker(caller); 1638 } 1639 1640 @Override 1641 public Class<?>[] exceptionTypes(MethodHandle handle) { 1642 return VarHandles.exceptionTypes(handle); 1643 } 1644 1645 @Override 1646 public MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException { 1647 return IMPL_LOOKUP.serializableConstructor(decl, ctorToCall); 1648 } 1649 1650 }); 1651 } 1652 1653 /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */ 1654 private static MethodHandle unboxResultHandle(Class<?> returnType) { 1655 if (returnType.isPrimitive()) { 1656 if (returnType == void.class) { 1657 return ValueConversions.ignore(); 1658 } else { 1659 Wrapper w = Wrapper.forPrimitiveType(returnType); 1660 return ValueConversions.unboxExact(w); 1661 } 1662 } else { 1663 return MethodHandles.identity(Object.class); 1664 } 1665 } 1666 1667 /** 1668 * Assembles a loop method handle from the given handles and type information. 1669 * 1670 * @param tloop the return type of the loop. 1671 * @param targs types of the arguments to be passed to the loop. 1672 * @param init sanitized array of initializers for loop-local variables. 1673 * @param step sanitized array of loop bodies. 1674 * @param pred sanitized array of predicates. 1675 * @param fini sanitized array of loop finalizers. 1676 * 1677 * @return a handle that, when invoked, will execute the loop. 1678 */ 1679 static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step, 1680 List<MethodHandle> pred, List<MethodHandle> fini) { 1681 MethodType type = MethodType.methodType(tloop, targs); 1682 BasicType[] initClauseTypes = 1683 init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new); 1684 LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes); 1685 1686 // Prepare auxiliary method handles used during LambdaForm interpretation. 1687 // Box arguments and wrap them into Object[]: ValueConversions.array(). 1688 MethodType varargsType = type.changeReturnType(Object[].class); 1689 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType); 1690 MethodHandle unboxResult = unboxResultHandle(tloop); 1691 1692 LoopClauses clauseData = 1693 new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)}); 1694 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL(); 1695 BoundMethodHandle mh; 1696 try { 1697 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData, 1698 (Object) collectArgs, (Object) unboxResult); 1699 } catch (Throwable ex) { 1700 throw uncaughtException(ex); 1701 } 1702 assert(mh.type() == type); 1703 return mh; 1704 } 1705 1706 private static MethodHandle[] toArray(List<MethodHandle> l) { 1707 return l.toArray(new MethodHandle[0]); 1708 } 1709 1710 /** 1711 * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are 1712 * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one 1713 * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by 1714 * {@code t12}): 1715 * <blockquote><pre>{@code 1716 * loop=Lambda(a0:L,a1:L)=>{ 1717 * t2:L=BoundMethodHandle$Species_L3.argL0(a0:L); // LoopClauses holding init, step, pred, fini handles 1718 * t3:L=BoundMethodHandle$Species_L3.argL1(a0:L); // helper handle to box the arguments into an Object[] 1719 * t4:L=BoundMethodHandle$Species_L3.argL2(a0:L); // helper handle to unbox the result 1720 * t5:L=MethodHandle.invokeBasic(t3:L,a1:L); // box the arguments into an Object[] 1721 * t6:L=MethodHandleImpl.loop(null,t2:L,t3:L); // call the loop executor 1722 * t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L} // unbox the result; return the result 1723 * }</pre></blockquote> 1724 * <p> 1725 * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method 1726 * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps 1727 * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary 1728 * ({@code ValueConversions.unbox()}). 1729 * <p> 1730 * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda 1731 * forms among loop combinators with the same basic type. 1732 * <p> 1733 * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to 1734 * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is 1735 * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int) 1736 * bytecode generation}. 1737 */ 1738 private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) { 1739 final int THIS_MH = 0; // the BMH_LLL 1740 final int ARG_BASE = 1; // start of incoming arguments 1741 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 1742 1743 int nameCursor = ARG_LIMIT; 1744 final int GET_CLAUSE_DATA = nameCursor++; 1745 final int GET_COLLECT_ARGS = nameCursor++; 1746 final int GET_UNBOX_RESULT = nameCursor++; 1747 final int BOXED_ARGS = nameCursor++; 1748 final int LOOP = nameCursor++; 1749 final int UNBOX_RESULT = nameCursor++; 1750 1751 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP); 1752 if (lform == null) { 1753 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType); 1754 1755 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL(); 1756 names[THIS_MH] = names[THIS_MH].withConstraint(data); 1757 names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]); 1758 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]); 1759 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]); 1760 1761 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...); 1762 MethodType collectArgsType = basicType.changeReturnType(Object.class); 1763 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType); 1764 Object[] args = new Object[invokeBasic.type().parameterCount()]; 1765 args[0] = names[GET_COLLECT_ARGS]; 1766 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE); 1767 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.LOOP)), args); 1768 1769 // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L); 1770 Object[] lArgs = 1771 new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor 1772 names[GET_CLAUSE_DATA], names[BOXED_ARGS]}; 1773 names[LOOP] = new Name(getFunction(NF_loop), lArgs); 1774 1775 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L); 1776 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class)); 1777 Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]}; 1778 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs); 1779 1780 lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP, 1781 LambdaForm.create(basicType.parameterCount() + 1, names, Kind.LOOP)); 1782 } 1783 1784 // BOXED_ARGS is the index into the names array where the loop idiom starts 1785 return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes); 1786 } 1787 1788 static class LoopClauses { 1789 @Stable final MethodHandle[][] clauses; 1790 LoopClauses(MethodHandle[][] clauses) { 1791 assert clauses.length == 4; 1792 this.clauses = clauses; 1793 } 1794 @Override 1795 public String toString() { 1796 StringBuilder sb = new StringBuilder("LoopClauses -- "); 1797 for (int i = 0; i < 4; ++i) { 1798 if (i > 0) { 1799 sb.append(" "); 1800 } 1801 sb.append('<').append(i).append(">: "); 1802 MethodHandle[] hs = clauses[i]; 1803 for (int j = 0; j < hs.length; ++j) { 1804 if (j > 0) { 1805 sb.append(" "); 1806 } 1807 sb.append('*').append(j).append(": ").append(hs[j]).append('\n'); 1808 } 1809 } 1810 sb.append(" --\n"); 1811 return sb.toString(); 1812 } 1813 } 1814 1815 /** 1816 * Intrinsified during LambdaForm compilation 1817 * (see {@link InvokerBytecodeGenerator#emitLoop(int)}). 1818 */ 1819 @Hidden 1820 static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable { 1821 final MethodHandle[] init = clauseData.clauses[0]; 1822 final MethodHandle[] step = clauseData.clauses[1]; 1823 final MethodHandle[] pred = clauseData.clauses[2]; 1824 final MethodHandle[] fini = clauseData.clauses[3]; 1825 int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count(); 1826 int nArgs = init[0].type().parameterCount(); 1827 Object[] varsAndArgs = new Object[varSize + nArgs]; 1828 for (int i = 0, v = 0; i < init.length; ++i) { 1829 MethodHandle ih = init[i]; 1830 if (ih.type().returnType() == void.class) { 1831 ih.invokeWithArguments(av); 1832 } else { 1833 varsAndArgs[v++] = ih.invokeWithArguments(av); 1834 } 1835 } 1836 System.arraycopy(av, 0, varsAndArgs, varSize, nArgs); 1837 final int nSteps = step.length; 1838 for (; ; ) { 1839 for (int i = 0, v = 0; i < nSteps; ++i) { 1840 MethodHandle p = pred[i]; 1841 MethodHandle s = step[i]; 1842 MethodHandle f = fini[i]; 1843 if (s.type().returnType() == void.class) { 1844 s.invokeWithArguments(varsAndArgs); 1845 } else { 1846 varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs); 1847 } 1848 if (!(boolean) p.invokeWithArguments(varsAndArgs)) { 1849 return f.invokeWithArguments(varsAndArgs); 1850 } 1851 } 1852 } 1853 } 1854 1855 /** 1856 * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle, 1857 * MethodHandle) counting loops}. 1858 * 1859 * @param limit the upper bound of the parameter, statically bound at loop creation time. 1860 * @param counter the counter parameter, passed in during loop execution. 1861 * 1862 * @return whether the counter has reached the limit. 1863 */ 1864 static boolean countedLoopPredicate(int limit, int counter) { 1865 return counter < limit; 1866 } 1867 1868 /** 1869 * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle, 1870 * MethodHandle) counting loops} to increment the counter. 1871 * 1872 * @param limit the upper bound of the loop counter (ignored). 1873 * @param counter the loop counter. 1874 * 1875 * @return the loop counter incremented by 1. 1876 */ 1877 static int countedLoopStep(int limit, int counter) { 1878 return counter + 1; 1879 } 1880 1881 /** 1882 * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}. 1883 * 1884 * @param it the {@link Iterable} over which the loop iterates. 1885 * 1886 * @return an {@link Iterator} over the argument's elements. 1887 */ 1888 static Iterator<?> initIterator(Iterable<?> it) { 1889 return it.iterator(); 1890 } 1891 1892 /** 1893 * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}. 1894 * 1895 * @param it the iterator to be checked. 1896 * 1897 * @return {@code true} iff there are more elements to iterate over. 1898 */ 1899 static boolean iteratePredicate(Iterator<?> it) { 1900 return it.hasNext(); 1901 } 1902 1903 /** 1904 * This method is bound as the step for retrieving the current value from the iterator in {@linkplain 1905 * MethodHandles#iteratedLoop iterating loops}. 1906 * 1907 * @param it the iterator. 1908 * 1909 * @return the next element from the iterator. 1910 */ 1911 static Object iterateNext(Iterator<?> it) { 1912 return it.next(); 1913 } 1914 1915 /** 1916 * Makes a {@code try-finally} handle that conforms to the type constraints. 1917 * 1918 * @param target the target to execute in a {@code try-finally} block. 1919 * @param cleanup the cleanup to execute in the {@code finally} block. 1920 * @param rtype the result type of the entire construct. 1921 * @param argTypes the types of the arguments. 1922 * 1923 * @return a handle on the constructed {@code try-finally} block. 1924 */ 1925 static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, Class<?>[] argTypes) { 1926 MethodType type = MethodType.methodType(rtype, argTypes); 1927 LambdaForm form = makeTryFinallyForm(type.basicType()); 1928 1929 // Prepare auxiliary method handles used during LambdaForm interpretation. 1930 // Box arguments and wrap them into Object[]: ValueConversions.array(). 1931 MethodType varargsType = type.changeReturnType(Object[].class); 1932 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType); 1933 MethodHandle unboxResult = unboxResultHandle(rtype); 1934 1935 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL(); 1936 BoundMethodHandle mh; 1937 try { 1938 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup, 1939 (Object) collectArgs, (Object) unboxResult); 1940 } catch (Throwable ex) { 1941 throw uncaughtException(ex); 1942 } 1943 assert(mh.type() == type); 1944 return mh; 1945 } 1946 1947 /** 1948 * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in 1949 * {@code a1}, and a reference return type, with the return value represented by {@code t8}): 1950 * <blockquote><pre>{@code 1951 * tryFinally=Lambda(a0:L,a1:L)=>{ 1952 * t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L); // target method handle 1953 * t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L); // cleanup method handle 1954 * t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L); // helper handle to box the arguments into an Object[] 1955 * t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L); // helper handle to unbox the result 1956 * t6:L=MethodHandle.invokeBasic(t4:L,a1:L); // box the arguments into an Object[] 1957 * t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor 1958 * t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L} // unbox the result; return the result 1959 * }</pre></blockquote> 1960 * <p> 1961 * {@code argL0} and {@code argL1} are the target and cleanup method handles. 1962 * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into 1963 * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary 1964 * ({@code ValueConversions.unbox()}). 1965 * <p> 1966 * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda 1967 * forms among tryFinally combinators with the same basic type. 1968 */ 1969 private static LambdaForm makeTryFinallyForm(MethodType basicType) { 1970 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF); 1971 if (lform != null) { 1972 return lform; 1973 } 1974 final int THIS_MH = 0; // the BMH_LLLL 1975 final int ARG_BASE = 1; // start of incoming arguments 1976 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 1977 1978 int nameCursor = ARG_LIMIT; 1979 final int GET_TARGET = nameCursor++; 1980 final int GET_CLEANUP = nameCursor++; 1981 final int GET_COLLECT_ARGS = nameCursor++; 1982 final int GET_UNBOX_RESULT = nameCursor++; 1983 final int BOXED_ARGS = nameCursor++; 1984 final int TRY_FINALLY = nameCursor++; 1985 final int UNBOX_RESULT = nameCursor++; 1986 1987 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType); 1988 1989 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL(); 1990 names[THIS_MH] = names[THIS_MH].withConstraint(data); 1991 names[GET_TARGET] = new Name(data.getterFunction(0), names[THIS_MH]); 1992 names[GET_CLEANUP] = new Name(data.getterFunction(1), names[THIS_MH]); 1993 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]); 1994 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]); 1995 1996 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...); 1997 MethodType collectArgsType = basicType.changeReturnType(Object.class); 1998 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType); 1999 Object[] args = new Object[invokeBasic.type().parameterCount()]; 2000 args[0] = names[GET_COLLECT_ARGS]; 2001 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE); 2002 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY)), args); 2003 2004 // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L); 2005 Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]}; 2006 names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs); 2007 2008 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L); 2009 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class)); 2010 Object[] unboxArgs = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]}; 2011 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs); 2012 2013 lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TRY_FINALLY); 2014 2015 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform); 2016 } 2017 2018 /** 2019 * Intrinsified during LambdaForm compilation 2020 * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}). 2021 */ 2022 @Hidden 2023 static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable { 2024 Throwable t = null; 2025 Object r = null; 2026 try { 2027 r = target.invokeWithArguments(av); 2028 } catch (Throwable thrown) { 2029 t = thrown; 2030 throw t; 2031 } finally { 2032 Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r); 2033 r = cleanup.invokeWithArguments(args); 2034 } 2035 return r; 2036 } 2037 2038 // see varargsArray method for chaching/package-private version of this 2039 private static MethodHandle makeCollector(Class<?> arrayType, int parameterCount) { 2040 MethodType type = MethodType.methodType(arrayType, Collections.nCopies(parameterCount, arrayType.componentType())); 2041 MethodHandle newArray = MethodHandles.arrayConstructor(arrayType); 2042 2043 LambdaForm form = makeCollectorForm(type.basicType(), arrayType); 2044 2045 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L(); 2046 BoundMethodHandle mh; 2047 try { 2048 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) newArray); 2049 } catch (Throwable ex) { 2050 throw uncaughtException(ex); 2051 } 2052 assert(mh.type() == type); 2053 return mh; 2054 } 2055 2056 private static LambdaForm makeCollectorForm(MethodType basicType, Class<?> arrayType) { 2057 int parameterCount = basicType.parameterCount(); 2058 2059 // Only share the lambda form for empty arrays and reference types. 2060 // Sharing based on the basic type alone doesn't work because 2061 // we need a separate lambda form for byte/short/char/int which 2062 // are all erased to int otherwise. 2063 // Other caching for primitive types happens at the MethodHandle level (see varargsArray). 2064 boolean isReferenceType = !arrayType.componentType().isPrimitive(); 2065 boolean isSharedLambdaForm = parameterCount == 0 || isReferenceType; 2066 if (isSharedLambdaForm) { 2067 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_COLLECTOR); 2068 if (lform != null) { 2069 return lform; 2070 } 2071 } 2072 2073 // use erased accessor for reference types 2074 MethodHandle storeFunc = isReferenceType 2075 ? ArrayAccessor.OBJECT_ARRAY_SETTER 2076 : makeArrayElementAccessor(arrayType, ArrayAccess.SET); 2077 2078 final int THIS_MH = 0; // the BMH_L 2079 final int ARG_BASE = 1; // start of incoming arguments 2080 final int ARG_LIMIT = ARG_BASE + parameterCount; 2081 2082 int nameCursor = ARG_LIMIT; 2083 final int GET_NEW_ARRAY = nameCursor++; 2084 final int CALL_NEW_ARRAY = nameCursor++; 2085 final int STORE_ELEMENT_BASE = nameCursor; 2086 final int STORE_ELEMENT_LIMIT = STORE_ELEMENT_BASE + parameterCount; 2087 nameCursor = STORE_ELEMENT_LIMIT; 2088 2089 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType); 2090 2091 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L(); 2092 names[THIS_MH] = names[THIS_MH].withConstraint(data); 2093 names[GET_NEW_ARRAY] = new Name(data.getterFunction(0), names[THIS_MH]); 2094 2095 MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(Object.class, int.class)); 2096 names[CALL_NEW_ARRAY] = new Name(new NamedFunction(invokeBasic), names[GET_NEW_ARRAY], parameterCount); 2097 for (int storeIndex = 0, 2098 storeNameCursor = STORE_ELEMENT_BASE, 2099 argCursor = ARG_BASE; 2100 storeNameCursor < STORE_ELEMENT_LIMIT; 2101 storeIndex++, storeNameCursor++, argCursor++){ 2102 2103 names[storeNameCursor] = new Name(new NamedFunction(makeIntrinsic(storeFunc, Intrinsic.ARRAY_STORE)), 2104 names[CALL_NEW_ARRAY], storeIndex, names[argCursor]); 2105 } 2106 2107 LambdaForm lform = LambdaForm.create(basicType.parameterCount() + 1, names, CALL_NEW_ARRAY, Kind.COLLECTOR); 2108 if (isSharedLambdaForm) { 2109 lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_COLLECTOR, lform); 2110 } 2111 return lform; 2112 } 2113 2114 // use a wrapper because we need this array to be @Stable 2115 static class CasesHolder { 2116 @Stable 2117 final MethodHandle[] cases; 2118 2119 public CasesHolder(MethodHandle[] cases) { 2120 this.cases = cases; 2121 } 2122 } 2123 2124 static MethodHandle makeTableSwitch(MethodType type, MethodHandle defaultCase, MethodHandle[] caseActions) { 2125 MethodType varargsType = type.changeReturnType(Object[].class); 2126 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType); 2127 2128 MethodHandle unboxResult = unboxResultHandle(type.returnType()); 2129 2130 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL(); 2131 LambdaForm form = makeTableSwitchForm(type.basicType(), data, caseActions.length); 2132 BoundMethodHandle mh; 2133 CasesHolder caseHolder = new CasesHolder(caseActions); 2134 try { 2135 mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) defaultCase, (Object) collectArgs, 2136 (Object) unboxResult, (Object) caseHolder); 2137 } catch (Throwable ex) { 2138 throw uncaughtException(ex); 2139 } 2140 assert(mh.type() == type); 2141 return mh; 2142 } 2143 2144 private static class TableSwitchCacheKey { 2145 private static final Map<TableSwitchCacheKey, LambdaForm> CACHE = new ConcurrentHashMap<>(); 2146 2147 private final MethodType basicType; 2148 private final int numberOfCases; 2149 2150 public TableSwitchCacheKey(MethodType basicType, int numberOfCases) { 2151 this.basicType = basicType; 2152 this.numberOfCases = numberOfCases; 2153 } 2154 2155 @Override 2156 public boolean equals(Object o) { 2157 if (this == o) return true; 2158 if (o == null || getClass() != o.getClass()) return false; 2159 TableSwitchCacheKey that = (TableSwitchCacheKey) o; 2160 return numberOfCases == that.numberOfCases && Objects.equals(basicType, that.basicType); 2161 } 2162 @Override 2163 public int hashCode() { 2164 return Objects.hash(basicType, numberOfCases); 2165 } 2166 } 2167 2168 private static LambdaForm makeTableSwitchForm(MethodType basicType, BoundMethodHandle.SpeciesData data, 2169 int numCases) { 2170 // We need to cache based on the basic type X number of cases, 2171 // since the number of cases is used when generating bytecode. 2172 // This also means that we can't use the cache in MethodTypeForm, 2173 // which only uses the basic type as a key. 2174 TableSwitchCacheKey key = new TableSwitchCacheKey(basicType, numCases); 2175 LambdaForm lform = TableSwitchCacheKey.CACHE.get(key); 2176 if (lform != null) { 2177 return lform; 2178 } 2179 2180 final int THIS_MH = 0; 2181 final int ARG_BASE = 1; // start of incoming arguments 2182 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 2183 final int ARG_SWITCH_ON = ARG_BASE; 2184 assert ARG_SWITCH_ON < ARG_LIMIT; 2185 2186 int nameCursor = ARG_LIMIT; 2187 final int GET_COLLECT_ARGS = nameCursor++; 2188 final int GET_DEFAULT_CASE = nameCursor++; 2189 final int GET_UNBOX_RESULT = nameCursor++; 2190 final int GET_CASES = nameCursor++; 2191 final int BOXED_ARGS = nameCursor++; 2192 final int TABLE_SWITCH = nameCursor++; 2193 final int UNBOXED_RESULT = nameCursor++; 2194 2195 int fieldCursor = 0; 2196 final int FIELD_DEFAULT_CASE = fieldCursor++; 2197 final int FIELD_COLLECT_ARGS = fieldCursor++; 2198 final int FIELD_UNBOX_RESULT = fieldCursor++; 2199 final int FIELD_CASES = fieldCursor++; 2200 2201 Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType); 2202 2203 names[THIS_MH] = names[THIS_MH].withConstraint(data); 2204 names[GET_DEFAULT_CASE] = new Name(data.getterFunction(FIELD_DEFAULT_CASE), names[THIS_MH]); 2205 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(FIELD_COLLECT_ARGS), names[THIS_MH]); 2206 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(FIELD_UNBOX_RESULT), names[THIS_MH]); 2207 names[GET_CASES] = new Name(data.getterFunction(FIELD_CASES), names[THIS_MH]); 2208 2209 { 2210 MethodType collectArgsType = basicType.changeReturnType(Object.class); 2211 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType); 2212 Object[] args = new Object[invokeBasic.type().parameterCount()]; 2213 args[0] = names[GET_COLLECT_ARGS]; 2214 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE); 2215 names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TABLE_SWITCH, numCases)), args); 2216 } 2217 2218 { 2219 Object[] tfArgs = new Object[]{ 2220 names[ARG_SWITCH_ON], names[GET_DEFAULT_CASE], names[GET_CASES], names[BOXED_ARGS]}; 2221 names[TABLE_SWITCH] = new Name(getFunction(NF_tableSwitch), tfArgs); 2222 } 2223 2224 { 2225 MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class)); 2226 Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[TABLE_SWITCH]}; 2227 names[UNBOXED_RESULT] = new Name(invokeBasic, unboxArgs); 2228 } 2229 2230 lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TABLE_SWITCH); 2231 LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform); 2232 return prev != null ? prev : lform; 2233 } 2234 2235 @Hidden 2236 static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable { 2237 MethodHandle[] caseActions = holder.cases; 2238 MethodHandle selectedCase; 2239 if (input < 0 || input >= caseActions.length) { 2240 selectedCase = defaultCase; 2241 } else { 2242 selectedCase = caseActions[input]; 2243 } 2244 return selectedCase.invokeWithArguments(args); 2245 } 2246 2247 // Indexes into constant method handles: 2248 static final int 2249 MH_cast = 0, 2250 MH_selectAlternative = 1, 2251 MH_countedLoopPred = 2, 2252 MH_countedLoopStep = 3, 2253 MH_initIterator = 4, 2254 MH_iteratePred = 5, 2255 MH_iterateNext = 6, 2256 MH_Array_newInstance = 7, 2257 MH_VarHandles_handleCheckedExceptions = 8, 2258 MH_LIMIT = 9; 2259 2260 static MethodHandle getConstantHandle(int idx) { 2261 MethodHandle handle = HANDLES[idx]; 2262 if (handle != null) { 2263 return handle; 2264 } 2265 return setCachedHandle(idx, makeConstantHandle(idx)); 2266 } 2267 2268 private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) { 2269 // Simulate a CAS, to avoid racy duplication of results. 2270 MethodHandle prev = HANDLES[idx]; 2271 if (prev != null) { 2272 return prev; 2273 } 2274 HANDLES[idx] = method; 2275 return method; 2276 } 2277 2278 // Local constant method handles: 2279 private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT]; 2280 2281 private static MethodHandle makeConstantHandle(int idx) { 2282 try { 2283 switch (idx) { 2284 case MH_cast: 2285 return IMPL_LOOKUP.findVirtual(Class.class, "cast", 2286 MethodType.methodType(Object.class, Object.class)); 2287 case MH_selectAlternative: 2288 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative", 2289 MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)); 2290 case MH_countedLoopPred: 2291 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate", 2292 MethodType.methodType(boolean.class, int.class, int.class)); 2293 case MH_countedLoopStep: 2294 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep", 2295 MethodType.methodType(int.class, int.class, int.class)); 2296 case MH_initIterator: 2297 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator", 2298 MethodType.methodType(Iterator.class, Iterable.class)); 2299 case MH_iteratePred: 2300 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate", 2301 MethodType.methodType(boolean.class, Iterator.class)); 2302 case MH_iterateNext: 2303 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext", 2304 MethodType.methodType(Object.class, Iterator.class)); 2305 case MH_Array_newInstance: 2306 return IMPL_LOOKUP.findStatic(Array.class, "newInstance", 2307 MethodType.methodType(Object.class, Class.class, int.class)); 2308 case MH_VarHandles_handleCheckedExceptions: 2309 return IMPL_LOOKUP.findStatic(VarHandles.class, "handleCheckedExceptions", 2310 MethodType.methodType(void.class, Throwable.class)); 2311 } 2312 } catch (ReflectiveOperationException ex) { 2313 throw newInternalError(ex); 2314 } 2315 throw newInternalError("Unknown function index: " + idx); 2316 } 2317 }