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