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