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