1 /* 2 * Copyright (c) 2008, 2013, 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 java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.Collections; 33 import java.util.function.Function; 34 35 import sun.invoke.empty.Empty; 36 import sun.invoke.util.ValueConversions; 37 import sun.invoke.util.VerifyType; 38 import sun.invoke.util.Wrapper; 39 import jdk.internal.HotSpotIntrinsicCandidate; 40 import sun.reflect.CallerSensitive; 41 import sun.reflect.Reflection; 42 import static java.lang.invoke.LambdaForm.*; 43 import static java.lang.invoke.MethodHandleStatics.*; 44 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; 45 46 /** 47 * Trusted implementation code for MethodHandle. 48 * @author jrose 49 */ 50 /*non-public*/ abstract class MethodHandleImpl { 51 // Do not adjust this except for special platforms: 52 private static final int MAX_ARITY; 53 static { 54 final Object[] values = { 255 }; 55 AccessController.doPrivileged(new PrivilegedAction<>() { 56 @Override 57 public Void run() { 58 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255); 59 return null; 60 } 61 }); 62 MAX_ARITY = (Integer) values[0]; 63 } 64 65 /// Factory methods to create method handles: 66 67 static void initStatics() { 68 // Trigger selected static initializations. 69 MemberName.Factory.INSTANCE.getClass(); 70 } 71 72 static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) { 73 if (arrayClass == Object[].class) 74 return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER); 75 if (!arrayClass.isArray()) 76 throw newIllegalArgumentException("not an array: "+arrayClass); 77 MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass); 78 int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX); 79 MethodHandle mh = cache[cacheIndex]; 80 if (mh != null) return mh; 81 mh = ArrayAccessor.getAccessor(arrayClass, isSetter); 82 MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter); 83 if (mh.type() != correctType) { 84 assert(mh.type().parameterType(0) == Object[].class); 85 assert((isSetter ? mh.type().parameterType(2) : mh.type().returnType()) == Object.class); 86 assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType()); 87 // safe to view non-strictly, because element type follows from array type 88 mh = mh.viewAsType(correctType, false); 89 } 90 mh = makeIntrinsic(mh, (isSetter ? Intrinsic.ARRAY_STORE : Intrinsic.ARRAY_LOAD)); 91 // Atomically update accessor cache. 92 synchronized(cache) { 93 if (cache[cacheIndex] == null) { 94 cache[cacheIndex] = mh; 95 } else { 96 // Throw away newly constructed accessor and use cached version. 97 mh = cache[cacheIndex]; 98 } 99 } 100 return mh; 101 } 102 103 static final class ArrayAccessor { 104 /// Support for array element access 105 static final int GETTER_INDEX = 0, SETTER_INDEX = 1, INDEX_LIMIT = 2; 106 static final ClassValue<MethodHandle[]> TYPED_ACCESSORS 107 = new ClassValue<MethodHandle[]>() { 108 @Override 109 protected MethodHandle[] computeValue(Class<?> type) { 110 return new MethodHandle[INDEX_LIMIT]; 111 } 112 }; 113 static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER; 114 static { 115 MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class); 116 cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, false), Intrinsic.ARRAY_LOAD); 117 cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, true), Intrinsic.ARRAY_STORE); 118 119 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName())); 120 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName())); 121 } 122 123 static int getElementI(int[] a, int i) { return a[i]; } 124 static long getElementJ(long[] a, int i) { return a[i]; } 125 static float getElementF(float[] a, int i) { return a[i]; } 126 static double getElementD(double[] a, int i) { return a[i]; } 127 static boolean getElementZ(boolean[] a, int i) { return a[i]; } 128 static byte getElementB(byte[] a, int i) { return a[i]; } 129 static short getElementS(short[] a, int i) { return a[i]; } 130 static char getElementC(char[] a, int i) { return a[i]; } 131 static Object getElementL(Object[] a, int i) { return a[i]; } 132 133 static void setElementI(int[] a, int i, int x) { a[i] = x; } 134 static void setElementJ(long[] a, int i, long x) { a[i] = x; } 135 static void setElementF(float[] a, int i, float x) { a[i] = x; } 136 static void setElementD(double[] a, int i, double x) { a[i] = x; } 137 static void setElementZ(boolean[] a, int i, boolean x) { a[i] = x; } 138 static void setElementB(byte[] a, int i, byte x) { a[i] = x; } 139 static void setElementS(short[] a, int i, short x) { a[i] = x; } 140 static void setElementC(char[] a, int i, char x) { a[i] = x; } 141 static void setElementL(Object[] a, int i, Object x) { a[i] = x; } 142 143 static String name(Class<?> arrayClass, boolean isSetter) { 144 Class<?> elemClass = arrayClass.getComponentType(); 145 if (elemClass == null) throw newIllegalArgumentException("not an array", arrayClass); 146 return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass); 147 } 148 static MethodType type(Class<?> arrayClass, boolean isSetter) { 149 Class<?> elemClass = arrayClass.getComponentType(); 150 Class<?> arrayArgClass = arrayClass; 151 if (!elemClass.isPrimitive()) { 152 arrayArgClass = Object[].class; 153 elemClass = Object.class; 154 } 155 return !isSetter ? 156 MethodType.methodType(elemClass, arrayArgClass, int.class) : 157 MethodType.methodType(void.class, arrayArgClass, int.class, elemClass); 158 } 159 static MethodType correctType(Class<?> arrayClass, boolean isSetter) { 160 Class<?> elemClass = arrayClass.getComponentType(); 161 return !isSetter ? 162 MethodType.methodType(elemClass, arrayClass, int.class) : 163 MethodType.methodType(void.class, arrayClass, int.class, elemClass); 164 } 165 static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) { 166 String name = name(arrayClass, isSetter); 167 MethodType type = type(arrayClass, isSetter); 168 try { 169 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type); 170 } catch (ReflectiveOperationException ex) { 171 throw uncaughtException(ex); 172 } 173 } 174 } 175 176 /** 177 * Create a JVM-level adapter method handle to conform the given method 178 * handle to the similar newType, using only pairwise argument conversions. 179 * For each argument, convert incoming argument to the exact type needed. 180 * The argument conversions allowed are casting, boxing and unboxing, 181 * integral widening or narrowing, and floating point widening or narrowing. 182 * @param srcType required call type 183 * @param target original method handle 184 * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed 185 * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double) 186 * @return an adapter to the original handle with the desired new type, 187 * or the original target if the types are already identical 188 * or null if the adaptation cannot be made 189 */ 190 static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, 191 boolean strict, boolean monobox) { 192 MethodType dstType = target.type(); 193 if (srcType == dstType) 194 return target; 195 return makePairwiseConvertByEditor(target, srcType, strict, monobox); 196 } 197 198 private static int countNonNull(Object[] array) { 199 int count = 0; 200 for (Object x : array) { 201 if (x != null) ++count; 202 } 203 return count; 204 } 205 206 static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType, 207 boolean strict, boolean monobox) { 208 Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox); 209 int convCount = countNonNull(convSpecs); 210 if (convCount == 0) 211 return target.viewAsType(srcType, strict); 212 MethodType basicSrcType = srcType.basicType(); 213 MethodType midType = target.type().basicType(); 214 BoundMethodHandle mh = target.rebind(); 215 // FIXME: Reduce number of bindings when there is more than one Class conversion. 216 // FIXME: Reduce number of bindings when there are repeated conversions. 217 for (int i = 0; i < convSpecs.length-1; i++) { 218 Object convSpec = convSpecs[i]; 219 if (convSpec == null) continue; 220 MethodHandle fn; 221 if (convSpec instanceof Class) { 222 fn = Lazy.MH_castReference.bindTo(convSpec); 223 } else { 224 fn = (MethodHandle) convSpec; 225 } 226 Class<?> newType = basicSrcType.parameterType(i); 227 if (--convCount == 0) 228 midType = srcType; 229 else 230 midType = midType.changeParameterType(i, newType); 231 LambdaForm form2 = mh.editor().filterArgumentForm(1+i, BasicType.basicType(newType)); 232 mh = mh.copyWithExtendL(midType, form2, fn); 233 mh = mh.rebind(); 234 } 235 Object convSpec = convSpecs[convSpecs.length-1]; 236 if (convSpec != null) { 237 MethodHandle fn; 238 if (convSpec instanceof Class) { 239 if (convSpec == void.class) 240 fn = null; 241 else 242 fn = Lazy.MH_castReference.bindTo(convSpec); 243 } else { 244 fn = (MethodHandle) convSpec; 245 } 246 Class<?> newType = basicSrcType.returnType(); 247 assert(--convCount == 0); 248 midType = srcType; 249 if (fn != null) { 250 mh = mh.rebind(); // rebind if too complex 251 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false); 252 mh = mh.copyWithExtendL(midType, form2, fn); 253 } else { 254 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true); 255 mh = mh.copyWith(midType, form2); 256 } 257 } 258 assert(convCount == 0); 259 assert(mh.type().equals(srcType)); 260 return mh; 261 } 262 263 static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType, 264 boolean strict, boolean monobox) { 265 assert(target.type().parameterCount() == srcType.parameterCount()); 266 // Calculate extra arguments (temporaries) required in the names array. 267 Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox); 268 final int INARG_COUNT = srcType.parameterCount(); 269 int convCount = countNonNull(convSpecs); 270 boolean retConv = (convSpecs[INARG_COUNT] != null); 271 boolean retVoid = srcType.returnType() == void.class; 272 if (retConv && retVoid) { 273 convCount -= 1; 274 retConv = false; 275 } 276 277 final int IN_MH = 0; 278 final int INARG_BASE = 1; 279 final int INARG_LIMIT = INARG_BASE + INARG_COUNT; 280 final int NAME_LIMIT = INARG_LIMIT + convCount + 1; 281 final int RETURN_CONV = (!retConv ? -1 : NAME_LIMIT - 1); 282 final int OUT_CALL = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1; 283 final int RESULT = (retVoid ? -1 : NAME_LIMIT - 1); 284 285 // Now build a LambdaForm. 286 MethodType lambdaType = srcType.basicType().invokerType(); 287 Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType); 288 289 // Collect the arguments to the outgoing call, maybe with conversions: 290 final int OUTARG_BASE = 0; // target MH is Name.function, name Name.arguments[0] 291 Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT]; 292 293 int nameCursor = INARG_LIMIT; 294 for (int i = 0; i < INARG_COUNT; i++) { 295 Object convSpec = convSpecs[i]; 296 if (convSpec == null) { 297 // do nothing: difference is trivial 298 outArgs[OUTARG_BASE + i] = names[INARG_BASE + i]; 299 continue; 300 } 301 302 Name conv; 303 if (convSpec instanceof Class) { 304 Class<?> convClass = (Class<?>) convSpec; 305 conv = new Name(Lazy.MH_castReference, convClass, names[INARG_BASE + i]); 306 } else { 307 MethodHandle fn = (MethodHandle) convSpec; 308 conv = new Name(fn, names[INARG_BASE + i]); 309 } 310 assert(names[nameCursor] == null); 311 names[nameCursor++] = conv; 312 assert(outArgs[OUTARG_BASE + i] == null); 313 outArgs[OUTARG_BASE + i] = conv; 314 } 315 316 // Build argument array for the call. 317 assert(nameCursor == OUT_CALL); 318 names[OUT_CALL] = new Name(target, outArgs); 319 320 Object convSpec = convSpecs[INARG_COUNT]; 321 if (!retConv) { 322 assert(OUT_CALL == names.length-1); 323 } else { 324 Name conv; 325 if (convSpec == void.class) { 326 conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType()))); 327 } else if (convSpec instanceof Class) { 328 Class<?> convClass = (Class<?>) convSpec; 329 conv = new Name(Lazy.MH_castReference, convClass, names[OUT_CALL]); 330 } else { 331 MethodHandle fn = (MethodHandle) convSpec; 332 if (fn.type().parameterCount() == 0) 333 conv = new Name(fn); // don't pass retval to void conversion 334 else 335 conv = new Name(fn, names[OUT_CALL]); 336 } 337 assert(names[RETURN_CONV] == null); 338 names[RETURN_CONV] = conv; 339 assert(RETURN_CONV == names.length-1); 340 } 341 342 LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names, RESULT); 343 return SimpleMethodHandle.make(srcType, form); 344 } 345 346 /** 347 * Identity function, with reference cast. 348 * @param t an arbitrary reference type 349 * @param x an arbitrary reference value 350 * @return the same value x 351 */ 352 @ForceInline 353 @SuppressWarnings("unchecked") 354 static <T,U> T castReference(Class<? extends T> t, U x) { 355 // inlined Class.cast because we can't ForceInline it 356 if (x != null && !t.isInstance(x)) 357 throw newClassCastException(t, x); 358 return (T) x; 359 } 360 361 private static ClassCastException newClassCastException(Class<?> t, Object obj) { 362 return new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + t.getName()); 363 } 364 365 static Object[] computeValueConversions(MethodType srcType, MethodType dstType, 366 boolean strict, boolean monobox) { 367 final int INARG_COUNT = srcType.parameterCount(); 368 Object[] convSpecs = new Object[INARG_COUNT+1]; 369 for (int i = 0; i <= INARG_COUNT; i++) { 370 boolean isRet = (i == INARG_COUNT); 371 Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i); 372 Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i); 373 if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) { 374 convSpecs[i] = valueConversion(src, dst, strict, monobox); 375 } 376 } 377 return convSpecs; 378 } 379 static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, 380 boolean strict) { 381 return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false); 382 } 383 384 /** 385 * Find a conversion function from the given source to the given destination. 386 * This conversion function will be used as a LF NamedFunction. 387 * Return a Class object if a simple cast is needed. 388 * Return void.class if void is involved. 389 */ 390 static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) { 391 assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)); // caller responsibility 392 if (dst == void.class) 393 return dst; 394 MethodHandle fn; 395 if (src.isPrimitive()) { 396 if (src == void.class) { 397 return void.class; // caller must recognize this specially 398 } else if (dst.isPrimitive()) { 399 // Examples: int->byte, byte->int, boolean->int (!strict) 400 fn = ValueConversions.convertPrimitive(src, dst); 401 } else { 402 // Examples: int->Integer, boolean->Object, float->Number 403 Wrapper wsrc = Wrapper.forPrimitiveType(src); 404 fn = ValueConversions.boxExact(wsrc); 405 assert(fn.type().parameterType(0) == wsrc.primitiveType()); 406 assert(fn.type().returnType() == wsrc.wrapperType()); 407 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) { 408 // Corner case, such as int->Long, which will probably fail. 409 MethodType mt = MethodType.methodType(dst, src); 410 if (strict) 411 fn = fn.asType(mt); 412 else 413 fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false); 414 } 415 } 416 } else if (dst.isPrimitive()) { 417 Wrapper wdst = Wrapper.forPrimitiveType(dst); 418 if (monobox || src == wdst.wrapperType()) { 419 // Use a strongly-typed unboxer, if possible. 420 fn = ValueConversions.unboxExact(wdst, strict); 421 } else { 422 // Examples: Object->int, Number->int, Comparable->int, Byte->int 423 // must include additional conversions 424 // src must be examined at runtime, to detect Byte, Character, etc. 425 fn = (strict 426 ? ValueConversions.unboxWiden(wdst) 427 : ValueConversions.unboxCast(wdst)); 428 } 429 } else { 430 // Simple reference conversion. 431 // Note: Do not check for a class hierarchy relation 432 // between src and dst. In all cases a 'null' argument 433 // will pass the cast conversion. 434 return dst; 435 } 436 assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn); 437 return fn; 438 } 439 440 static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) { 441 MethodType type = target.type(); 442 int last = type.parameterCount() - 1; 443 if (type.parameterType(last) != arrayType) 444 target = target.asType(type.changeParameterType(last, arrayType)); 445 target = target.asFixedArity(); // make sure this attribute is turned off 446 return new AsVarargsCollector(target, arrayType); 447 } 448 449 private static final class AsVarargsCollector extends DelegatingMethodHandle { 450 private final MethodHandle target; 451 private final Class<?> arrayType; 452 private @Stable MethodHandle asCollectorCache; 453 454 AsVarargsCollector(MethodHandle target, Class<?> arrayType) { 455 this(target.type(), target, arrayType); 456 } 457 AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) { 458 super(type, target); 459 this.target = target; 460 this.arrayType = arrayType; 461 this.asCollectorCache = target.asCollector(arrayType, 0); 462 } 463 464 @Override 465 public boolean isVarargsCollector() { 466 return true; 467 } 468 469 @Override 470 protected MethodHandle getTarget() { 471 return target; 472 } 473 474 @Override 475 public MethodHandle asFixedArity() { 476 return target; 477 } 478 479 @Override 480 MethodHandle setVarargs(MemberName member) { 481 if (member.isVarargs()) return this; 482 return asFixedArity(); 483 } 484 485 @Override 486 public MethodHandle asTypeUncached(MethodType newType) { 487 MethodType type = this.type(); 488 int collectArg = type.parameterCount() - 1; 489 int newArity = newType.parameterCount(); 490 if (newArity == collectArg+1 && 491 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) { 492 // if arity and trailing parameter are compatible, do normal thing 493 return asTypeCache = asFixedArity().asType(newType); 494 } 495 // check cache 496 MethodHandle acc = asCollectorCache; 497 if (acc != null && acc.type().parameterCount() == newArity) 498 return asTypeCache = acc.asType(newType); 499 // build and cache a collector 500 int arrayLength = newArity - collectArg; 501 MethodHandle collector; 502 try { 503 collector = asFixedArity().asCollector(arrayType, arrayLength); 504 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector; 505 } catch (IllegalArgumentException ex) { 506 throw new WrongMethodTypeException("cannot build collector", ex); 507 } 508 asCollectorCache = collector; 509 return asTypeCache = collector.asType(newType); 510 } 511 512 @Override 513 boolean viewAsTypeChecks(MethodType newType, boolean strict) { 514 super.viewAsTypeChecks(newType, true); 515 if (strict) return true; 516 // extra assertion for non-strict checks: 517 assert (type().lastParameterType().getComponentType() 518 .isAssignableFrom( 519 newType.lastParameterType().getComponentType())) 520 : Arrays.asList(this, newType); 521 return true; 522 } 523 } 524 525 /** Factory method: Spread selected argument. */ 526 static MethodHandle makeSpreadArguments(MethodHandle target, 527 Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) { 528 MethodType targetType = target.type(); 529 530 for (int i = 0; i < spreadArgCount; i++) { 531 Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i); 532 if (arg == null) arg = Object.class; 533 targetType = targetType.changeParameterType(spreadArgPos + i, arg); 534 } 535 target = target.asType(targetType); 536 537 MethodType srcType = targetType 538 .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType); 539 // Now build a LambdaForm. 540 MethodType lambdaType = srcType.invokerType(); 541 Name[] names = arguments(spreadArgCount + 2, lambdaType); 542 int nameCursor = lambdaType.parameterCount(); 543 int[] indexes = new int[targetType.parameterCount()]; 544 545 for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) { 546 Class<?> src = lambdaType.parameterType(i); 547 if (i == spreadArgPos) { 548 // Spread the array. 549 MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType); 550 Name array = names[argIndex]; 551 names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount); 552 for (int j = 0; j < spreadArgCount; i++, j++) { 553 indexes[i] = nameCursor; 554 names[nameCursor++] = new Name(aload, array, j); 555 } 556 } else if (i < indexes.length) { 557 indexes[i] = argIndex; 558 } 559 } 560 assert(nameCursor == names.length-1); // leave room for the final call 561 562 // Build argument array for the call. 563 Name[] targetArgs = new Name[targetType.parameterCount()]; 564 for (int i = 0; i < targetType.parameterCount(); i++) { 565 int idx = indexes[i]; 566 targetArgs[i] = names[idx]; 567 } 568 names[names.length - 1] = new Name(target, (Object[]) targetArgs); 569 570 LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names); 571 return SimpleMethodHandle.make(srcType, form); 572 } 573 574 static void checkSpreadArgument(Object av, int n) { 575 if (av == null) { 576 if (n == 0) return; 577 } else if (av instanceof Object[]) { 578 int len = ((Object[])av).length; 579 if (len == n) return; 580 } else { 581 int len = java.lang.reflect.Array.getLength(av); 582 if (len == n) return; 583 } 584 // fall through to error: 585 throw newIllegalArgumentException("array is not of length "+n); 586 } 587 588 /** 589 * Pre-initialized NamedFunctions for bootstrapping purposes. 590 * Factored in an inner class to delay initialization until first usage. 591 */ 592 static class Lazy { 593 private static final Class<?> MHI = MethodHandleImpl.class; 594 595 private static final MethodHandle[] ARRAYS; 596 private static final MethodHandle[] FILL_ARRAYS; 597 598 static final NamedFunction NF_checkSpreadArgument; 599 static final NamedFunction NF_guardWithCatch; 600 static final NamedFunction NF_throwException; 601 static final NamedFunction NF_profileBoolean; 602 603 static final MethodHandle MH_castReference; 604 static final MethodHandle MH_selectAlternative; 605 static final MethodHandle MH_copyAsPrimitiveArray; 606 static final MethodHandle MH_fillNewTypedArray; 607 static final MethodHandle MH_fillNewArray; 608 static final MethodHandle MH_arrayIdentity; 609 610 static { 611 ARRAYS = makeArrays(); 612 FILL_ARRAYS = makeFillArrays(); 613 614 try { 615 NF_checkSpreadArgument = new NamedFunction(MHI.getDeclaredMethod("checkSpreadArgument", Object.class, int.class)); 616 NF_guardWithCatch = new NamedFunction(MHI.getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class, 617 MethodHandle.class, Object[].class)); 618 NF_throwException = new NamedFunction(MHI.getDeclaredMethod("throwException", Throwable.class)); 619 NF_profileBoolean = new NamedFunction(MHI.getDeclaredMethod("profileBoolean", boolean.class, int[].class)); 620 621 NF_checkSpreadArgument.resolve(); 622 NF_guardWithCatch.resolve(); 623 NF_throwException.resolve(); 624 NF_profileBoolean.resolve(); 625 626 MH_castReference = IMPL_LOOKUP.findStatic(MHI, "castReference", 627 MethodType.methodType(Object.class, Class.class, Object.class)); 628 MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray", 629 MethodType.methodType(Object.class, Wrapper.class, Object[].class)); 630 MH_arrayIdentity = IMPL_LOOKUP.findStatic(MHI, "identity", 631 MethodType.methodType(Object[].class, Object[].class)); 632 MH_fillNewArray = IMPL_LOOKUP.findStatic(MHI, "fillNewArray", 633 MethodType.methodType(Object[].class, Integer.class, Object[].class)); 634 MH_fillNewTypedArray = IMPL_LOOKUP.findStatic(MHI, "fillNewTypedArray", 635 MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class)); 636 637 MH_selectAlternative = makeIntrinsic( 638 IMPL_LOOKUP.findStatic(MHI, "selectAlternative", 639 MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)), 640 Intrinsic.SELECT_ALTERNATIVE); 641 } catch (ReflectiveOperationException ex) { 642 throw newInternalError(ex); 643 } 644 } 645 } 646 647 /** Factory method: Collect or filter selected argument(s). */ 648 static MethodHandle makeCollectArguments(MethodHandle target, 649 MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) { 650 MethodType targetType = target.type(); // (a..., c, [b...])=>r 651 MethodType collectorType = collector.type(); // (b...)=>c 652 int collectArgCount = collectorType.parameterCount(); 653 Class<?> collectValType = collectorType.returnType(); 654 int collectValCount = (collectValType == void.class ? 0 : 1); 655 MethodType srcType = targetType // (a..., [b...])=>r 656 .dropParameterTypes(collectArgPos, collectArgPos+collectValCount); 657 if (!retainOriginalArgs) { // (a..., b...)=>r 658 srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList()); 659 } 660 // in arglist: [0: ...keep1 | cpos: collect... | cpos+cacount: keep2... ] 661 // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ] 662 // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ] 663 664 // Now build a LambdaForm. 665 MethodType lambdaType = srcType.invokerType(); 666 Name[] names = arguments(2, lambdaType); 667 final int collectNamePos = names.length - 2; 668 final int targetNamePos = names.length - 1; 669 670 Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount); 671 names[collectNamePos] = new Name(collector, (Object[]) collectorArgs); 672 673 // Build argument array for the target. 674 // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ]. 675 // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ]. 676 Name[] targetArgs = new Name[targetType.parameterCount()]; 677 int inputArgPos = 1; // incoming LF args to copy to target 678 int targetArgPos = 0; // fill pointer for targetArgs 679 int chunk = collectArgPos; // |headArgs| 680 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 681 inputArgPos += chunk; 682 targetArgPos += chunk; 683 if (collectValType != void.class) { 684 targetArgs[targetArgPos++] = names[collectNamePos]; 685 } 686 chunk = collectArgCount; 687 if (retainOriginalArgs) { 688 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 689 targetArgPos += chunk; // optionally pass on the collected chunk 690 } 691 inputArgPos += chunk; 692 chunk = targetArgs.length - targetArgPos; // all the rest 693 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 694 assert(inputArgPos + chunk == collectNamePos); // use of rest of input args also 695 names[targetNamePos] = new Name(target, (Object[]) targetArgs); 696 697 LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names); 698 return SimpleMethodHandle.make(srcType, form); 699 } 700 701 @LambdaForm.Hidden 702 static 703 MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) { 704 if (testResult) { 705 return target; 706 } else { 707 return fallback; 708 } 709 } 710 711 // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies. 712 @LambdaForm.Hidden 713 @jdk.internal.HotSpotIntrinsicCandidate 714 static 715 boolean profileBoolean(boolean result, int[] counters) { 716 // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively. 717 int idx = result ? 1 : 0; 718 try { 719 counters[idx] = Math.addExact(counters[idx], 1); 720 } catch (ArithmeticException e) { 721 // Avoid continuous overflow by halving the problematic count. 722 counters[idx] = counters[idx] / 2; 723 } 724 return result; 725 } 726 727 // Intrinsified by C2. Returns true if obj is a compile-time constant. 728 @LambdaForm.Hidden 729 @jdk.internal.HotSpotIntrinsicCandidate 730 static 731 boolean isCompileConstant(Object obj) { 732 return false; 733 } 734 735 static 736 MethodHandle makeGuardWithTest(MethodHandle test, 737 MethodHandle target, 738 MethodHandle fallback) { 739 MethodType type = target.type(); 740 assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type)); 741 MethodType basicType = type.basicType(); 742 LambdaForm form = makeGuardWithTestForm(basicType); 743 BoundMethodHandle mh; 744 try { 745 if (PROFILE_GWT) { 746 int[] counts = new int[2]; 747 mh = (BoundMethodHandle) 748 BoundMethodHandle.speciesData_LLLL().constructor().invokeBasic(type, form, 749 (Object) test, (Object) profile(target), (Object) profile(fallback), counts); 750 } else { 751 mh = (BoundMethodHandle) 752 BoundMethodHandle.speciesData_LLL().constructor().invokeBasic(type, form, 753 (Object) test, (Object) profile(target), (Object) profile(fallback)); 754 } 755 } catch (Throwable ex) { 756 throw uncaughtException(ex); 757 } 758 assert(mh.type() == type); 759 return mh; 760 } 761 762 763 static 764 MethodHandle profile(MethodHandle target) { 765 if (DONT_INLINE_THRESHOLD >= 0) { 766 return makeBlockInliningWrapper(target); 767 } else { 768 return target; 769 } 770 } 771 772 /** 773 * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times. 774 * Corresponding LambdaForm has @DontInline when compiled into bytecode. 775 */ 776 static 777 MethodHandle makeBlockInliningWrapper(MethodHandle target) { 778 LambdaForm lform; 779 if (DONT_INLINE_THRESHOLD > 0) { 780 lform = PRODUCE_BLOCK_INLINING_FORM.apply(target); 781 } else { 782 lform = PRODUCE_REINVOKER_FORM.apply(target); 783 } 784 return new CountingWrapper(target, lform, 785 PRODUCE_BLOCK_INLINING_FORM, PRODUCE_REINVOKER_FORM, 786 DONT_INLINE_THRESHOLD); 787 } 788 789 /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */ 790 private static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() { 791 @Override 792 public LambdaForm apply(MethodHandle target) { 793 return DelegatingMethodHandle.makeReinvokerForm(target, 794 MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, "reinvoker.dontInline", false, 795 DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting); 796 } 797 }; 798 799 /** Constructs simple reinvoker lambda form for a particular method handle */ 800 private static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() { 801 @Override 802 public LambdaForm apply(MethodHandle target) { 803 return DelegatingMethodHandle.makeReinvokerForm(target, 804 MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget); 805 } 806 }; 807 808 /** 809 * Counting method handle. It has 2 states: counting and non-counting. 810 * It is in counting state for the first n invocations and then transitions to non-counting state. 811 * Behavior in counting and non-counting states is determined by lambda forms produced by 812 * countingFormProducer & nonCountingFormProducer respectively. 813 */ 814 static class CountingWrapper extends DelegatingMethodHandle { 815 private final MethodHandle target; 816 private int count; 817 private Function<MethodHandle, LambdaForm> countingFormProducer; 818 private Function<MethodHandle, LambdaForm> nonCountingFormProducer; 819 private volatile boolean isCounting; 820 821 private CountingWrapper(MethodHandle target, LambdaForm lform, 822 Function<MethodHandle, LambdaForm> countingFromProducer, 823 Function<MethodHandle, LambdaForm> nonCountingFormProducer, 824 int count) { 825 super(target.type(), lform); 826 this.target = target; 827 this.count = count; 828 this.countingFormProducer = countingFromProducer; 829 this.nonCountingFormProducer = nonCountingFormProducer; 830 this.isCounting = (count > 0); 831 } 832 833 @Hidden 834 @Override 835 protected MethodHandle getTarget() { 836 return target; 837 } 838 839 @Override 840 public MethodHandle asTypeUncached(MethodType newType) { 841 MethodHandle newTarget = target.asType(newType); 842 MethodHandle wrapper; 843 if (isCounting) { 844 LambdaForm lform; 845 lform = countingFormProducer.apply(newTarget); 846 wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD); 847 } else { 848 wrapper = newTarget; // no need for a counting wrapper anymore 849 } 850 return (asTypeCache = wrapper); 851 } 852 853 boolean countDown() { 854 int c = count; 855 if (c <= 1) { 856 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility. 857 if (isCounting) { 858 isCounting = false; 859 return true; 860 } else { 861 return false; 862 } 863 } else { 864 count = c - 1; 865 return false; 866 } 867 } 868 869 @Hidden 870 static void maybeStopCounting(Object o1) { 871 CountingWrapper wrapper = (CountingWrapper) o1; 872 if (wrapper.countDown()) { 873 // Reached invocation threshold. Replace counting behavior with a non-counting one. 874 LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target); 875 lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition 876 wrapper.updateForm(lform); 877 } 878 } 879 880 static final NamedFunction NF_maybeStopCounting; 881 static { 882 Class<?> THIS_CLASS = CountingWrapper.class; 883 try { 884 NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class)); 885 } catch (ReflectiveOperationException ex) { 886 throw newInternalError(ex); 887 } 888 } 889 } 890 891 static 892 LambdaForm makeGuardWithTestForm(MethodType basicType) { 893 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT); 894 if (lform != null) return lform; 895 final int THIS_MH = 0; // the BMH_LLL 896 final int ARG_BASE = 1; // start of incoming arguments 897 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 898 int nameCursor = ARG_LIMIT; 899 final int GET_TEST = nameCursor++; 900 final int GET_TARGET = nameCursor++; 901 final int GET_FALLBACK = nameCursor++; 902 final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1; 903 final int CALL_TEST = nameCursor++; 904 final int PROFILE = (GET_COUNTERS != -1) ? nameCursor++ : -1; 905 final int TEST = nameCursor-1; // previous statement: either PROFILE or CALL_TEST 906 final int SELECT_ALT = nameCursor++; 907 final int CALL_TARGET = nameCursor++; 908 assert(CALL_TARGET == SELECT_ALT+1); // must be true to trigger IBG.emitSelectAlternative 909 910 MethodType lambdaType = basicType.invokerType(); 911 Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType); 912 913 BoundMethodHandle.SpeciesData data = 914 (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL() 915 : BoundMethodHandle.speciesData_LLL(); 916 names[THIS_MH] = names[THIS_MH].withConstraint(data); 917 names[GET_TEST] = new Name(data.getterFunction(0), names[THIS_MH]); 918 names[GET_TARGET] = new Name(data.getterFunction(1), names[THIS_MH]); 919 names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]); 920 if (GET_COUNTERS != -1) { 921 names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]); 922 } 923 Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class); 924 925 // call test 926 MethodType testType = basicType.changeReturnType(boolean.class).basicType(); 927 invokeArgs[0] = names[GET_TEST]; 928 names[CALL_TEST] = new Name(testType, invokeArgs); 929 930 // profile branch 931 if (PROFILE != -1) { 932 names[PROFILE] = new Name(Lazy.NF_profileBoolean, names[CALL_TEST], names[GET_COUNTERS]); 933 } 934 // call selectAlternative 935 names[SELECT_ALT] = new Name(Lazy.MH_selectAlternative, names[TEST], names[GET_TARGET], names[GET_FALLBACK]); 936 937 // call target or fallback 938 invokeArgs[0] = names[SELECT_ALT]; 939 names[CALL_TARGET] = new Name(basicType, invokeArgs); 940 941 lform = new LambdaForm("guard", lambdaType.parameterCount(), names, /*forceInline=*/true); 942 943 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform); 944 } 945 946 /** 947 * The LambdaForm shape for catchException combinator is the following: 948 * <blockquote><pre>{@code 949 * guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{ 950 * t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L); 951 * t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L); 952 * t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L); 953 * t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L); 954 * t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L); 955 * t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L); 956 * t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L); 957 * t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I} 958 * }</pre></blockquote> 959 * 960 * argL0 and argL2 are target and catcher method handles. argL1 is exception class. 961 * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[] 962 * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()). 963 * 964 * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms 965 * among catchException combinators with the same basic type. 966 */ 967 private static LambdaForm makeGuardWithCatchForm(MethodType basicType) { 968 MethodType lambdaType = basicType.invokerType(); 969 970 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC); 971 if (lform != null) { 972 return lform; 973 } 974 final int THIS_MH = 0; // the BMH_LLLLL 975 final int ARG_BASE = 1; // start of incoming arguments 976 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 977 978 int nameCursor = ARG_LIMIT; 979 final int GET_TARGET = nameCursor++; 980 final int GET_CLASS = nameCursor++; 981 final int GET_CATCHER = nameCursor++; 982 final int GET_COLLECT_ARGS = nameCursor++; 983 final int GET_UNBOX_RESULT = nameCursor++; 984 final int BOXED_ARGS = nameCursor++; 985 final int TRY_CATCH = nameCursor++; 986 final int UNBOX_RESULT = nameCursor++; 987 988 Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType); 989 990 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL(); 991 names[THIS_MH] = names[THIS_MH].withConstraint(data); 992 names[GET_TARGET] = new Name(data.getterFunction(0), names[THIS_MH]); 993 names[GET_CLASS] = new Name(data.getterFunction(1), names[THIS_MH]); 994 names[GET_CATCHER] = new Name(data.getterFunction(2), names[THIS_MH]); 995 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]); 996 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]); 997 998 // FIXME: rework argument boxing/result unboxing logic for LF interpretation 999 1000 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...); 1001 MethodType collectArgsType = basicType.changeReturnType(Object.class); 1002 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType); 1003 Object[] args = new Object[invokeBasic.type().parameterCount()]; 1004 args[0] = names[GET_COLLECT_ARGS]; 1005 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE); 1006 names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args); 1007 1008 // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L); 1009 Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]}; 1010 names[TRY_CATCH] = new Name(Lazy.NF_guardWithCatch, gwcArgs); 1011 1012 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L); 1013 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class)); 1014 Object[] unboxArgs = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]}; 1015 names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs); 1016 1017 lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names); 1018 1019 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform); 1020 } 1021 1022 static 1023 MethodHandle makeGuardWithCatch(MethodHandle target, 1024 Class<? extends Throwable> exType, 1025 MethodHandle catcher) { 1026 MethodType type = target.type(); 1027 LambdaForm form = makeGuardWithCatchForm(type.basicType()); 1028 1029 // Prepare auxiliary method handles used during LambdaForm interpretation. 1030 // Box arguments and wrap them into Object[]: ValueConversions.array(). 1031 MethodType varargsType = type.changeReturnType(Object[].class); 1032 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType); 1033 // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). 1034 MethodHandle unboxResult; 1035 Class<?> rtype = type.returnType(); 1036 if (rtype.isPrimitive()) { 1037 if (rtype == void.class) { 1038 unboxResult = ValueConversions.ignore(); 1039 } else { 1040 Wrapper w = Wrapper.forPrimitiveType(type.returnType()); 1041 unboxResult = ValueConversions.unboxExact(w); 1042 } 1043 } else { 1044 unboxResult = MethodHandles.identity(Object.class); 1045 } 1046 1047 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL(); 1048 BoundMethodHandle mh; 1049 try { 1050 mh = (BoundMethodHandle) 1051 data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher, 1052 (Object) collectArgs, (Object) unboxResult); 1053 } catch (Throwable ex) { 1054 throw uncaughtException(ex); 1055 } 1056 assert(mh.type() == type); 1057 return mh; 1058 } 1059 1060 /** 1061 * Intrinsified during LambdaForm compilation 1062 * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}). 1063 */ 1064 @LambdaForm.Hidden 1065 static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher, 1066 Object... av) throws Throwable { 1067 // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case. 1068 try { 1069 return target.asFixedArity().invokeWithArguments(av); 1070 } catch (Throwable t) { 1071 if (!exType.isInstance(t)) throw t; 1072 return catcher.asFixedArity().invokeWithArguments(prepend(t, av)); 1073 } 1074 } 1075 1076 /** Prepend an element {@code elem} to an {@code array}. */ 1077 @LambdaForm.Hidden 1078 private static Object[] prepend(Object elem, Object[] array) { 1079 Object[] newArray = new Object[array.length+1]; 1080 newArray[0] = elem; 1081 System.arraycopy(array, 0, newArray, 1, array.length); 1082 return newArray; 1083 } 1084 1085 static 1086 MethodHandle throwException(MethodType type) { 1087 assert(Throwable.class.isAssignableFrom(type.parameterType(0))); 1088 int arity = type.parameterCount(); 1089 if (arity > 1) { 1090 MethodHandle mh = throwException(type.dropParameterTypes(1, arity)); 1091 mh = MethodHandles.dropArguments(mh, 1, type.parameterList().subList(1, arity)); 1092 return mh; 1093 } 1094 return makePairwiseConvert(Lazy.NF_throwException.resolvedHandle(), type, false, true); 1095 } 1096 1097 static <T extends Throwable> Empty throwException(T t) throws T { throw t; } 1098 1099 static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2]; 1100 static MethodHandle fakeMethodHandleInvoke(MemberName method) { 1101 int idx; 1102 assert(method.isMethodHandleInvoke()); 1103 switch (method.getName()) { 1104 case "invoke": idx = 0; break; 1105 case "invokeExact": idx = 1; break; 1106 default: throw new InternalError(method.getName()); 1107 } 1108 MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx]; 1109 if (mh != null) return mh; 1110 MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class, 1111 MethodHandle.class, Object[].class); 1112 mh = throwException(type); 1113 mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle")); 1114 if (!method.getInvocationType().equals(mh.type())) 1115 throw new InternalError(method.toString()); 1116 mh = mh.withInternalMemberName(method, false); 1117 mh = mh.asVarargsCollector(Object[].class); 1118 assert(method.isVarargs()); 1119 FAKE_METHOD_HANDLE_INVOKE[idx] = mh; 1120 return mh; 1121 } 1122 1123 /** 1124 * Create an alias for the method handle which, when called, 1125 * appears to be called from the same class loader and protection domain 1126 * as hostClass. 1127 * This is an expensive no-op unless the method which is called 1128 * is sensitive to its caller. A small number of system methods 1129 * are in this category, including Class.forName and Method.invoke. 1130 */ 1131 static 1132 MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) { 1133 return BindCaller.bindCaller(mh, hostClass); 1134 } 1135 1136 // Put the whole mess into its own nested class. 1137 // That way we can lazily load the code and set up the constants. 1138 private static class BindCaller { 1139 static 1140 MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) { 1141 // Do not use this function to inject calls into system classes. 1142 if (hostClass == null 1143 || (hostClass.isArray() || 1144 hostClass.isPrimitive() || 1145 hostClass.getName().startsWith("java.") || 1146 hostClass.getName().startsWith("sun."))) { 1147 throw new InternalError(); // does not happen, and should not anyway 1148 } 1149 // For simplicity, convert mh to a varargs-like method. 1150 MethodHandle vamh = prepareForInvoker(mh); 1151 // Cache the result of makeInjectedInvoker once per argument class. 1152 MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass); 1153 return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass); 1154 } 1155 1156 private static MethodHandle makeInjectedInvoker(Class<?> hostClass) { 1157 Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null); 1158 if (hostClass.getClassLoader() != bcc.getClassLoader()) 1159 throw new InternalError(hostClass.getName()+" (CL)"); 1160 try { 1161 if (hostClass.getProtectionDomain() != bcc.getProtectionDomain()) 1162 throw new InternalError(hostClass.getName()+" (PD)"); 1163 } catch (SecurityException ex) { 1164 // Self-check was blocked by security manager. This is OK. 1165 // In fact the whole try body could be turned into an assertion. 1166 } 1167 try { 1168 MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class)); 1169 init.invokeExact(); // force initialization of the class 1170 } catch (Throwable ex) { 1171 throw uncaughtException(ex); 1172 } 1173 MethodHandle bccInvoker; 1174 try { 1175 MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class); 1176 bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT); 1177 } catch (ReflectiveOperationException ex) { 1178 throw uncaughtException(ex); 1179 } 1180 // Test the invoker, to ensure that it really injects into the right place. 1181 try { 1182 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass); 1183 Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc}); 1184 } catch (Throwable ex) { 1185 throw new InternalError(ex); 1186 } 1187 return bccInvoker; 1188 } 1189 private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() { 1190 @Override protected MethodHandle computeValue(Class<?> hostClass) { 1191 return makeInjectedInvoker(hostClass); 1192 } 1193 }; 1194 1195 // Adapt mh so that it can be called directly from an injected invoker: 1196 private static MethodHandle prepareForInvoker(MethodHandle mh) { 1197 mh = mh.asFixedArity(); 1198 MethodType mt = mh.type(); 1199 int arity = mt.parameterCount(); 1200 MethodHandle vamh = mh.asType(mt.generic()); 1201 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames 1202 vamh = vamh.asSpreader(Object[].class, arity); 1203 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames 1204 return vamh; 1205 } 1206 1207 // Undo the adapter effect of prepareForInvoker: 1208 private static MethodHandle restoreToType(MethodHandle vamh, 1209 MethodHandle original, 1210 Class<?> hostClass) { 1211 MethodType type = original.type(); 1212 MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount()); 1213 MemberName member = original.internalMemberName(); 1214 mh = mh.asType(type); 1215 mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass); 1216 return mh; 1217 } 1218 1219 private static final MethodHandle MH_checkCallerClass; 1220 static { 1221 final Class<?> THIS_CLASS = BindCaller.class; 1222 assert(checkCallerClass(THIS_CLASS, THIS_CLASS)); 1223 try { 1224 MH_checkCallerClass = IMPL_LOOKUP 1225 .findStatic(THIS_CLASS, "checkCallerClass", 1226 MethodType.methodType(boolean.class, Class.class, Class.class)); 1227 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS)); 1228 } catch (Throwable ex) { 1229 throw new InternalError(ex); 1230 } 1231 } 1232 1233 @CallerSensitive 1234 private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) { 1235 // This method is called via MH_checkCallerClass and so it's 1236 // correct to ask for the immediate caller here. 1237 Class<?> actual = Reflection.getCallerClass(); 1238 if (actual != expected && actual != expected2) 1239 throw new InternalError("found "+actual.getName()+", expected "+expected.getName() 1240 +(expected == expected2 ? "" : ", or else "+expected2.getName())); 1241 return true; 1242 } 1243 1244 private static final byte[] T_BYTES; 1245 static { 1246 final Object[] values = {null}; 1247 AccessController.doPrivileged(new PrivilegedAction<>() { 1248 public Void run() { 1249 try { 1250 Class<T> tClass = T.class; 1251 String tName = tClass.getName(); 1252 String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class"; 1253 java.net.URLConnection uconn = tClass.getResource(tResource).openConnection(); 1254 int len = uconn.getContentLength(); 1255 byte[] bytes = new byte[len]; 1256 try (java.io.InputStream str = uconn.getInputStream()) { 1257 int nr = str.read(bytes); 1258 if (nr != len) throw new java.io.IOException(tResource); 1259 } 1260 values[0] = bytes; 1261 } catch (java.io.IOException ex) { 1262 throw new InternalError(ex); 1263 } 1264 return null; 1265 } 1266 }); 1267 T_BYTES = (byte[]) values[0]; 1268 } 1269 1270 // The following class is used as a template for Unsafe.defineAnonymousClass: 1271 private static class T { 1272 static void init() { } // side effect: initializes this class 1273 static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable { 1274 return vamh.invokeExact(args); 1275 } 1276 } 1277 } 1278 1279 1280 /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */ 1281 private static final class WrappedMember extends DelegatingMethodHandle { 1282 private final MethodHandle target; 1283 private final MemberName member; 1284 private final Class<?> callerClass; 1285 private final boolean isInvokeSpecial; 1286 1287 private WrappedMember(MethodHandle target, MethodType type, 1288 MemberName member, boolean isInvokeSpecial, 1289 Class<?> callerClass) { 1290 super(type, target); 1291 this.target = target; 1292 this.member = member; 1293 this.callerClass = callerClass; 1294 this.isInvokeSpecial = isInvokeSpecial; 1295 } 1296 1297 @Override 1298 MemberName internalMemberName() { 1299 return member; 1300 } 1301 @Override 1302 Class<?> internalCallerClass() { 1303 return callerClass; 1304 } 1305 @Override 1306 boolean isInvokeSpecial() { 1307 return isInvokeSpecial; 1308 } 1309 @Override 1310 protected MethodHandle getTarget() { 1311 return target; 1312 } 1313 @Override 1314 public MethodHandle asTypeUncached(MethodType newType) { 1315 // This MH is an alias for target, except for the MemberName 1316 // Drop the MemberName if there is any conversion. 1317 return asTypeCache = target.asType(newType); 1318 } 1319 } 1320 1321 static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) { 1322 if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial()) 1323 return target; 1324 return new WrappedMember(target, target.type(), member, isInvokeSpecial, null); 1325 } 1326 1327 /** Intrinsic IDs */ 1328 /*non-public*/ 1329 enum Intrinsic { 1330 SELECT_ALTERNATIVE, 1331 GUARD_WITH_CATCH, 1332 NEW_ARRAY, 1333 ARRAY_LOAD, 1334 ARRAY_STORE, 1335 IDENTITY, 1336 ZERO, 1337 NONE // no intrinsic associated 1338 } 1339 1340 /** Mark arbitrary method handle as intrinsic. 1341 * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */ 1342 private static final class IntrinsicMethodHandle extends DelegatingMethodHandle { 1343 private final MethodHandle target; 1344 private final Intrinsic intrinsicName; 1345 1346 IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) { 1347 super(target.type(), target); 1348 this.target = target; 1349 this.intrinsicName = intrinsicName; 1350 } 1351 1352 @Override 1353 protected MethodHandle getTarget() { 1354 return target; 1355 } 1356 1357 @Override 1358 Intrinsic intrinsicName() { 1359 return intrinsicName; 1360 } 1361 1362 @Override 1363 public MethodHandle asTypeUncached(MethodType newType) { 1364 // This MH is an alias for target, except for the intrinsic name 1365 // Drop the name if there is any conversion. 1366 return asTypeCache = target.asType(newType); 1367 } 1368 1369 @Override 1370 String internalProperties() { 1371 return super.internalProperties() + 1372 "\n& Intrinsic="+intrinsicName; 1373 } 1374 1375 @Override 1376 public MethodHandle asCollector(Class<?> arrayType, int arrayLength) { 1377 if (intrinsicName == Intrinsic.IDENTITY) { 1378 MethodType resultType = type().asCollectorType(arrayType, arrayLength); 1379 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength); 1380 return newArray.asType(resultType); 1381 } 1382 return super.asCollector(arrayType, arrayLength); 1383 } 1384 } 1385 1386 static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) { 1387 if (intrinsicName == target.intrinsicName()) 1388 return target; 1389 return new IntrinsicMethodHandle(target, intrinsicName); 1390 } 1391 1392 static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) { 1393 return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName); 1394 } 1395 1396 /// Collection of multiple arguments. 1397 1398 private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) { 1399 MethodType type = MethodType.genericMethodType(nargs) 1400 .changeReturnType(rtype) 1401 .insertParameterTypes(0, ptypes); 1402 try { 1403 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type); 1404 } catch (ReflectiveOperationException ex) { 1405 return null; 1406 } 1407 } 1408 1409 private static final Object[] NO_ARGS_ARRAY = {}; 1410 private static Object[] makeArray(Object... args) { return args; } 1411 private static Object[] array() { return NO_ARGS_ARRAY; } 1412 private static Object[] array(Object a0) 1413 { return makeArray(a0); } 1414 private static Object[] array(Object a0, Object a1) 1415 { return makeArray(a0, a1); } 1416 private static Object[] array(Object a0, Object a1, Object a2) 1417 { return makeArray(a0, a1, a2); } 1418 private static Object[] array(Object a0, Object a1, Object a2, Object a3) 1419 { return makeArray(a0, a1, a2, a3); } 1420 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1421 Object a4) 1422 { return makeArray(a0, a1, a2, a3, a4); } 1423 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1424 Object a4, Object a5) 1425 { return makeArray(a0, a1, a2, a3, a4, a5); } 1426 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1427 Object a4, Object a5, Object a6) 1428 { return makeArray(a0, a1, a2, a3, a4, a5, a6); } 1429 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1430 Object a4, Object a5, Object a6, Object a7) 1431 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); } 1432 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1433 Object a4, Object a5, Object a6, Object a7, 1434 Object a8) 1435 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); } 1436 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1437 Object a4, Object a5, Object a6, Object a7, 1438 Object a8, Object a9) 1439 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } 1440 1441 private static final int ARRAYS_COUNT = 11; 1442 1443 private static MethodHandle[] makeArrays() { 1444 MethodHandle[] mhs = new MethodHandle[MAX_ARITY + 1]; 1445 for (int i = 0; i < ARRAYS_COUNT; i++) { 1446 MethodHandle mh = findCollector("array", i, Object[].class); 1447 mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY); 1448 mhs[i] = mh; 1449 } 1450 assert(assertArrayMethodCount(mhs)); 1451 return mhs; 1452 } 1453 1454 private static boolean assertArrayMethodCount(MethodHandle[] mhs) { 1455 assert(findCollector("array", ARRAYS_COUNT, Object[].class) == null); 1456 for (int i = 0; i < ARRAYS_COUNT; i++) { 1457 assert(mhs[i] != null); 1458 } 1459 return true; 1460 } 1461 1462 // filling versions of the above: 1463 // using Integer len instead of int len and no varargs to avoid bootstrapping problems 1464 private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) { 1465 Object[] a = new Object[len]; 1466 fillWithArguments(a, 0, args); 1467 return a; 1468 } 1469 private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) { 1470 Object[] a = Arrays.copyOf(example, len); 1471 assert(a.getClass() != Object[].class); 1472 fillWithArguments(a, 0, args); 1473 return a; 1474 } 1475 private static void fillWithArguments(Object[] a, int pos, Object... args) { 1476 System.arraycopy(args, 0, a, pos, args.length); 1477 } 1478 // using Integer pos instead of int pos to avoid bootstrapping problems 1479 private static Object[] fillArray(Integer pos, Object[] a, Object a0) 1480 { fillWithArguments(a, pos, a0); return a; } 1481 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1) 1482 { fillWithArguments(a, pos, a0, a1); return a; } 1483 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2) 1484 { fillWithArguments(a, pos, a0, a1, a2); return a; } 1485 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3) 1486 { fillWithArguments(a, pos, a0, a1, a2, a3); return a; } 1487 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1488 Object a4) 1489 { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; } 1490 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1491 Object a4, Object a5) 1492 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; } 1493 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1494 Object a4, Object a5, Object a6) 1495 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; } 1496 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1497 Object a4, Object a5, Object a6, Object a7) 1498 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; } 1499 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1500 Object a4, Object a5, Object a6, Object a7, 1501 Object a8) 1502 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; } 1503 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1504 Object a4, Object a5, Object a6, Object a7, 1505 Object a8, Object a9) 1506 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; } 1507 1508 private static final int FILL_ARRAYS_COUNT = 11; // current number of fillArray methods 1509 1510 private static MethodHandle[] makeFillArrays() { 1511 MethodHandle[] mhs = new MethodHandle[FILL_ARRAYS_COUNT]; 1512 mhs[0] = null; // there is no empty fill; at least a0 is required 1513 for (int i = 1; i < FILL_ARRAYS_COUNT; i++) { 1514 MethodHandle mh = findCollector("fillArray", i, Object[].class, Integer.class, Object[].class); 1515 mhs[i] = mh; 1516 } 1517 assert(assertFillArrayMethodCount(mhs)); 1518 return mhs; 1519 } 1520 1521 private static boolean assertFillArrayMethodCount(MethodHandle[] mhs) { 1522 assert(findCollector("fillArray", FILL_ARRAYS_COUNT, Object[].class, Integer.class, Object[].class) == null); 1523 for (int i = 1; i < FILL_ARRAYS_COUNT; i++) { 1524 assert(mhs[i] != null); 1525 } 1526 return true; 1527 } 1528 1529 private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) { 1530 Object a = w.makeArray(boxes.length); 1531 w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length); 1532 return a; 1533 } 1534 1535 /** Return a method handle that takes the indicated number of Object 1536 * arguments and returns an Object array of them, as if for varargs. 1537 */ 1538 static MethodHandle varargsArray(int nargs) { 1539 MethodHandle mh = Lazy.ARRAYS[nargs]; 1540 if (mh != null) return mh; 1541 mh = buildVarargsArray(Lazy.MH_fillNewArray, Lazy.MH_arrayIdentity, nargs); 1542 assert(assertCorrectArity(mh, nargs)); 1543 mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY); 1544 return Lazy.ARRAYS[nargs] = mh; 1545 } 1546 1547 private static boolean assertCorrectArity(MethodHandle mh, int arity) { 1548 assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh; 1549 return true; 1550 } 1551 1552 // Array identity function (used as Lazy.MH_arrayIdentity). 1553 static <T> T[] identity(T[] x) { 1554 return x; 1555 } 1556 1557 private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) { 1558 // Build up the result mh as a sequence of fills like this: 1559 // finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23)) 1560 // The various fill(_,10*I,___*[J]) are reusable. 1561 int leftLen = Math.min(nargs, LEFT_ARGS); // absorb some arguments immediately 1562 int rightLen = nargs - leftLen; 1563 MethodHandle leftCollector = newArray.bindTo(nargs); 1564 leftCollector = leftCollector.asCollector(Object[].class, leftLen); 1565 MethodHandle mh = finisher; 1566 if (rightLen > 0) { 1567 MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen); 1568 if (mh == Lazy.MH_arrayIdentity) 1569 mh = rightFiller; 1570 else 1571 mh = MethodHandles.collectArguments(mh, 0, rightFiller); 1572 } 1573 if (mh == Lazy.MH_arrayIdentity) 1574 mh = leftCollector; 1575 else 1576 mh = MethodHandles.collectArguments(mh, 0, leftCollector); 1577 return mh; 1578 } 1579 1580 private static final int LEFT_ARGS = FILL_ARRAYS_COUNT - 1; 1581 private static final MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1]; 1582 /** fill_array_to_right(N).invoke(a, argL..arg[N-1]) 1583 * fills a[L]..a[N-1] with corresponding arguments, 1584 * and then returns a. The value L is a global constant (LEFT_ARGS). 1585 */ 1586 private static MethodHandle fillToRight(int nargs) { 1587 MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs]; 1588 if (filler != null) return filler; 1589 filler = buildFiller(nargs); 1590 assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1)); 1591 return FILL_ARRAY_TO_RIGHT[nargs] = filler; 1592 } 1593 private static MethodHandle buildFiller(int nargs) { 1594 if (nargs <= LEFT_ARGS) 1595 return Lazy.MH_arrayIdentity; // no args to fill; return the array unchanged 1596 // we need room for both mh and a in mh.invoke(a, arg*[nargs]) 1597 final int CHUNK = LEFT_ARGS; 1598 int rightLen = nargs % CHUNK; 1599 int midLen = nargs - rightLen; 1600 if (rightLen == 0) { 1601 midLen = nargs - (rightLen = CHUNK); 1602 if (FILL_ARRAY_TO_RIGHT[midLen] == null) { 1603 // build some precursors from left to right 1604 for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK) 1605 if (j > LEFT_ARGS) fillToRight(j); 1606 } 1607 } 1608 if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS); 1609 assert(rightLen > 0); 1610 MethodHandle midFill = fillToRight(midLen); // recursive fill 1611 MethodHandle rightFill = Lazy.FILL_ARRAYS[rightLen].bindTo(midLen); // [midLen..nargs-1] 1612 assert(midFill.type().parameterCount() == 1 + midLen - LEFT_ARGS); 1613 assert(rightFill.type().parameterCount() == 1 + rightLen); 1614 1615 // Combine the two fills: 1616 // right(mid(a, x10..x19), x20..x23) 1617 // The final product will look like this: 1618 // right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23) 1619 if (midLen == LEFT_ARGS) 1620 return rightFill; 1621 else 1622 return MethodHandles.collectArguments(rightFill, 0, midFill); 1623 } 1624 1625 // Type-polymorphic version of varargs maker. 1626 private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS 1627 = new ClassValue<MethodHandle[]>() { 1628 @Override 1629 protected MethodHandle[] computeValue(Class<?> type) { 1630 return new MethodHandle[256]; 1631 } 1632 }; 1633 1634 static final int MAX_JVM_ARITY = 255; // limit imposed by the JVM 1635 1636 /** Return a method handle that takes the indicated number of 1637 * typed arguments and returns an array of them. 1638 * The type argument is the array type. 1639 */ 1640 static MethodHandle varargsArray(Class<?> arrayType, int nargs) { 1641 Class<?> elemType = arrayType.getComponentType(); 1642 if (elemType == null) throw new IllegalArgumentException("not an array: "+arrayType); 1643 // FIXME: Need more special casing and caching here. 1644 if (nargs >= MAX_JVM_ARITY/2 - 1) { 1645 int slots = nargs; 1646 final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1; // 1 for receiver MH 1647 if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive()) 1648 slots *= Wrapper.forPrimitiveType(elemType).stackSlots(); 1649 if (slots > MAX_ARRAY_SLOTS) 1650 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs); 1651 } 1652 if (elemType == Object.class) 1653 return varargsArray(nargs); 1654 // other cases: primitive arrays, subtypes of Object[] 1655 MethodHandle cache[] = TYPED_COLLECTORS.get(elemType); 1656 MethodHandle mh = nargs < cache.length ? cache[nargs] : null; 1657 if (mh != null) return mh; 1658 if (nargs == 0) { 1659 Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0); 1660 mh = MethodHandles.constant(arrayType, example); 1661 } else if (elemType.isPrimitive()) { 1662 MethodHandle builder = Lazy.MH_fillNewArray; 1663 MethodHandle producer = buildArrayProducer(arrayType); 1664 mh = buildVarargsArray(builder, producer, nargs); 1665 } else { 1666 Class<? extends Object[]> objArrayType = arrayType.asSubclass(Object[].class); 1667 Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType); 1668 MethodHandle builder = Lazy.MH_fillNewTypedArray.bindTo(example); 1669 MethodHandle producer = Lazy.MH_arrayIdentity; // must be weakly typed 1670 mh = buildVarargsArray(builder, producer, nargs); 1671 } 1672 mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType))); 1673 mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY); 1674 assert(assertCorrectArity(mh, nargs)); 1675 if (nargs < cache.length) 1676 cache[nargs] = mh; 1677 return mh; 1678 } 1679 1680 private static MethodHandle buildArrayProducer(Class<?> arrayType) { 1681 Class<?> elemType = arrayType.getComponentType(); 1682 assert(elemType.isPrimitive()); 1683 return Lazy.MH_copyAsPrimitiveArray.bindTo(Wrapper.forPrimitiveType(elemType)); 1684 } 1685 1686 /*non-public*/ static void assertSame(Object mh1, Object mh2) { 1687 if (mh1 != mh2) { 1688 String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)", 1689 mh1, ((MethodHandle)mh1).form, 1690 mh2, ((MethodHandle)mh2).form); 1691 throw newInternalError(msg); 1692 } 1693 } 1694 }