rev 13050 : 8142334: Improve lazy initialization of java.lang.invoke
Reviewed-by: psandoz, vlivanov, mhaupt

   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.ArrayList;
  31 import java.util.Arrays;
  32 import java.util.Collections;
  33 import java.util.function.Function;
  34 
  35 import sun.invoke.empty.Empty;
  36 import sun.invoke.util.ValueConversions;
  37 import sun.invoke.util.VerifyType;
  38 import sun.invoke.util.Wrapper;
  39 import jdk.internal.HotSpotIntrinsicCandidate;
  40 import sun.reflect.CallerSensitive;
  41 import sun.reflect.Reflection;
  42 import static java.lang.invoke.LambdaForm.*;
  43 import static java.lang.invoke.MethodHandleStatics.*;
  44 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  45 
  46 /**
  47  * Trusted implementation code for MethodHandle.
  48  * @author jrose
  49  */
  50 /*non-public*/ abstract class MethodHandleImpl {
  51     // Do not adjust this except for special platforms:
  52     private static final int MAX_ARITY;
  53     static {
  54         final Object[] values = { 255 };
  55         AccessController.doPrivileged(new PrivilegedAction<>() {
  56             @Override
  57             public Void run() {
  58                 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255);
  59                 return null;
  60             }
  61         });
  62         MAX_ARITY = (Integer) values[0];
  63     }
  64 
  65     /// Factory methods to create method handles:
  66 
  67     static void initStatics() {
  68         // Trigger selected static initializations.
  69         MemberName.Factory.INSTANCE.getClass();
  70     }
  71 
  72     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
  73         if (arrayClass == Object[].class)
  74             return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER);
  75         if (!arrayClass.isArray())
  76             throw newIllegalArgumentException("not an array: "+arrayClass);
  77         MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
  78         int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX);
  79         MethodHandle mh = cache[cacheIndex];
  80         if (mh != null)  return mh;
  81         mh = ArrayAccessor.getAccessor(arrayClass, isSetter);
  82         MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter);
  83         if (mh.type() != correctType) {
  84             assert(mh.type().parameterType(0) == Object[].class);
  85             assert((isSetter ? mh.type().parameterType(2) : mh.type().returnType()) == Object.class);
  86             assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType());
  87             // safe to view non-strictly, because element type follows from array type
  88             mh = mh.viewAsType(correctType, false);
  89         }
  90         mh = makeIntrinsic(mh, (isSetter ? Intrinsic.ARRAY_STORE : Intrinsic.ARRAY_LOAD));
  91         // Atomically update accessor cache.
  92         synchronized(cache) {
  93             if (cache[cacheIndex] == null) {
  94                 cache[cacheIndex] = mh;
  95             } else {
  96                 // Throw away newly constructed accessor and use cached version.
  97                 mh = cache[cacheIndex];
  98             }
  99         }
 100         return mh;
 101     }
 102 
 103     static final class ArrayAccessor {
 104         /// Support for array element access
 105         static final int GETTER_INDEX = 0, SETTER_INDEX = 1, INDEX_LIMIT = 2;
 106         static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
 107                 = new ClassValue<MethodHandle[]>() {
 108                     @Override
 109                     protected MethodHandle[] computeValue(Class<?> type) {
 110                         return new MethodHandle[INDEX_LIMIT];
 111                     }
 112                 };
 113         static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER;
 114         static {
 115             MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
 116             cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, false), Intrinsic.ARRAY_LOAD);
 117             cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, true),  Intrinsic.ARRAY_STORE);
 118 
 119             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
 120             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
 121         }
 122 
 123         static int     getElementI(int[]     a, int i)            { return              a[i]; }
 124         static long    getElementJ(long[]    a, int i)            { return              a[i]; }
 125         static float   getElementF(float[]   a, int i)            { return              a[i]; }
 126         static double  getElementD(double[]  a, int i)            { return              a[i]; }
 127         static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
 128         static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
 129         static short   getElementS(short[]   a, int i)            { return              a[i]; }
 130         static char    getElementC(char[]    a, int i)            { return              a[i]; }
 131         static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
 132 
 133         static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
 134         static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
 135         static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
 136         static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
 137         static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
 138         static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
 139         static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
 140         static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
 141         static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
 142 
 143         static String name(Class<?> arrayClass, boolean isSetter) {
 144             Class<?> elemClass = arrayClass.getComponentType();
 145             if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
 146             return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
 147         }
 148         static MethodType type(Class<?> arrayClass, boolean isSetter) {
 149             Class<?> elemClass = arrayClass.getComponentType();
 150             Class<?> arrayArgClass = arrayClass;
 151             if (!elemClass.isPrimitive()) {
 152                 arrayArgClass = Object[].class;
 153                 elemClass = Object.class;
 154             }
 155             return !isSetter ?
 156                     MethodType.methodType(elemClass,  arrayArgClass, int.class) :
 157                     MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
 158         }
 159         static MethodType correctType(Class<?> arrayClass, boolean isSetter) {
 160             Class<?> elemClass = arrayClass.getComponentType();
 161             return !isSetter ?
 162                     MethodType.methodType(elemClass,  arrayClass, int.class) :
 163                     MethodType.methodType(void.class, arrayClass, int.class, elemClass);
 164         }
 165         static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) {
 166             String     name = name(arrayClass, isSetter);
 167             MethodType type = type(arrayClass, isSetter);
 168             try {
 169                 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
 170             } catch (ReflectiveOperationException ex) {
 171                 throw uncaughtException(ex);
 172             }
 173         }
 174     }
 175 
 176     /**
 177      * Create a JVM-level adapter method handle to conform the given method
 178      * handle to the similar newType, using only pairwise argument conversions.
 179      * For each argument, convert incoming argument to the exact type needed.
 180      * The argument conversions allowed are casting, boxing and unboxing,
 181      * integral widening or narrowing, and floating point widening or narrowing.
 182      * @param srcType required call type
 183      * @param target original method handle
 184      * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
 185      * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
 186      * @return an adapter to the original handle with the desired new type,
 187      *          or the original target if the types are already identical
 188      *          or null if the adaptation cannot be made
 189      */
 190     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 191                                             boolean strict, boolean monobox) {
 192         MethodType dstType = target.type();
 193         if (srcType == dstType)
 194             return target;
 195         return makePairwiseConvertByEditor(target, srcType, strict, monobox);
 196     }
 197 
 198     private static int countNonNull(Object[] array) {
 199         int count = 0;
 200         for (Object x : array) {
 201             if (x != null)  ++count;
 202         }
 203         return count;
 204     }
 205 
 206     static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
 207                                                     boolean strict, boolean monobox) {
 208         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 209         int convCount = countNonNull(convSpecs);
 210         if (convCount == 0)
 211             return target.viewAsType(srcType, strict);
 212         MethodType basicSrcType = srcType.basicType();
 213         MethodType midType = target.type().basicType();
 214         BoundMethodHandle mh = target.rebind();
 215         // FIXME: Reduce number of bindings when there is more than one Class conversion.
 216         // FIXME: Reduce number of bindings when there are repeated conversions.
 217         for (int i = 0; i < convSpecs.length-1; i++) {
 218             Object convSpec = convSpecs[i];
 219             if (convSpec == null)  continue;
 220             MethodHandle fn;
 221             if (convSpec instanceof Class) {
 222                 fn = getConstantHandle(MH_cast).bindTo(convSpec);
 223             } else {
 224                 fn = (MethodHandle) convSpec;
 225             }
 226             Class<?> newType = basicSrcType.parameterType(i);
 227             if (--convCount == 0)
 228                 midType = srcType;
 229             else
 230                 midType = midType.changeParameterType(i, newType);
 231             LambdaForm form2 = mh.editor().filterArgumentForm(1+i, BasicType.basicType(newType));
 232             mh = mh.copyWithExtendL(midType, form2, fn);
 233             mh = mh.rebind();
 234         }
 235         Object convSpec = convSpecs[convSpecs.length-1];
 236         if (convSpec != null) {
 237             MethodHandle fn;
 238             if (convSpec instanceof Class) {
 239                 if (convSpec == void.class)
 240                     fn = null;
 241                 else
 242                     fn = getConstantHandle(MH_cast).bindTo(convSpec);
 243             } else {
 244                 fn = (MethodHandle) convSpec;
 245             }
 246             Class<?> newType = basicSrcType.returnType();
 247             assert(--convCount == 0);
 248             midType = srcType;
 249             if (fn != null) {
 250                 mh = mh.rebind();  // rebind if too complex
 251                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
 252                 mh = mh.copyWithExtendL(midType, form2, fn);
 253             } else {
 254                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
 255                 mh = mh.copyWith(midType, form2);
 256             }
 257         }
 258         assert(convCount == 0);
 259         assert(mh.type().equals(srcType));
 260         return mh;
 261     }
 262 
 263     static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
 264                                                     boolean strict, boolean monobox) {
 265         assert(target.type().parameterCount() == srcType.parameterCount());
 266         // Calculate extra arguments (temporaries) required in the names array.
 267         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 268         final int INARG_COUNT = srcType.parameterCount();
 269         int convCount = countNonNull(convSpecs);
 270         boolean retConv = (convSpecs[INARG_COUNT] != null);
 271         boolean retVoid = srcType.returnType() == void.class;
 272         if (retConv && retVoid) {
 273             convCount -= 1;
 274             retConv = false;
 275         }
 276 
 277         final int IN_MH         = 0;
 278         final int INARG_BASE    = 1;
 279         final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
 280         final int NAME_LIMIT    = INARG_LIMIT + convCount + 1;
 281         final int RETURN_CONV   = (!retConv ? -1         : NAME_LIMIT - 1);
 282         final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
 283         final int RESULT        = (retVoid ? -1 : NAME_LIMIT - 1);
 284 
 285         // Now build a LambdaForm.
 286         MethodType lambdaType = srcType.basicType().invokerType();
 287         Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
 288 
 289         // Collect the arguments to the outgoing call, maybe with conversions:
 290         final int OUTARG_BASE = 0;  // target MH is Name.function, name Name.arguments[0]
 291         Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
 292 
 293         int nameCursor = INARG_LIMIT;
 294         for (int i = 0; i < INARG_COUNT; i++) {
 295             Object convSpec = convSpecs[i];
 296             if (convSpec == null) {
 297                 // do nothing: difference is trivial
 298                 outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
 299                 continue;
 300             }
 301 
 302             Name conv;
 303             if (convSpec instanceof Class) {
 304                 Class<?> convClass = (Class<?>) convSpec;
 305                 conv = new Name(getConstantHandle(MH_cast), convClass, names[INARG_BASE + i]);
 306             } else {
 307                 MethodHandle fn = (MethodHandle) convSpec;
 308                 conv = new Name(fn, names[INARG_BASE + i]);
 309             }
 310             assert(names[nameCursor] == null);
 311             names[nameCursor++] = conv;
 312             assert(outArgs[OUTARG_BASE + i] == null);
 313             outArgs[OUTARG_BASE + i] = conv;
 314         }
 315 
 316         // Build argument array for the call.
 317         assert(nameCursor == OUT_CALL);
 318         names[OUT_CALL] = new Name(target, outArgs);
 319 
 320         Object convSpec = convSpecs[INARG_COUNT];
 321         if (!retConv) {
 322             assert(OUT_CALL == names.length-1);
 323         } else {
 324             Name conv;
 325             if (convSpec == void.class) {
 326                 conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType())));
 327             } else if (convSpec instanceof Class) {
 328                 Class<?> convClass = (Class<?>) convSpec;
 329                 conv = new Name(getConstantHandle(MH_cast), convClass, names[OUT_CALL]);
 330             } else {
 331                 MethodHandle fn = (MethodHandle) convSpec;
 332                 if (fn.type().parameterCount() == 0)
 333                     conv = new Name(fn);  // don't pass retval to void conversion
 334                 else
 335                     conv = new Name(fn, names[OUT_CALL]);
 336             }
 337             assert(names[RETURN_CONV] == null);
 338             names[RETURN_CONV] = conv;
 339             assert(RETURN_CONV == names.length-1);
 340         }
 341 
 342         LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names, RESULT);
 343         return SimpleMethodHandle.make(srcType, form);
 344     }
 345 
 346     static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
 347                                             boolean strict, boolean monobox) {
 348         final int INARG_COUNT = srcType.parameterCount();
 349         Object[] convSpecs = new Object[INARG_COUNT+1];
 350         for (int i = 0; i <= INARG_COUNT; i++) {
 351             boolean isRet = (i == INARG_COUNT);
 352             Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
 353             Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
 354             if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
 355                 convSpecs[i] = valueConversion(src, dst, strict, monobox);
 356             }
 357         }
 358         return convSpecs;
 359     }
 360     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 361                                             boolean strict) {
 362         return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
 363     }
 364 
 365     /**
 366      * Find a conversion function from the given source to the given destination.
 367      * This conversion function will be used as a LF NamedFunction.
 368      * Return a Class object if a simple cast is needed.
 369      * Return void.class if void is involved.
 370      */
 371     static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
 372         assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
 373         if (dst == void.class)
 374             return dst;
 375         MethodHandle fn;
 376         if (src.isPrimitive()) {
 377             if (src == void.class) {
 378                 return void.class;  // caller must recognize this specially
 379             } else if (dst.isPrimitive()) {
 380                 // Examples: int->byte, byte->int, boolean->int (!strict)
 381                 fn = ValueConversions.convertPrimitive(src, dst);
 382             } else {
 383                 // Examples: int->Integer, boolean->Object, float->Number
 384                 Wrapper wsrc = Wrapper.forPrimitiveType(src);
 385                 fn = ValueConversions.boxExact(wsrc);
 386                 assert(fn.type().parameterType(0) == wsrc.primitiveType());
 387                 assert(fn.type().returnType() == wsrc.wrapperType());
 388                 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
 389                     // Corner case, such as int->Long, which will probably fail.
 390                     MethodType mt = MethodType.methodType(dst, src);
 391                     if (strict)
 392                         fn = fn.asType(mt);
 393                     else
 394                         fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
 395                 }
 396             }
 397         } else if (dst.isPrimitive()) {
 398             Wrapper wdst = Wrapper.forPrimitiveType(dst);
 399             if (monobox || src == wdst.wrapperType()) {
 400                 // Use a strongly-typed unboxer, if possible.
 401                 fn = ValueConversions.unboxExact(wdst, strict);
 402             } else {
 403                 // Examples:  Object->int, Number->int, Comparable->int, Byte->int
 404                 // must include additional conversions
 405                 // src must be examined at runtime, to detect Byte, Character, etc.
 406                 fn = (strict
 407                         ? ValueConversions.unboxWiden(wdst)
 408                         : ValueConversions.unboxCast(wdst));
 409             }
 410         } else {
 411             // Simple reference conversion.
 412             // Note:  Do not check for a class hierarchy relation
 413             // between src and dst.  In all cases a 'null' argument
 414             // will pass the cast conversion.
 415             return dst;
 416         }
 417         assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
 418         return fn;
 419     }
 420 
 421     static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
 422         MethodType type = target.type();
 423         int last = type.parameterCount() - 1;
 424         if (type.parameterType(last) != arrayType)
 425             target = target.asType(type.changeParameterType(last, arrayType));
 426         target = target.asFixedArity();  // make sure this attribute is turned off
 427         return new AsVarargsCollector(target, arrayType);
 428     }
 429 
 430     private static final class AsVarargsCollector extends DelegatingMethodHandle {
 431         private final MethodHandle target;
 432         private final Class<?> arrayType;
 433         private @Stable MethodHandle asCollectorCache;
 434 
 435         AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
 436             this(target.type(), target, arrayType);
 437         }
 438         AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
 439             super(type, target);
 440             this.target = target;
 441             this.arrayType = arrayType;
 442             this.asCollectorCache = target.asCollector(arrayType, 0);
 443         }
 444 
 445         @Override
 446         public boolean isVarargsCollector() {
 447             return true;
 448         }
 449 
 450         @Override
 451         protected MethodHandle getTarget() {
 452             return target;
 453         }
 454 
 455         @Override
 456         public MethodHandle asFixedArity() {
 457             return target;
 458         }
 459 
 460         @Override
 461         MethodHandle setVarargs(MemberName member) {
 462             if (member.isVarargs())  return this;
 463             return asFixedArity();
 464         }
 465 
 466         @Override
 467         public MethodHandle asTypeUncached(MethodType newType) {
 468             MethodType type = this.type();
 469             int collectArg = type.parameterCount() - 1;
 470             int newArity = newType.parameterCount();
 471             if (newArity == collectArg+1 &&
 472                 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
 473                 // if arity and trailing parameter are compatible, do normal thing
 474                 return asTypeCache = asFixedArity().asType(newType);
 475             }
 476             // check cache
 477             MethodHandle acc = asCollectorCache;
 478             if (acc != null && acc.type().parameterCount() == newArity)
 479                 return asTypeCache = acc.asType(newType);
 480             // build and cache a collector
 481             int arrayLength = newArity - collectArg;
 482             MethodHandle collector;
 483             try {
 484                 collector = asFixedArity().asCollector(arrayType, arrayLength);
 485                 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
 486             } catch (IllegalArgumentException ex) {
 487                 throw new WrongMethodTypeException("cannot build collector", ex);
 488             }
 489             asCollectorCache = collector;
 490             return asTypeCache = collector.asType(newType);
 491         }
 492 
 493         @Override
 494         boolean viewAsTypeChecks(MethodType newType, boolean strict) {
 495             super.viewAsTypeChecks(newType, true);
 496             if (strict) return true;
 497             // extra assertion for non-strict checks:
 498             assert (type().lastParameterType().getComponentType()
 499                     .isAssignableFrom(
 500                             newType.lastParameterType().getComponentType()))
 501                     : Arrays.asList(this, newType);
 502             return true;
 503         }
 504     }
 505 
 506     /** Factory method:  Spread selected argument. */
 507     static MethodHandle makeSpreadArguments(MethodHandle target,
 508                                             Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
 509         MethodType targetType = target.type();
 510 
 511         for (int i = 0; i < spreadArgCount; i++) {
 512             Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
 513             if (arg == null)  arg = Object.class;
 514             targetType = targetType.changeParameterType(spreadArgPos + i, arg);
 515         }
 516         target = target.asType(targetType);
 517 
 518         MethodType srcType = targetType
 519                 .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
 520         // Now build a LambdaForm.
 521         MethodType lambdaType = srcType.invokerType();
 522         Name[] names = arguments(spreadArgCount + 2, lambdaType);
 523         int nameCursor = lambdaType.parameterCount();
 524         int[] indexes = new int[targetType.parameterCount()];
 525 
 526         for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
 527             Class<?> src = lambdaType.parameterType(i);
 528             if (i == spreadArgPos) {
 529                 // Spread the array.
 530                 MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
 531                 Name array = names[argIndex];
 532                 names[nameCursor++] = new Name(getConstantFunction(NF_checkSpreadArgument), array, spreadArgCount);
 533                 for (int j = 0; j < spreadArgCount; i++, j++) {
 534                     indexes[i] = nameCursor;
 535                     names[nameCursor++] = new Name(aload, array, j);
 536                 }
 537             } else if (i < indexes.length) {
 538                 indexes[i] = argIndex;
 539             }
 540         }
 541         assert(nameCursor == names.length-1);  // leave room for the final call
 542 
 543         // Build argument array for the call.
 544         Name[] targetArgs = new Name[targetType.parameterCount()];
 545         for (int i = 0; i < targetType.parameterCount(); i++) {
 546             int idx = indexes[i];
 547             targetArgs[i] = names[idx];
 548         }
 549         names[names.length - 1] = new Name(target, (Object[]) targetArgs);
 550 
 551         LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names);
 552         return SimpleMethodHandle.make(srcType, form);
 553     }
 554 
 555     static void checkSpreadArgument(Object av, int n) {
 556         if (av == null) {
 557             if (n == 0)  return;
 558         } else if (av instanceof Object[]) {
 559             int len = ((Object[])av).length;
 560             if (len == n)  return;
 561         } else {
 562             int len = java.lang.reflect.Array.getLength(av);
 563             if (len == n)  return;
 564         }
 565         // fall through to error:
 566         throw newIllegalArgumentException("array is not of length "+n);
 567     }
 568 




























































 569     /** Factory method:  Collect or filter selected argument(s). */
 570     static MethodHandle makeCollectArguments(MethodHandle target,
 571                 MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
 572         MethodType targetType = target.type();          // (a..., c, [b...])=>r
 573         MethodType collectorType = collector.type();    // (b...)=>c
 574         int collectArgCount = collectorType.parameterCount();
 575         Class<?> collectValType = collectorType.returnType();
 576         int collectValCount = (collectValType == void.class ? 0 : 1);
 577         MethodType srcType = targetType                 // (a..., [b...])=>r
 578                 .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
 579         if (!retainOriginalArgs) {                      // (a..., b...)=>r
 580             srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
 581         }
 582         // in  arglist: [0: ...keep1 | cpos: collect...  | cpos+cacount: keep2... ]
 583         // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
 584         // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
 585 
 586         // Now build a LambdaForm.
 587         MethodType lambdaType = srcType.invokerType();
 588         Name[] names = arguments(2, lambdaType);
 589         final int collectNamePos = names.length - 2;
 590         final int targetNamePos  = names.length - 1;
 591 
 592         Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
 593         names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
 594 
 595         // Build argument array for the target.
 596         // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
 597         // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
 598         Name[] targetArgs = new Name[targetType.parameterCount()];
 599         int inputArgPos  = 1;  // incoming LF args to copy to target
 600         int targetArgPos = 0;  // fill pointer for targetArgs
 601         int chunk = collectArgPos;  // |headArgs|
 602         System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 603         inputArgPos  += chunk;
 604         targetArgPos += chunk;
 605         if (collectValType != void.class) {
 606             targetArgs[targetArgPos++] = names[collectNamePos];
 607         }
 608         chunk = collectArgCount;
 609         if (retainOriginalArgs) {
 610             System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 611             targetArgPos += chunk;   // optionally pass on the collected chunk
 612         }
 613         inputArgPos += chunk;
 614         chunk = targetArgs.length - targetArgPos;  // all the rest
 615         System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 616         assert(inputArgPos + chunk == collectNamePos);  // use of rest of input args also
 617         names[targetNamePos] = new Name(target, (Object[]) targetArgs);
 618 
 619         LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names);
 620         return SimpleMethodHandle.make(srcType, form);
 621     }
 622 
 623     @LambdaForm.Hidden
 624     static
 625     MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
 626         if (testResult) {
 627             return target;
 628         } else {
 629             return fallback;
 630         }
 631     }
 632 
 633     // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
 634     @LambdaForm.Hidden
 635     @jdk.internal.HotSpotIntrinsicCandidate
 636     static
 637     boolean profileBoolean(boolean result, int[] counters) {
 638         // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
 639         int idx = result ? 1 : 0;
 640         try {
 641             counters[idx] = Math.addExact(counters[idx], 1);
 642         } catch (ArithmeticException e) {
 643             // Avoid continuous overflow by halving the problematic count.
 644             counters[idx] = counters[idx] / 2;
 645         }
 646         return result;
 647     }
 648 
 649     // Intrinsified by C2. Returns true if obj is a compile-time constant.
 650     @LambdaForm.Hidden
 651     @jdk.internal.HotSpotIntrinsicCandidate
 652     static
 653     boolean isCompileConstant(Object obj) {
 654         return false;
 655     }
 656 
 657     static
 658     MethodHandle makeGuardWithTest(MethodHandle test,
 659                                    MethodHandle target,
 660                                    MethodHandle fallback) {
 661         MethodType type = target.type();
 662         assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
 663         MethodType basicType = type.basicType();
 664         LambdaForm form = makeGuardWithTestForm(basicType);
 665         BoundMethodHandle mh;
 666         try {
 667             if (PROFILE_GWT) {
 668                 int[] counts = new int[2];
 669                 mh = (BoundMethodHandle)
 670                         BoundMethodHandle.speciesData_LLLL().constructor().invokeBasic(type, form,
 671                                 (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
 672             } else {
 673                 mh = (BoundMethodHandle)
 674                         BoundMethodHandle.speciesData_LLL().constructor().invokeBasic(type, form,
 675                                 (Object) test, (Object) profile(target), (Object) profile(fallback));
 676             }
 677         } catch (Throwable ex) {
 678             throw uncaughtException(ex);
 679         }
 680         assert(mh.type() == type);
 681         return mh;
 682     }
 683 
 684 
 685     static
 686     MethodHandle profile(MethodHandle target) {
 687         if (DONT_INLINE_THRESHOLD >= 0) {
 688             return makeBlockInliningWrapper(target);
 689         } else {
 690             return target;
 691         }
 692     }
 693 
 694     /**
 695      * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
 696      * Corresponding LambdaForm has @DontInline when compiled into bytecode.
 697      */
 698     static
 699     MethodHandle makeBlockInliningWrapper(MethodHandle target) {
 700         LambdaForm lform;
 701         if (DONT_INLINE_THRESHOLD > 0) {
 702             lform = PRODUCE_BLOCK_INLINING_FORM.apply(target);
 703         } else {
 704             lform = PRODUCE_REINVOKER_FORM.apply(target);
 705         }
 706         return new CountingWrapper(target, lform,
 707                 PRODUCE_BLOCK_INLINING_FORM, PRODUCE_REINVOKER_FORM,
 708                                    DONT_INLINE_THRESHOLD);
 709     }
 710 
 711     /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
 712     private static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
 713         @Override
 714         public LambdaForm apply(MethodHandle target) {
 715             return DelegatingMethodHandle.makeReinvokerForm(target,
 716                                MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, "reinvoker.dontInline", false,
 717                                DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
 718         }
 719     };
 720 
 721     /** Constructs simple reinvoker lambda form for a particular method handle */
 722     private static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
 723         @Override
 724         public LambdaForm apply(MethodHandle target) {
 725             return DelegatingMethodHandle.makeReinvokerForm(target,
 726                     MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
 727         }
 728     };
 729 
 730     /**
 731      * Counting method handle. It has 2 states: counting and non-counting.
 732      * It is in counting state for the first n invocations and then transitions to non-counting state.
 733      * Behavior in counting and non-counting states is determined by lambda forms produced by
 734      * countingFormProducer & nonCountingFormProducer respectively.
 735      */
 736     static class CountingWrapper extends DelegatingMethodHandle {
 737         private final MethodHandle target;
 738         private int count;
 739         private Function<MethodHandle, LambdaForm> countingFormProducer;
 740         private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
 741         private volatile boolean isCounting;
 742 
 743         private CountingWrapper(MethodHandle target, LambdaForm lform,
 744                                 Function<MethodHandle, LambdaForm> countingFromProducer,
 745                                 Function<MethodHandle, LambdaForm> nonCountingFormProducer,
 746                                 int count) {
 747             super(target.type(), lform);
 748             this.target = target;
 749             this.count = count;
 750             this.countingFormProducer = countingFromProducer;
 751             this.nonCountingFormProducer = nonCountingFormProducer;
 752             this.isCounting = (count > 0);
 753         }
 754 
 755         @Hidden
 756         @Override
 757         protected MethodHandle getTarget() {
 758             return target;
 759         }
 760 
 761         @Override
 762         public MethodHandle asTypeUncached(MethodType newType) {
 763             MethodHandle newTarget = target.asType(newType);
 764             MethodHandle wrapper;
 765             if (isCounting) {
 766                 LambdaForm lform;
 767                 lform = countingFormProducer.apply(newTarget);
 768                 wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
 769             } else {
 770                 wrapper = newTarget; // no need for a counting wrapper anymore
 771             }
 772             return (asTypeCache = wrapper);
 773         }
 774 
 775         boolean countDown() {
 776             int c = count;
 777             if (c <= 1) {
 778                 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
 779                 if (isCounting) {
 780                     isCounting = false;
 781                     return true;
 782                 } else {
 783                     return false;
 784                 }
 785             } else {
 786                 count = c - 1;
 787                 return false;
 788             }
 789         }
 790 
 791         @Hidden
 792         static void maybeStopCounting(Object o1) {
 793              CountingWrapper wrapper = (CountingWrapper) o1;
 794              if (wrapper.countDown()) {
 795                  // Reached invocation threshold. Replace counting behavior with a non-counting one.
 796                  LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
 797                  lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
 798                  wrapper.updateForm(lform);
 799              }
 800         }
 801 
 802         static final NamedFunction NF_maybeStopCounting;
 803         static {
 804             Class<?> THIS_CLASS = CountingWrapper.class;
 805             try {
 806                 NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
 807             } catch (ReflectiveOperationException ex) {
 808                 throw newInternalError(ex);
 809             }
 810         }
 811     }
 812 
 813     static
 814     LambdaForm makeGuardWithTestForm(MethodType basicType) {
 815         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
 816         if (lform != null)  return lform;
 817         final int THIS_MH      = 0;  // the BMH_LLL
 818         final int ARG_BASE     = 1;  // start of incoming arguments
 819         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 820         int nameCursor = ARG_LIMIT;
 821         final int GET_TEST     = nameCursor++;
 822         final int GET_TARGET   = nameCursor++;
 823         final int GET_FALLBACK = nameCursor++;
 824         final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
 825         final int CALL_TEST    = nameCursor++;
 826         final int PROFILE      = (GET_COUNTERS != -1) ? nameCursor++ : -1;
 827         final int TEST         = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
 828         final int SELECT_ALT   = nameCursor++;
 829         final int CALL_TARGET  = nameCursor++;
 830         assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
 831 
 832         MethodType lambdaType = basicType.invokerType();
 833         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 834 
 835         BoundMethodHandle.SpeciesData data =
 836                 (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
 837                                      : BoundMethodHandle.speciesData_LLL();
 838         names[THIS_MH] = names[THIS_MH].withConstraint(data);
 839         names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
 840         names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
 841         names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
 842         if (GET_COUNTERS != -1) {
 843             names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
 844         }
 845         Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
 846 
 847         // call test
 848         MethodType testType = basicType.changeReturnType(boolean.class).basicType();
 849         invokeArgs[0] = names[GET_TEST];
 850         names[CALL_TEST] = new Name(testType, invokeArgs);
 851 
 852         // profile branch
 853         if (PROFILE != -1) {
 854             names[PROFILE] = new Name(getConstantFunction(NF_profileBoolean), names[CALL_TEST], names[GET_COUNTERS]);
 855         }
 856         // call selectAlternative
 857         names[SELECT_ALT] = new Name(getConstantHandle(MH_selectAlternative), names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
 858 
 859         // call target or fallback
 860         invokeArgs[0] = names[SELECT_ALT];
 861         names[CALL_TARGET] = new Name(basicType, invokeArgs);
 862 
 863         lform = new LambdaForm("guard", lambdaType.parameterCount(), names, /*forceInline=*/true);
 864 
 865         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
 866     }
 867 
 868     /**
 869      * The LambdaForm shape for catchException combinator is the following:
 870      * <blockquote><pre>{@code
 871      *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
 872      *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
 873      *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
 874      *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
 875      *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
 876      *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
 877      *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
 878      *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
 879      *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
 880      * }</pre></blockquote>
 881      *
 882      * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
 883      * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
 884      * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
 885      *
 886      * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
 887      * among catchException combinators with the same basic type.
 888      */
 889     private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
 890         MethodType lambdaType = basicType.invokerType();
 891 
 892         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
 893         if (lform != null) {
 894             return lform;
 895         }
 896         final int THIS_MH      = 0;  // the BMH_LLLLL
 897         final int ARG_BASE     = 1;  // start of incoming arguments
 898         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 899 
 900         int nameCursor = ARG_LIMIT;
 901         final int GET_TARGET       = nameCursor++;
 902         final int GET_CLASS        = nameCursor++;
 903         final int GET_CATCHER      = nameCursor++;
 904         final int GET_COLLECT_ARGS = nameCursor++;
 905         final int GET_UNBOX_RESULT = nameCursor++;
 906         final int BOXED_ARGS       = nameCursor++;
 907         final int TRY_CATCH        = nameCursor++;
 908         final int UNBOX_RESULT     = nameCursor++;
 909 
 910         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 911 
 912         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 913         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
 914         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
 915         names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
 916         names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
 917         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
 918         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
 919 
 920         // FIXME: rework argument boxing/result unboxing logic for LF interpretation
 921 
 922         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
 923         MethodType collectArgsType = basicType.changeReturnType(Object.class);
 924         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
 925         Object[] args = new Object[invokeBasic.type().parameterCount()];
 926         args[0] = names[GET_COLLECT_ARGS];
 927         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
 928         names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args);
 929 
 930         // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
 931         Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
 932         names[TRY_CATCH] = new Name(getConstantFunction(NF_guardWithCatch), gwcArgs);
 933 
 934         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
 935         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
 936         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
 937         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
 938 
 939         lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names);
 940 
 941         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
 942     }
 943 
 944     static
 945     MethodHandle makeGuardWithCatch(MethodHandle target,
 946                                     Class<? extends Throwable> exType,
 947                                     MethodHandle catcher) {
 948         MethodType type = target.type();
 949         LambdaForm form = makeGuardWithCatchForm(type.basicType());
 950 
 951         // Prepare auxiliary method handles used during LambdaForm interpretation.
 952         // Box arguments and wrap them into Object[]: ValueConversions.array().
 953         MethodType varargsType = type.changeReturnType(Object[].class);
 954         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
 955         // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
 956         MethodHandle unboxResult;
 957         Class<?> rtype = type.returnType();
 958         if (rtype.isPrimitive()) {
 959             if (rtype == void.class) {
 960                 unboxResult = ValueConversions.ignore();
 961             } else {
 962                 Wrapper w = Wrapper.forPrimitiveType(type.returnType());
 963                 unboxResult = ValueConversions.unboxExact(w);
 964             }
 965         } else {
 966             unboxResult = MethodHandles.identity(Object.class);
 967         }
 968 
 969         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 970         BoundMethodHandle mh;
 971         try {
 972             mh = (BoundMethodHandle)
 973                     data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
 974                                                    (Object) collectArgs, (Object) unboxResult);
 975         } catch (Throwable ex) {
 976             throw uncaughtException(ex);
 977         }
 978         assert(mh.type() == type);
 979         return mh;
 980     }
 981 
 982     /**
 983      * Intrinsified during LambdaForm compilation
 984      * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
 985      */
 986     @LambdaForm.Hidden
 987     static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
 988                                  Object... av) throws Throwable {
 989         // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
 990         try {
 991             return target.asFixedArity().invokeWithArguments(av);
 992         } catch (Throwable t) {
 993             if (!exType.isInstance(t)) throw t;
 994             return catcher.asFixedArity().invokeWithArguments(prepend(t, av));
 995         }
 996     }
 997 
 998     /** Prepend an element {@code elem} to an {@code array}. */
 999     @LambdaForm.Hidden
1000     private static Object[] prepend(Object elem, Object[] array) {
1001         Object[] newArray = new Object[array.length+1];
1002         newArray[0] = elem;
1003         System.arraycopy(array, 0, newArray, 1, array.length);
1004         return newArray;
1005     }
1006 
1007     static
1008     MethodHandle throwException(MethodType type) {
1009         assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
1010         int arity = type.parameterCount();
1011         if (arity > 1) {
1012             MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
1013             mh = MethodHandles.dropArguments(mh, 1, type.parameterList().subList(1, arity));
1014             return mh;
1015         }
1016         return makePairwiseConvert(getConstantFunction(NF_throwException).resolvedHandle(), type, false, true);
1017     }
1018 
1019     static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
1020 
1021     static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
1022     static MethodHandle fakeMethodHandleInvoke(MemberName method) {
1023         int idx;
1024         assert(method.isMethodHandleInvoke());
1025         switch (method.getName()) {
1026         case "invoke":       idx = 0; break;
1027         case "invokeExact":  idx = 1; break;
1028         default:             throw new InternalError(method.getName());
1029         }
1030         MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
1031         if (mh != null)  return mh;
1032         MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
1033                                                 MethodHandle.class, Object[].class);
1034         mh = throwException(type);
1035         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
1036         if (!method.getInvocationType().equals(mh.type()))
1037             throw new InternalError(method.toString());
1038         mh = mh.withInternalMemberName(method, false);
1039         mh = mh.asVarargsCollector(Object[].class);
1040         assert(method.isVarargs());
1041         FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
1042         return mh;
1043     }
1044 
1045     /**
1046      * Create an alias for the method handle which, when called,
1047      * appears to be called from the same class loader and protection domain
1048      * as hostClass.
1049      * This is an expensive no-op unless the method which is called
1050      * is sensitive to its caller.  A small number of system methods
1051      * are in this category, including Class.forName and Method.invoke.
1052      */
1053     static
1054     MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1055         return BindCaller.bindCaller(mh, hostClass);
1056     }
1057 
1058     // Put the whole mess into its own nested class.
1059     // That way we can lazily load the code and set up the constants.
1060     private static class BindCaller {
1061         static
1062         MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1063             // Do not use this function to inject calls into system classes.
1064             if (hostClass == null
1065                 ||    (hostClass.isArray() ||
1066                        hostClass.isPrimitive() ||
1067                        hostClass.getName().startsWith("java.") ||
1068                        hostClass.getName().startsWith("sun."))) {
1069                 throw new InternalError();  // does not happen, and should not anyway
1070             }
1071             // For simplicity, convert mh to a varargs-like method.
1072             MethodHandle vamh = prepareForInvoker(mh);
1073             // Cache the result of makeInjectedInvoker once per argument class.
1074             MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
1075             return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
1076         }
1077 
1078         private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
1079             Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
1080             if (hostClass.getClassLoader() != bcc.getClassLoader())
1081                 throw new InternalError(hostClass.getName()+" (CL)");
1082             try {
1083                 if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
1084                     throw new InternalError(hostClass.getName()+" (PD)");
1085             } catch (SecurityException ex) {
1086                 // Self-check was blocked by security manager.  This is OK.
1087                 // In fact the whole try body could be turned into an assertion.
1088             }
1089             try {
1090                 MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
1091                 init.invokeExact();  // force initialization of the class
1092             } catch (Throwable ex) {
1093                 throw uncaughtException(ex);
1094             }
1095             MethodHandle bccInvoker;
1096             try {
1097                 MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1098                 bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
1099             } catch (ReflectiveOperationException ex) {
1100                 throw uncaughtException(ex);
1101             }
1102             // Test the invoker, to ensure that it really injects into the right place.
1103             try {
1104                 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
1105                 Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
1106             } catch (Throwable ex) {
1107                 throw new InternalError(ex);
1108             }
1109             return bccInvoker;
1110         }
1111         private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
1112             @Override protected MethodHandle computeValue(Class<?> hostClass) {
1113                 return makeInjectedInvoker(hostClass);
1114             }
1115         };
1116 
1117         // Adapt mh so that it can be called directly from an injected invoker:
1118         private static MethodHandle prepareForInvoker(MethodHandle mh) {
1119             mh = mh.asFixedArity();
1120             MethodType mt = mh.type();
1121             int arity = mt.parameterCount();
1122             MethodHandle vamh = mh.asType(mt.generic());
1123             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1124             vamh = vamh.asSpreader(Object[].class, arity);
1125             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1126             return vamh;
1127         }
1128 
1129         // Undo the adapter effect of prepareForInvoker:
1130         private static MethodHandle restoreToType(MethodHandle vamh,
1131                                                   MethodHandle original,
1132                                                   Class<?> hostClass) {
1133             MethodType type = original.type();
1134             MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
1135             MemberName member = original.internalMemberName();
1136             mh = mh.asType(type);
1137             mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
1138             return mh;
1139         }
1140 
1141         private static final MethodHandle MH_checkCallerClass;
1142         static {
1143             final Class<?> THIS_CLASS = BindCaller.class;
1144             assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
1145             try {
1146                 MH_checkCallerClass = IMPL_LOOKUP
1147                     .findStatic(THIS_CLASS, "checkCallerClass",
1148                                 MethodType.methodType(boolean.class, Class.class, Class.class));
1149                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
1150             } catch (Throwable ex) {
1151                 throw new InternalError(ex);
1152             }
1153         }
1154 
1155         @CallerSensitive
1156         private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
1157             // This method is called via MH_checkCallerClass and so it's
1158             // correct to ask for the immediate caller here.
1159             Class<?> actual = Reflection.getCallerClass();
1160             if (actual != expected && actual != expected2)
1161                 throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
1162                                         +(expected == expected2 ? "" : ", or else "+expected2.getName()));
1163             return true;
1164         }
1165 
1166         private static final byte[] T_BYTES;
1167         static {
1168             final Object[] values = {null};
1169             AccessController.doPrivileged(new PrivilegedAction<>() {
1170                     public Void run() {
1171                         try {
1172                             Class<T> tClass = T.class;
1173                             String tName = tClass.getName();
1174                             String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
1175                             java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
1176                             int len = uconn.getContentLength();
1177                             byte[] bytes = new byte[len];
1178                             try (java.io.InputStream str = uconn.getInputStream()) {
1179                                 int nr = str.read(bytes);
1180                                 if (nr != len)  throw new java.io.IOException(tResource);
1181                             }
1182                             values[0] = bytes;
1183                         } catch (java.io.IOException ex) {
1184                             throw new InternalError(ex);
1185                         }
1186                         return null;
1187                     }
1188                 });
1189             T_BYTES = (byte[]) values[0];
1190         }
1191 
1192         // The following class is used as a template for Unsafe.defineAnonymousClass:
1193         private static class T {
1194             static void init() { }  // side effect: initializes this class
1195             static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
1196                 return vamh.invokeExact(args);
1197             }
1198         }
1199     }
1200 
1201 
1202     /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
1203     private static final class WrappedMember extends DelegatingMethodHandle {
1204         private final MethodHandle target;
1205         private final MemberName member;
1206         private final Class<?> callerClass;
1207         private final boolean isInvokeSpecial;
1208 
1209         private WrappedMember(MethodHandle target, MethodType type,
1210                               MemberName member, boolean isInvokeSpecial,
1211                               Class<?> callerClass) {
1212             super(type, target);
1213             this.target = target;
1214             this.member = member;
1215             this.callerClass = callerClass;
1216             this.isInvokeSpecial = isInvokeSpecial;
1217         }
1218 
1219         @Override
1220         MemberName internalMemberName() {
1221             return member;
1222         }
1223         @Override
1224         Class<?> internalCallerClass() {
1225             return callerClass;
1226         }
1227         @Override
1228         boolean isInvokeSpecial() {
1229             return isInvokeSpecial;
1230         }
1231         @Override
1232         protected MethodHandle getTarget() {
1233             return target;
1234         }
1235         @Override
1236         public MethodHandle asTypeUncached(MethodType newType) {
1237             // This MH is an alias for target, except for the MemberName
1238             // Drop the MemberName if there is any conversion.
1239             return asTypeCache = target.asType(newType);
1240         }
1241     }
1242 
1243     static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1244         if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
1245             return target;
1246         return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
1247     }
1248 
1249     /** Intrinsic IDs */
1250     /*non-public*/
1251     enum Intrinsic {
1252         SELECT_ALTERNATIVE,
1253         GUARD_WITH_CATCH,
1254         NEW_ARRAY,
1255         ARRAY_LOAD,
1256         ARRAY_STORE,
1257         IDENTITY,
1258         ZERO,
1259         NONE // no intrinsic associated
1260     }
1261 
1262     /** Mark arbitrary method handle as intrinsic.
1263      * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
1264     private static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
1265         private final MethodHandle target;
1266         private final Intrinsic intrinsicName;
1267 
1268         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
1269             super(target.type(), target);
1270             this.target = target;
1271             this.intrinsicName = intrinsicName;
1272         }
1273 
1274         @Override
1275         protected MethodHandle getTarget() {
1276             return target;
1277         }
1278 
1279         @Override
1280         Intrinsic intrinsicName() {
1281             return intrinsicName;
1282         }
1283 
1284         @Override
1285         public MethodHandle asTypeUncached(MethodType newType) {
1286             // This MH is an alias for target, except for the intrinsic name
1287             // Drop the name if there is any conversion.
1288             return asTypeCache = target.asType(newType);
1289         }
1290 
1291         @Override
1292         String internalProperties() {
1293             return super.internalProperties() +
1294                     "\n& Intrinsic="+intrinsicName;
1295         }
1296 
1297         @Override
1298         public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
1299             if (intrinsicName == Intrinsic.IDENTITY) {
1300                 MethodType resultType = type().asCollectorType(arrayType, arrayLength);
1301                 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
1302                 return newArray.asType(resultType);
1303             }
1304             return super.asCollector(arrayType, arrayLength);
1305         }
1306     }
1307 
1308     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
1309         if (intrinsicName == target.intrinsicName())
1310             return target;
1311         return new IntrinsicMethodHandle(target, intrinsicName);
1312     }
1313 
1314     static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
1315         return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
1316     }
1317 
1318     /// Collection of multiple arguments.
1319 
1320     private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) {
1321         MethodType type = MethodType.genericMethodType(nargs)
1322                 .changeReturnType(rtype)
1323                 .insertParameterTypes(0, ptypes);
1324         try {
1325             return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type);
1326         } catch (ReflectiveOperationException ex) {
1327             return null;
1328         }
1329     }
1330 
1331     private static final Object[] NO_ARGS_ARRAY = {};
1332     private static Object[] makeArray(Object... args) { return args; }
1333     private static Object[] array() { return NO_ARGS_ARRAY; }
1334     private static Object[] array(Object a0)
1335                 { return makeArray(a0); }
1336     private static Object[] array(Object a0, Object a1)
1337                 { return makeArray(a0, a1); }
1338     private static Object[] array(Object a0, Object a1, Object a2)
1339                 { return makeArray(a0, a1, a2); }
1340     private static Object[] array(Object a0, Object a1, Object a2, Object a3)
1341                 { return makeArray(a0, a1, a2, a3); }
1342     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1343                                   Object a4)
1344                 { return makeArray(a0, a1, a2, a3, a4); }
1345     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1346                                   Object a4, Object a5)
1347                 { return makeArray(a0, a1, a2, a3, a4, a5); }
1348     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1349                                   Object a4, Object a5, Object a6)
1350                 { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
1351     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1352                                   Object a4, Object a5, Object a6, Object a7)
1353                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
1354     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1355                                   Object a4, Object a5, Object a6, Object a7,
1356                                   Object a8)
1357                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
1358     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1359                                   Object a4, Object a5, Object a6, Object a7,
1360                                   Object a8, Object a9)
1361                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
1362 
1363     private static final int ARRAYS_COUNT = 11;
1364     private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];


















1365 
1366     // filling versions of the above:
1367     // using Integer len instead of int len and no varargs to avoid bootstrapping problems
1368     private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) {
1369         Object[] a = new Object[len];
1370         fillWithArguments(a, 0, args);
1371         return a;
1372     }
1373     private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) {
1374         Object[] a = Arrays.copyOf(example, len);
1375         assert(a.getClass() != Object[].class);
1376         fillWithArguments(a, 0, args);
1377         return a;
1378     }
1379     private static void fillWithArguments(Object[] a, int pos, Object... args) {
1380         System.arraycopy(args, 0, a, pos, args.length);
1381     }
1382     // using Integer pos instead of int pos to avoid bootstrapping problems
1383     private static Object[] fillArray(Integer pos, Object[] a, Object a0)
1384                 { fillWithArguments(a, pos, a0); return a; }
1385     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1)
1386                 { fillWithArguments(a, pos, a0, a1); return a; }
1387     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2)
1388                 { fillWithArguments(a, pos, a0, a1, a2); return a; }
1389     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3)
1390                 { fillWithArguments(a, pos, a0, a1, a2, a3); return a; }
1391     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1392                                   Object a4)
1393                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; }
1394     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1395                                   Object a4, Object a5)
1396                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; }
1397     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1398                                   Object a4, Object a5, Object a6)
1399                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; }
1400     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1401                                   Object a4, Object a5, Object a6, Object a7)
1402                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); 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, Object a7,
1405                                   Object a8)
1406                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; }
1407     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1408                                   Object a4, Object a5, Object a6, Object a7,
1409                                   Object a8, Object a9)
1410                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
1411 
1412     private static final int FILL_ARRAYS_COUNT = 11; // current number of fillArray methods
1413     private static final @Stable MethodHandle[] FILL_ARRAYS = new MethodHandle[FILL_ARRAYS_COUNT];
1414 
1415     private static MethodHandle getFillArray(int count) {
1416         assert (count > 0 && count < FILL_ARRAYS_COUNT); 
1417         MethodHandle mh = FILL_ARRAYS[count];
1418         if (mh != null) {
1419             return mh;










1420         }
1421         mh = findCollector("fillArray", count, Object[].class, Integer.class, Object[].class);
1422         FILL_ARRAYS[count] = mh;
1423         return mh;
1424     }
1425 
1426     private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) {
1427         Object a = w.makeArray(boxes.length);
1428         w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length);
1429         return a;
1430     }
1431 
1432     /** Return a method handle that takes the indicated number of Object
1433      *  arguments and returns an Object array of them, as if for varargs.
1434      */
1435     static MethodHandle varargsArray(int nargs) {
1436         MethodHandle mh = ARRAYS[nargs];
1437         if (mh != null) {
1438             return mh;
1439         }
1440         if (nargs < ARRAYS_COUNT) {
1441             mh = findCollector("array", nargs, Object[].class);
1442         } else {
1443             mh = buildVarargsArray(getConstantHandle(MH_fillNewArray),
1444                     getConstantHandle(MH_arrayIdentity), nargs);
1445         }
1446         assert(assertCorrectArity(mh, nargs));
1447         mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1448         return ARRAYS[nargs] = mh;
1449     }
1450 
1451     private static boolean assertCorrectArity(MethodHandle mh, int arity) {
1452         assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
1453         return true;
1454     }
1455 
1456     // Array identity function (used as getConstantHandle(MH_arrayIdentity)).
1457     static <T> T[] identity(T[] x) {
1458         return x;
1459     }
1460 
1461     private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
1462         // Build up the result mh as a sequence of fills like this:
1463         //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
1464         // The various fill(_,10*I,___*[J]) are reusable.
1465         int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
1466         int rightLen = nargs - leftLen;
1467         MethodHandle leftCollector = newArray.bindTo(nargs);
1468         leftCollector = leftCollector.asCollector(Object[].class, leftLen);
1469         MethodHandle mh = finisher;
1470         if (rightLen > 0) {
1471             MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
1472             if (mh.equals(getConstantHandle(MH_arrayIdentity)))
1473                 mh = rightFiller;
1474             else
1475                 mh = MethodHandles.collectArguments(mh, 0, rightFiller);
1476         }
1477         if (mh.equals(getConstantHandle(MH_arrayIdentity)))
1478             mh = leftCollector;
1479         else
1480             mh = MethodHandles.collectArguments(mh, 0, leftCollector);
1481         return mh;
1482     }
1483 
1484     private static final int LEFT_ARGS = FILL_ARRAYS_COUNT - 1;
1485     private static final @Stable MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1];
1486     /** fill_array_to_right(N).invoke(a, argL..arg[N-1])
1487      *  fills a[L]..a[N-1] with corresponding arguments,
1488      *  and then returns a.  The value L is a global constant (LEFT_ARGS).
1489      */
1490     private static MethodHandle fillToRight(int nargs) {
1491         MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs];
1492         if (filler != null)  return filler;
1493         filler = buildFiller(nargs);
1494         assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1));
1495         return FILL_ARRAY_TO_RIGHT[nargs] = filler;
1496     }
1497     private static MethodHandle buildFiller(int nargs) {
1498         if (nargs <= LEFT_ARGS)
1499             return getConstantHandle(MH_arrayIdentity);  // no args to fill; return the array unchanged
1500         // we need room for both mh and a in mh.invoke(a, arg*[nargs])
1501         final int CHUNK = LEFT_ARGS;
1502         int rightLen = nargs % CHUNK;
1503         int midLen = nargs - rightLen;
1504         if (rightLen == 0) {
1505             midLen = nargs - (rightLen = CHUNK);
1506             if (FILL_ARRAY_TO_RIGHT[midLen] == null) {
1507                 // build some precursors from left to right
1508                 for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK)
1509                     if (j > LEFT_ARGS)  fillToRight(j);
1510             }
1511         }
1512         if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS);
1513         assert(rightLen > 0);
1514         MethodHandle midFill = fillToRight(midLen);  // recursive fill
1515         MethodHandle rightFill = getFillArray(rightLen).bindTo(midLen);  // [midLen..nargs-1]
1516         assert(midFill.type().parameterCount()   == 1 + midLen - LEFT_ARGS);
1517         assert(rightFill.type().parameterCount() == 1 + rightLen);
1518 
1519         // Combine the two fills:
1520         //   right(mid(a, x10..x19), x20..x23)
1521         // The final product will look like this:
1522         //   right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23)
1523         if (midLen == LEFT_ARGS)
1524             return rightFill;
1525         else
1526             return MethodHandles.collectArguments(rightFill, 0, midFill);
1527     }
1528 
1529     // Type-polymorphic version of varargs maker.
1530     private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS
1531         = new ClassValue<MethodHandle[]>() {
1532             @Override
1533             protected MethodHandle[] computeValue(Class<?> type) {
1534                 return new MethodHandle[256];
1535             }
1536     };
1537 
1538     static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
1539 
1540     /** Return a method handle that takes the indicated number of
1541      *  typed arguments and returns an array of them.
1542      *  The type argument is the array type.
1543      */
1544     static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
1545         Class<?> elemType = arrayType.getComponentType();
1546         if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
1547         // FIXME: Need more special casing and caching here.
1548         if (nargs >= MAX_JVM_ARITY/2 - 1) {
1549             int slots = nargs;
1550             final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
1551             if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
1552                 slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
1553             if (slots > MAX_ARRAY_SLOTS)
1554                 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
1555         }
1556         if (elemType == Object.class)
1557             return varargsArray(nargs);
1558         // other cases:  primitive arrays, subtypes of Object[]
1559         MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
1560         MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
1561         if (mh != null)  return mh;
1562         if (nargs == 0) {
1563             Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
1564             mh = MethodHandles.constant(arrayType, example);
1565         } else if (elemType.isPrimitive()) {
1566             MethodHandle builder = getConstantHandle(MH_fillNewArray);
1567             MethodHandle producer = buildArrayProducer(arrayType);
1568             mh = buildVarargsArray(builder, producer, nargs);
1569         } else {
1570             Class<? extends Object[]> objArrayType = arrayType.asSubclass(Object[].class);
1571             Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
1572             MethodHandle builder = getConstantHandle(MH_fillNewTypedArray).bindTo(example);
1573             MethodHandle producer = getConstantHandle(MH_arrayIdentity); // must be weakly typed
1574             mh = buildVarargsArray(builder, producer, nargs);
1575         }
1576         mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
1577         mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1578         assert(assertCorrectArity(mh, nargs));
1579         if (nargs < cache.length)
1580             cache[nargs] = mh;
1581         return mh;
1582     }
1583 
1584     private static MethodHandle buildArrayProducer(Class<?> arrayType) {
1585         Class<?> elemType = arrayType.getComponentType();
1586         assert(elemType.isPrimitive());
1587         return getConstantHandle(MH_copyAsPrimitiveArray).bindTo(Wrapper.forPrimitiveType(elemType));
1588     }
1589 
1590     /*non-public*/ static void assertSame(Object mh1, Object mh2) {
1591         if (mh1 != mh2) {
1592             String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
1593                     mh1, ((MethodHandle)mh1).form,
1594                     mh2, ((MethodHandle)mh2).form);
1595             throw newInternalError(msg);
1596         }
1597     }
1598 
1599     /*non-public*/ static NamedFunction checkSpreadArgumentFunction() {
1600         return getConstantFunction(NF_checkSpreadArgument);
1601     }
1602     
1603     private static NamedFunction getConstantFunction(int idx) {
1604         NamedFunction function = FUNCTIONS[idx];
1605         if (function != null) {
1606             return function;
1607         }
1608         return setCachedFunction(idx, makeConstantFunction(idx));
1609     }
1610 
1611     private static synchronized NamedFunction setCachedFunction(int idx, final NamedFunction function) {
1612         // Simulate a CAS, to avoid racy duplication of results.
1613         NamedFunction prev = FUNCTIONS[idx];
1614         if (prev != null)  return prev;
1615         function.resolve();
1616         UNSAFE.storeFence();
1617         FUNCTIONS[idx] = function;
1618         return function;
1619     }
1620 
1621     // Indexes into constant functions:
1622     private static final int
1623             NF_checkSpreadArgument =  0,
1624             NF_guardWithCatch      =  1,
1625             NF_throwException      =  2,
1626             NF_profileBoolean      =  3,
1627             NF_LIMIT               =  4;
1628 
1629     // Local constant functions:
1630     private static final @Stable NamedFunction[] FUNCTIONS = new NamedFunction[NF_LIMIT];
1631 
1632     private static NamedFunction makeConstantFunction(int idx) {
1633         try {
1634             switch (idx) {
1635                 case NF_checkSpreadArgument:
1636                     return new NamedFunction(MethodHandleImpl.class
1637                             .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
1638                 case NF_guardWithCatch:
1639                     return new NamedFunction(MethodHandleImpl.class
1640                             .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
1641                                     MethodHandle.class, Object[].class));
1642                 case NF_throwException:
1643                     return new NamedFunction(MethodHandleImpl.class
1644                             .getDeclaredMethod("throwException", Throwable.class));
1645                 case NF_profileBoolean:
1646                     return new NamedFunction(MethodHandleImpl.class
1647                             .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
1648             }
1649         } catch (ReflectiveOperationException ex) {
1650             throw newInternalError(ex);
1651         }
1652         throw newInternalError("Unknown function index: " + idx);
1653     }
1654 
1655     private static MethodHandle getConstantHandle(int idx) {
1656         MethodHandle handle = HANDLES[idx];
1657         if (handle != null) {
1658             return handle;
1659         }
1660         return setCachedHandle(idx, makeConstantHandle(idx));
1661     }
1662 
1663     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
1664         // Simulate a CAS, to avoid racy duplication of results.
1665         MethodHandle prev = HANDLES[idx];
1666         if (prev != null) {
1667             return prev;
1668         }
1669         HANDLES[idx] = method;
1670         return method;
1671     }
1672 
1673     
1674     // Indexes into constant method handles:
1675     private static final int
1676             MH_cast                  =  0,
1677             MH_selectAlternative     =  1,
1678             MH_copyAsPrimitiveArray  =  2,
1679             MH_fillNewTypedArray     =  3,
1680             MH_fillNewArray          =  4,
1681             MH_arrayIdentity         =  5,
1682             MH_LIMIT                 =  6;
1683 
1684 
1685     // Local constant method handles:
1686     private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
1687 
1688     private static MethodHandle makeConstantHandle(int idx) {
1689         try {
1690             switch (idx) {
1691                 case MH_cast:
1692                     return IMPL_LOOKUP.findVirtual(Class.class, "cast",
1693                             MethodType.methodType(Object.class, Object.class));
1694                 case MH_copyAsPrimitiveArray:
1695                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "copyAsPrimitiveArray",
1696                             MethodType.methodType(Object.class, Wrapper.class, Object[].class));
1697                 case MH_arrayIdentity:
1698                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "identity",
1699                             MethodType.methodType(Object[].class, Object[].class));
1700                 case MH_fillNewArray:
1701                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewArray",
1702                             MethodType.methodType(Object[].class, Integer.class, Object[].class));
1703                 case MH_fillNewTypedArray:
1704                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "fillNewTypedArray",
1705                             MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
1706                 case MH_selectAlternative:
1707                     return makeIntrinsic(IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
1708                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)),
1709                         Intrinsic.SELECT_ALTERNATIVE);
1710             }
1711         } catch (ReflectiveOperationException ex) {
1712             throw newInternalError(ex);
1713         }
1714         throw newInternalError("Unknown function index: " + idx);
1715     }
1716 }
--- EOF ---