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