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