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