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