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