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