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