1 /*
   2  * Copyright (c) 2008, 2021, 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 jdk.internal.access.JavaLangInvokeAccess;
  29 import jdk.internal.access.SharedSecrets;
  30 import jdk.internal.invoke.NativeEntryPoint;
  31 import jdk.internal.org.objectweb.asm.ClassWriter;
  32 import jdk.internal.org.objectweb.asm.MethodVisitor;
  33 import jdk.internal.reflect.CallerSensitive;
  34 import jdk.internal.reflect.Reflection;
  35 import jdk.internal.vm.annotation.ForceInline;
  36 import jdk.internal.vm.annotation.Hidden;
  37 import jdk.internal.vm.annotation.Stable;
  38 import sun.invoke.empty.Empty;
  39 import sun.invoke.util.ValueConversions;
  40 import sun.invoke.util.VerifyType;
  41 import sun.invoke.util.Wrapper;
  42 
  43 import java.lang.invoke.MethodHandles.Lookup;
  44 import java.lang.reflect.Array;
  45 import java.lang.reflect.Field;
  46 import java.nio.ByteOrder;
  47 import java.util.Arrays;
  48 import java.util.Collections;
  49 import java.util.HashMap;
  50 import java.util.Iterator;
  51 import java.util.List;
  52 import java.util.Map;
  53 import java.util.Objects;
  54 import java.util.concurrent.ConcurrentHashMap;
  55 import java.util.function.Function;
  56 import java.util.stream.Stream;
  57 
  58 import static java.lang.invoke.LambdaForm.*;
  59 import static java.lang.invoke.MethodHandleStatics.*;
  60 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  61 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  62 
  63 /**
  64  * Trusted implementation code for MethodHandle.
  65  * @author jrose
  66  */
  67 /*non-public*/
  68 abstract class MethodHandleImpl {
  69 
  70     /// Factory methods to create method handles:
  71 
  72     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, ArrayAccess access) {
  73         if (arrayClass == Object[].class) {
  74             return ArrayAccess.objectAccessor(access);
  75         }
  76         if (!arrayClass.isArray())
  77             throw newIllegalArgumentException("not an array: "+arrayClass);
  78         MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
  79         int cacheIndex = ArrayAccess.cacheIndex(access);
  80         MethodHandle mh = cache[cacheIndex];
  81         if (mh != null)  return mh;
  82         mh = ArrayAccessor.getAccessor(arrayClass, access);
  83         MethodType correctType = ArrayAccessor.correctType(arrayClass, access);
  84         if (mh.type() != correctType) {
  85             assert(mh.type().parameterType(0) == Object[].class);
  86             /* if access == SET */ assert(access != ArrayAccess.SET || mh.type().parameterType(2) == Object.class);
  87             /* if access == GET */ assert(access != ArrayAccess.GET ||
  88                     (mh.type().returnType() == Object.class &&
  89                      correctType.parameterType(0).getComponentType() == correctType.returnType()));
  90             // safe to view non-strictly, because element type follows from array type
  91             mh = mh.viewAsType(correctType, false);
  92         }
  93         mh = makeIntrinsic(mh, ArrayAccess.intrinsic(access));
  94         // Atomically update accessor cache.
  95         synchronized(cache) {
  96             if (cache[cacheIndex] == null) {
  97                 cache[cacheIndex] = mh;
  98             } else {
  99                 // Throw away newly constructed accessor and use cached version.
 100                 mh = cache[cacheIndex];
 101             }
 102         }
 103         return mh;
 104     }
 105 
 106     enum ArrayAccess {
 107         GET, SET, LENGTH;
 108 
 109         // As ArrayAccess and ArrayAccessor have a circular dependency, the ArrayAccess properties cannot be stored in
 110         // final fields.
 111 
 112         static String opName(ArrayAccess a) {
 113             return switch (a) {
 114                 case GET    -> "getElement";
 115                 case SET    -> "setElement";
 116                 case LENGTH -> "length";
 117                 default -> throw unmatchedArrayAccess(a);
 118             };
 119         }
 120 
 121         static MethodHandle objectAccessor(ArrayAccess a) {
 122             return switch (a) {
 123                 case GET    -> ArrayAccessor.OBJECT_ARRAY_GETTER;
 124                 case SET    -> ArrayAccessor.OBJECT_ARRAY_SETTER;
 125                 case LENGTH -> ArrayAccessor.OBJECT_ARRAY_LENGTH;
 126                 default -> throw unmatchedArrayAccess(a);
 127             };
 128         }
 129 
 130         static int cacheIndex(ArrayAccess a) {
 131             return switch (a) {
 132                 case GET    -> ArrayAccessor.GETTER_INDEX;
 133                 case SET    -> ArrayAccessor.SETTER_INDEX;
 134                 case LENGTH -> ArrayAccessor.LENGTH_INDEX;
 135                 default -> throw unmatchedArrayAccess(a);
 136             };
 137         }
 138 
 139         static Intrinsic intrinsic(ArrayAccess a) {
 140             return switch (a) {
 141                 case GET    -> Intrinsic.ARRAY_LOAD;
 142                 case SET    -> Intrinsic.ARRAY_STORE;
 143                 case LENGTH -> Intrinsic.ARRAY_LENGTH;
 144                 default -> throw unmatchedArrayAccess(a);
 145             };
 146         }
 147     }
 148 
 149     static InternalError unmatchedArrayAccess(ArrayAccess a) {
 150         return newInternalError("should not reach here (unmatched ArrayAccess: " + a + ")");
 151     }
 152 
 153     static final class ArrayAccessor {
 154         /// Support for array element and length access
 155         static final int GETTER_INDEX = 0, SETTER_INDEX = 1, LENGTH_INDEX = 2, INDEX_LIMIT = 3;
 156         static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
 157                 = new ClassValue<MethodHandle[]>() {
 158                     @Override
 159                     protected MethodHandle[] computeValue(Class<?> type) {
 160                         return new MethodHandle[INDEX_LIMIT];
 161                     }
 162                 };
 163         static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER, OBJECT_ARRAY_LENGTH;
 164         static {
 165             MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
 166             cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.GET),    Intrinsic.ARRAY_LOAD);
 167             cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.SET),    Intrinsic.ARRAY_STORE);
 168             cache[LENGTH_INDEX] = OBJECT_ARRAY_LENGTH = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.LENGTH), Intrinsic.ARRAY_LENGTH);
 169 
 170             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
 171             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
 172             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_LENGTH.internalMemberName()));
 173         }
 174 
 175         static int     getElementI(int[]     a, int i)            { return              a[i]; }
 176         static long    getElementJ(long[]    a, int i)            { return              a[i]; }
 177         static float   getElementF(float[]   a, int i)            { return              a[i]; }
 178         static double  getElementD(double[]  a, int i)            { return              a[i]; }
 179         static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
 180         static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
 181         static short   getElementS(short[]   a, int i)            { return              a[i]; }
 182         static char    getElementC(char[]    a, int i)            { return              a[i]; }
 183         static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
 184 
 185         static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
 186         static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
 187         static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
 188         static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
 189         static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
 190         static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
 191         static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
 192         static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
 193         static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
 194 
 195         static int     lengthI(int[]     a)                       { return a.length; }
 196         static int     lengthJ(long[]    a)                       { return a.length; }
 197         static int     lengthF(float[]   a)                       { return a.length; }
 198         static int     lengthD(double[]  a)                       { return a.length; }
 199         static int     lengthZ(boolean[] a)                       { return a.length; }
 200         static int     lengthB(byte[]    a)                       { return a.length; }
 201         static int     lengthS(short[]   a)                       { return a.length; }
 202         static int     lengthC(char[]    a)                       { return a.length; }
 203         static int     lengthL(Object[]  a)                       { return a.length; }
 204 
 205         static String name(Class<?> arrayClass, ArrayAccess access) {
 206             Class<?> elemClass = arrayClass.getComponentType();
 207             if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
 208             return ArrayAccess.opName(access) + Wrapper.basicTypeChar(elemClass);
 209         }
 210         static MethodType type(Class<?> arrayClass, ArrayAccess access) {
 211             Class<?> elemClass = arrayClass.getComponentType();
 212             Class<?> arrayArgClass = arrayClass;
 213             if (!elemClass.isPrimitive()) {
 214                 arrayArgClass = Object[].class;
 215                 elemClass = Object.class;
 216             }
 217             return switch (access) {
 218                 case GET    -> MethodType.methodType(elemClass, arrayArgClass, int.class);
 219                 case SET    -> MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
 220                 case LENGTH -> MethodType.methodType(int.class, arrayArgClass);
 221                 default -> throw unmatchedArrayAccess(access);
 222             };
 223         }
 224         static MethodType correctType(Class<?> arrayClass, ArrayAccess access) {
 225             Class<?> elemClass = arrayClass.getComponentType();
 226             return switch (access) {
 227                 case GET    -> MethodType.methodType(elemClass, arrayClass, int.class);
 228                 case SET    -> MethodType.methodType(void.class, arrayClass, int.class, elemClass);
 229                 case LENGTH -> MethodType.methodType(int.class, arrayClass);
 230                 default -> throw unmatchedArrayAccess(access);
 231             };
 232         }
 233         static MethodHandle getAccessor(Class<?> arrayClass, ArrayAccess access) {
 234             String     name = name(arrayClass, access);
 235             MethodType type = type(arrayClass, access);
 236             try {
 237                 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
 238             } catch (ReflectiveOperationException ex) {
 239                 throw uncaughtException(ex);
 240             }
 241         }
 242     }
 243 
 244     /**
 245      * Create a JVM-level adapter method handle to conform the given method
 246      * handle to the similar newType, using only pairwise argument conversions.
 247      * For each argument, convert incoming argument to the exact type needed.
 248      * The argument conversions allowed are casting, boxing and unboxing,
 249      * integral widening or narrowing, and floating point widening or narrowing.
 250      * @param srcType required call type
 251      * @param target original method handle
 252      * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
 253      * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
 254      * @return an adapter to the original handle with the desired new type,
 255      *          or the original target if the types are already identical
 256      *          or null if the adaptation cannot be made
 257      */
 258     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 259                                             boolean strict, boolean monobox) {
 260         MethodType dstType = target.type();
 261         if (srcType == dstType)
 262             return target;
 263         return makePairwiseConvertByEditor(target, srcType, strict, monobox);
 264     }
 265 
 266     private static int countNonNull(Object[] array) {
 267         int count = 0;
 268         if (array != null) {
 269             for (Object x : array) {
 270                 if (x != null) ++count;
 271             }
 272         }
 273         return count;
 274     }
 275 
 276     static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
 277                                                     boolean strict, boolean monobox) {
 278         // In method types arguments start at index 0, while the LF
 279         // editor have the MH receiver at position 0 - adjust appropriately.
 280         final int MH_RECEIVER_OFFSET = 1;
 281         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 282         int convCount = countNonNull(convSpecs);
 283         if (convCount == 0)
 284             return target.viewAsType(srcType, strict);
 285         MethodType basicSrcType = srcType.basicType();
 286         MethodType midType = target.type().basicType();
 287         BoundMethodHandle mh = target.rebind();
 288 
 289         // Match each unique conversion to the positions at which it is to be applied
 290         var convSpecMap = new HashMap<Object, int[]>(((4 * convCount) / 3) + 1);
 291         for (int i = 0; i < convSpecs.length - MH_RECEIVER_OFFSET; i++) {
 292             Object convSpec = convSpecs[i];
 293             if (convSpec == null) continue;
 294             int[] positions = convSpecMap.get(convSpec);
 295             if (positions == null) {
 296                 positions = new int[] { i + MH_RECEIVER_OFFSET };
 297             } else {
 298                 positions = Arrays.copyOf(positions, positions.length + 1);
 299                 positions[positions.length - 1] = i + MH_RECEIVER_OFFSET;
 300             }
 301             convSpecMap.put(convSpec, positions);
 302         }
 303         for (var entry : convSpecMap.entrySet()) {
 304             Object convSpec = entry.getKey();
 305 
 306             MethodHandle fn;
 307             if (convSpec instanceof Class) {
 308                 fn = getConstantHandle(MH_cast).bindTo(convSpec);
 309             } else {
 310                 fn = (MethodHandle) convSpec;
 311             }
 312             int[] positions = entry.getValue();
 313             Class<?> newType = basicSrcType.parameterType(positions[0] - MH_RECEIVER_OFFSET);
 314             BasicType newBasicType = BasicType.basicType(newType);
 315             convCount -= positions.length;
 316             if (convCount == 0) {
 317                 midType = srcType;
 318             } else {
 319                 Class<?>[] ptypes = midType.ptypes().clone();
 320                 for (int pos : positions) {
 321                     ptypes[pos - 1] = newType;
 322                 }
 323                 midType = MethodType.makeImpl(midType.rtype(), ptypes, true);
 324             }
 325             LambdaForm form2;
 326             if (positions.length > 1) {
 327                 form2 = mh.editor().filterRepeatedArgumentForm(newBasicType, positions);
 328             } else {
 329                 form2 = mh.editor().filterArgumentForm(positions[0], newBasicType);
 330             }
 331             mh = mh.copyWithExtendL(midType, form2, fn);
 332         }
 333         Object convSpec = convSpecs[convSpecs.length - 1];
 334         if (convSpec != null) {
 335             MethodHandle fn;
 336             if (convSpec instanceof Class) {
 337                 if (convSpec == void.class)
 338                     fn = null;
 339                 else
 340                     fn = getConstantHandle(MH_cast).bindTo(convSpec);
 341             } else {
 342                 fn = (MethodHandle) convSpec;
 343             }
 344             Class<?> newType = basicSrcType.returnType();
 345             assert(--convCount == 0);
 346             midType = srcType;
 347             if (fn != null) {
 348                 mh = mh.rebind();  // rebind if too complex
 349                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
 350                 mh = mh.copyWithExtendL(midType, form2, fn);
 351             } else {
 352                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
 353                 mh = mh.copyWith(midType, form2);
 354             }
 355         }
 356         assert(convCount == 0);
 357         assert(mh.type().equals(srcType));
 358         return mh;
 359     }
 360 
 361     static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
 362                                             boolean strict, boolean monobox) {
 363         final int INARG_COUNT = srcType.parameterCount();
 364         Object[] convSpecs = null;
 365         for (int i = 0; i <= INARG_COUNT; i++) {
 366             boolean isRet = (i == INARG_COUNT);
 367             Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
 368             Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
 369             if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
 370                 if (convSpecs == null) {
 371                     convSpecs = new Object[INARG_COUNT + 1];
 372                 }
 373                 convSpecs[i] = valueConversion(src, dst, strict, monobox);
 374             }
 375         }
 376         return convSpecs;
 377     }
 378     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 379                                             boolean strict) {
 380         return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
 381     }
 382 
 383     /**
 384      * Find a conversion function from the given source to the given destination.
 385      * This conversion function will be used as a LF NamedFunction.
 386      * Return a Class object if a simple cast is needed.
 387      * Return void.class if void is involved.
 388      */
 389     static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
 390         assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
 391         if (dst == void.class)
 392             return dst;
 393         MethodHandle fn;
 394         if (src.isPrimitive()) {
 395             if (src == void.class) {
 396                 return void.class;  // caller must recognize this specially
 397             } else if (dst.isPrimitive()) {
 398                 // Examples: int->byte, byte->int, boolean->int (!strict)
 399                 fn = ValueConversions.convertPrimitive(src, dst);
 400             } else {
 401                 // Examples: int->Integer, boolean->Object, float->Number
 402                 Wrapper wsrc = Wrapper.forPrimitiveType(src);
 403                 fn = ValueConversions.boxExact(wsrc);
 404                 assert(fn.type().parameterType(0) == wsrc.primitiveType());
 405                 assert(fn.type().returnType() == wsrc.wrapperType());
 406                 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
 407                     // Corner case, such as int->Long, which will probably fail.
 408                     MethodType mt = MethodType.methodType(dst, src);
 409                     if (strict)
 410                         fn = fn.asType(mt);
 411                     else
 412                         fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
 413                 }
 414             }
 415         } else if (dst.isPrimitive()) {
 416             Wrapper wdst = Wrapper.forPrimitiveType(dst);
 417             if (monobox || src == wdst.wrapperType()) {
 418                 // Use a strongly-typed unboxer, if possible.
 419                 fn = ValueConversions.unboxExact(wdst, strict);
 420             } else {
 421                 // Examples:  Object->int, Number->int, Comparable->int, Byte->int
 422                 // must include additional conversions
 423                 // src must be examined at runtime, to detect Byte, Character, etc.
 424                 fn = (strict
 425                         ? ValueConversions.unboxWiden(wdst)
 426                         : ValueConversions.unboxCast(wdst));
 427             }
 428         } else {
 429             // Simple reference conversion.
 430             // Note:  Do not check for a class hierarchy relation
 431             // between src and dst.  In all cases a 'null' argument
 432             // will pass the cast conversion.
 433             return dst;
 434         }
 435         assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
 436         return fn;
 437     }
 438 
 439     static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
 440         MethodType type = target.type();
 441         int last = type.parameterCount() - 1;
 442         if (type.parameterType(last) != arrayType)
 443             target = target.asType(type.changeParameterType(last, arrayType));
 444         target = target.asFixedArity();  // make sure this attribute is turned off
 445         return new AsVarargsCollector(target, arrayType);
 446     }
 447 
 448     private static final class AsVarargsCollector extends DelegatingMethodHandle {
 449         private final MethodHandle target;
 450         private final Class<?> arrayType;
 451         private @Stable MethodHandle asCollectorCache;
 452 
 453         AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
 454             this(target.type(), target, arrayType);
 455         }
 456         AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
 457             super(type, target);
 458             this.target = target;
 459             this.arrayType = arrayType;
 460         }
 461 
 462         @Override
 463         public boolean isVarargsCollector() {
 464             return true;
 465         }
 466 
 467         @Override
 468         protected MethodHandle getTarget() {
 469             return target;
 470         }
 471 
 472         @Override
 473         public MethodHandle asFixedArity() {
 474             return target;
 475         }
 476 
 477         @Override
 478         MethodHandle setVarargs(MemberName member) {
 479             if (member.isVarargs())  return this;
 480             return asFixedArity();
 481         }
 482 
 483         @Override
 484         public MethodHandle withVarargs(boolean makeVarargs) {
 485             if (makeVarargs)  return this;
 486             return asFixedArity();
 487         }
 488 
 489         @Override
 490         public MethodHandle asTypeUncached(MethodType newType) {
 491             MethodType type = this.type();
 492             int collectArg = type.parameterCount() - 1;
 493             int newArity = newType.parameterCount();
 494             if (newArity == collectArg+1 &&
 495                 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
 496                 // if arity and trailing parameter are compatible, do normal thing
 497                 return asFixedArity().asType(newType);
 498             }
 499             // check cache
 500             MethodHandle acc = asCollectorCache;
 501             if (acc != null && acc.type().parameterCount() == newArity)
 502                 return acc.asType(newType);
 503             // build and cache a collector
 504             int arrayLength = newArity - collectArg;
 505             MethodHandle collector;
 506             try {
 507                 collector = asFixedArity().asCollector(arrayType, arrayLength);
 508                 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
 509             } catch (IllegalArgumentException ex) {
 510                 throw new WrongMethodTypeException("cannot build collector", ex);
 511             }
 512             asCollectorCache = collector;
 513             return collector.asType(newType);
 514         }
 515 
 516         @Override
 517         boolean viewAsTypeChecks(MethodType newType, boolean strict) {
 518             super.viewAsTypeChecks(newType, true);
 519             if (strict) return true;
 520             // extra assertion for non-strict checks:
 521             assert (type().lastParameterType().getComponentType()
 522                     .isAssignableFrom(
 523                             newType.lastParameterType().getComponentType()))
 524                     : Arrays.asList(this, newType);
 525             return true;
 526         }
 527 
 528         @Override
 529         public Object invokeWithArguments(Object... arguments) throws Throwable {
 530             MethodType type = this.type();
 531             int argc;
 532             final int MAX_SAFE = 127;  // 127 longs require 254 slots, which is safe to spread
 533             if (arguments == null
 534                     || (argc = arguments.length) <= MAX_SAFE
 535                     || argc < type.parameterCount()) {
 536                 return super.invokeWithArguments(arguments);
 537             }
 538 
 539             // a jumbo invocation requires more explicit reboxing of the trailing arguments
 540             int uncollected = type.parameterCount() - 1;
 541             Class<?> elemType = arrayType.getComponentType();
 542             int collected = argc - uncollected;
 543             Object collArgs = (elemType == Object.class)
 544                 ? new Object[collected] : Array.newInstance(elemType, collected);
 545             if (!elemType.isPrimitive()) {
 546                 // simple cast:  just do some casting
 547                 try {
 548                     System.arraycopy(arguments, uncollected, collArgs, 0, collected);
 549                 } catch (ArrayStoreException ex) {
 550                     return super.invokeWithArguments(arguments);
 551                 }
 552             } else {
 553                 // corner case of flat array requires reflection (or specialized copy loop)
 554                 MethodHandle arraySetter = MethodHandles.arrayElementSetter(arrayType);
 555                 try {
 556                     for (int i = 0; i < collected; i++) {
 557                         arraySetter.invoke(collArgs, i, arguments[uncollected + i]);
 558                     }
 559                 } catch (WrongMethodTypeException|ClassCastException ex) {
 560                     return super.invokeWithArguments(arguments);
 561                 }
 562             }
 563 
 564             // chop the jumbo list down to size and call in non-varargs mode
 565             Object[] newArgs = new Object[uncollected + 1];
 566             System.arraycopy(arguments, 0, newArgs, 0, uncollected);
 567             newArgs[uncollected] = collArgs;
 568             return asFixedArity().invokeWithArguments(newArgs);
 569         }
 570     }
 571 
 572     static void checkSpreadArgument(Object av, int n) {
 573         if (av == null && n == 0) {
 574             return;
 575         } else if (av == null) {
 576             throw new NullPointerException("null array reference");
 577         } else if (av instanceof Object[]) {
 578             int len = ((Object[])av).length;
 579             if (len == n)  return;
 580         } else {
 581             int len = java.lang.reflect.Array.getLength(av);
 582             if (len == n)  return;
 583         }
 584         // fall through to error:
 585         throw newIllegalArgumentException("array is not of length "+n);
 586     }
 587 
 588     @Hidden
 589     static MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
 590         if (testResult) {
 591             return target;
 592         } else {
 593             return fallback;
 594         }
 595     }
 596 
 597     // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
 598     @Hidden
 599     @jdk.internal.vm.annotation.IntrinsicCandidate
 600     static boolean profileBoolean(boolean result, int[] counters) {
 601         // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
 602         int idx = result ? 1 : 0;
 603         try {
 604             counters[idx] = Math.addExact(counters[idx], 1);
 605         } catch (ArithmeticException e) {
 606             // Avoid continuous overflow by halving the problematic count.
 607             counters[idx] = counters[idx] / 2;
 608         }
 609         return result;
 610     }
 611 
 612     // Intrinsified by C2. Returns true if obj is a compile-time constant.
 613     @Hidden
 614     @jdk.internal.vm.annotation.IntrinsicCandidate
 615     static boolean isCompileConstant(Object obj) {
 616         return false;
 617     }
 618 
 619     static MethodHandle makeGuardWithTest(MethodHandle test,
 620                                    MethodHandle target,
 621                                    MethodHandle fallback) {
 622         MethodType type = target.type();
 623         assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
 624         MethodType basicType = type.basicType();
 625         LambdaForm form = makeGuardWithTestForm(basicType);
 626         BoundMethodHandle mh;
 627         try {
 628             if (PROFILE_GWT) {
 629                 int[] counts = new int[2];
 630                 mh = (BoundMethodHandle)
 631                         BoundMethodHandle.speciesData_LLLL().factory().invokeBasic(type, form,
 632                                 (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
 633             } else {
 634                 mh = (BoundMethodHandle)
 635                         BoundMethodHandle.speciesData_LLL().factory().invokeBasic(type, form,
 636                                 (Object) test, (Object) profile(target), (Object) profile(fallback));
 637             }
 638         } catch (Throwable ex) {
 639             throw uncaughtException(ex);
 640         }
 641         assert(mh.type() == type);
 642         return mh;
 643     }
 644 
 645 
 646     static MethodHandle profile(MethodHandle target) {
 647         if (DONT_INLINE_THRESHOLD >= 0) {
 648             return makeBlockInliningWrapper(target);
 649         } else {
 650             return target;
 651         }
 652     }
 653 
 654     /**
 655      * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
 656      * Corresponding LambdaForm has @DontInline when compiled into bytecode.
 657      */
 658     static MethodHandle makeBlockInliningWrapper(MethodHandle target) {
 659         LambdaForm lform;
 660         if (DONT_INLINE_THRESHOLD > 0) {
 661             lform = Makers.PRODUCE_BLOCK_INLINING_FORM.apply(target);
 662         } else {
 663             lform = Makers.PRODUCE_REINVOKER_FORM.apply(target);
 664         }
 665         return new CountingWrapper(target, lform,
 666                 Makers.PRODUCE_BLOCK_INLINING_FORM, Makers.PRODUCE_REINVOKER_FORM,
 667                                    DONT_INLINE_THRESHOLD);
 668     }
 669 
 670     private static final class Makers {
 671         /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
 672         static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
 673             @Override
 674             public LambdaForm apply(MethodHandle target) {
 675                 return DelegatingMethodHandle.makeReinvokerForm(target,
 676                                    MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, false,
 677                                    DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
 678             }
 679         };
 680 
 681         /** Constructs simple reinvoker lambda form for a particular method handle */
 682         static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
 683             @Override
 684             public LambdaForm apply(MethodHandle target) {
 685                 return DelegatingMethodHandle.makeReinvokerForm(target,
 686                         MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
 687             }
 688         };
 689 
 690         /** Maker of type-polymorphic varargs */
 691         static final ClassValue<MethodHandle[]> TYPED_COLLECTORS = new ClassValue<MethodHandle[]>() {
 692             @Override
 693             protected MethodHandle[] computeValue(Class<?> type) {
 694                 return new MethodHandle[MAX_JVM_ARITY + 1];
 695             }
 696         };
 697     }
 698 
 699     /**
 700      * Counting method handle. It has 2 states: counting and non-counting.
 701      * It is in counting state for the first n invocations and then transitions to non-counting state.
 702      * Behavior in counting and non-counting states is determined by lambda forms produced by
 703      * countingFormProducer & nonCountingFormProducer respectively.
 704      */
 705     static class CountingWrapper extends DelegatingMethodHandle {
 706         private final MethodHandle target;
 707         private int count;
 708         private Function<MethodHandle, LambdaForm> countingFormProducer;
 709         private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
 710         private volatile boolean isCounting;
 711 
 712         private CountingWrapper(MethodHandle target, LambdaForm lform,
 713                                 Function<MethodHandle, LambdaForm> countingFromProducer,
 714                                 Function<MethodHandle, LambdaForm> nonCountingFormProducer,
 715                                 int count) {
 716             super(target.type(), lform);
 717             this.target = target;
 718             this.count = count;
 719             this.countingFormProducer = countingFromProducer;
 720             this.nonCountingFormProducer = nonCountingFormProducer;
 721             this.isCounting = (count > 0);
 722         }
 723 
 724         @Hidden
 725         @Override
 726         protected MethodHandle getTarget() {
 727             return target;
 728         }
 729 
 730         @Override
 731         public MethodHandle asTypeUncached(MethodType newType) {
 732             MethodHandle newTarget = target.asType(newType);
 733             MethodHandle wrapper;
 734             if (isCounting) {
 735                 LambdaForm lform;
 736                 lform = countingFormProducer.apply(newTarget);
 737                 wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
 738             } else {
 739                 wrapper = newTarget; // no need for a counting wrapper anymore
 740             }
 741             return wrapper;
 742         }
 743 
 744         boolean countDown() {
 745             int c = count;
 746             target.maybeCustomize(); // customize if counting happens for too long
 747             if (c <= 1) {
 748                 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
 749                 if (isCounting) {
 750                     isCounting = false;
 751                     return true;
 752                 } else {
 753                     return false;
 754                 }
 755             } else {
 756                 count = c - 1;
 757                 return false;
 758             }
 759         }
 760 
 761         @Hidden
 762         static void maybeStopCounting(Object o1) {
 763              final CountingWrapper wrapper = (CountingWrapper) o1;
 764              if (wrapper.countDown()) {
 765                  // Reached invocation threshold. Replace counting behavior with a non-counting one.
 766                  wrapper.updateForm(new Function<>() {
 767                      public LambdaForm apply(LambdaForm oldForm) {
 768                          LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
 769                          lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
 770                          return lform;
 771                      }});
 772              }
 773         }
 774 
 775         static final NamedFunction NF_maybeStopCounting;
 776         static {
 777             Class<?> THIS_CLASS = CountingWrapper.class;
 778             try {
 779                 NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
 780             } catch (ReflectiveOperationException ex) {
 781                 throw newInternalError(ex);
 782             }
 783         }
 784     }
 785 
 786     static LambdaForm makeGuardWithTestForm(MethodType basicType) {
 787         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
 788         if (lform != null)  return lform;
 789         final int THIS_MH      = 0;  // the BMH_LLL
 790         final int ARG_BASE     = 1;  // start of incoming arguments
 791         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 792         int nameCursor = ARG_LIMIT;
 793         final int GET_TEST     = nameCursor++;
 794         final int GET_TARGET   = nameCursor++;
 795         final int GET_FALLBACK = nameCursor++;
 796         final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
 797         final int CALL_TEST    = nameCursor++;
 798         final int PROFILE      = (GET_COUNTERS != -1) ? nameCursor++ : -1;
 799         final int TEST         = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
 800         final int SELECT_ALT   = nameCursor++;
 801         final int CALL_TARGET  = nameCursor++;
 802         assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
 803 
 804         MethodType lambdaType = basicType.invokerType();
 805         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 806 
 807         BoundMethodHandle.SpeciesData data =
 808                 (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
 809                                      : BoundMethodHandle.speciesData_LLL();
 810         names[THIS_MH] = names[THIS_MH].withConstraint(data);
 811         names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
 812         names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
 813         names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
 814         if (GET_COUNTERS != -1) {
 815             names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
 816         }
 817         Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
 818 
 819         // call test
 820         MethodType testType = basicType.changeReturnType(boolean.class).basicType();
 821         invokeArgs[0] = names[GET_TEST];
 822         names[CALL_TEST] = new Name(testType, invokeArgs);
 823 
 824         // profile branch
 825         if (PROFILE != -1) {
 826             names[PROFILE] = new Name(getFunction(NF_profileBoolean), names[CALL_TEST], names[GET_COUNTERS]);
 827         }
 828         // call selectAlternative
 829         names[SELECT_ALT] = new Name(new NamedFunction(
 830                 makeIntrinsic(getConstantHandle(MH_selectAlternative), Intrinsic.SELECT_ALTERNATIVE)),
 831                 names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
 832 
 833         // call target or fallback
 834         invokeArgs[0] = names[SELECT_ALT];
 835         names[CALL_TARGET] = new Name(basicType, invokeArgs);
 836 
 837         lform = new LambdaForm(lambdaType.parameterCount(), names, /*forceInline=*/true, Kind.GUARD);
 838 
 839         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
 840     }
 841 
 842     /**
 843      * The LambdaForm shape for catchException combinator is the following:
 844      * <blockquote><pre>{@code
 845      *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
 846      *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
 847      *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
 848      *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
 849      *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
 850      *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
 851      *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
 852      *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
 853      *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
 854      * }</pre></blockquote>
 855      *
 856      * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
 857      * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
 858      * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
 859      *
 860      * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
 861      * among catchException combinators with the same basic type.
 862      */
 863     private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
 864         MethodType lambdaType = basicType.invokerType();
 865 
 866         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
 867         if (lform != null) {
 868             return lform;
 869         }
 870         final int THIS_MH      = 0;  // the BMH_LLLLL
 871         final int ARG_BASE     = 1;  // start of incoming arguments
 872         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 873 
 874         int nameCursor = ARG_LIMIT;
 875         final int GET_TARGET       = nameCursor++;
 876         final int GET_CLASS        = nameCursor++;
 877         final int GET_CATCHER      = nameCursor++;
 878         final int GET_COLLECT_ARGS = nameCursor++;
 879         final int GET_UNBOX_RESULT = nameCursor++;
 880         final int BOXED_ARGS       = nameCursor++;
 881         final int TRY_CATCH        = nameCursor++;
 882         final int UNBOX_RESULT     = nameCursor++;
 883 
 884         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 885 
 886         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 887         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
 888         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
 889         names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
 890         names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
 891         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
 892         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
 893 
 894         // FIXME: rework argument boxing/result unboxing logic for LF interpretation
 895 
 896         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
 897         MethodType collectArgsType = basicType.changeReturnType(Object.class);
 898         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
 899         Object[] args = new Object[invokeBasic.type().parameterCount()];
 900         args[0] = names[GET_COLLECT_ARGS];
 901         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
 902         names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH)), args);
 903 
 904         // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
 905         Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
 906         names[TRY_CATCH] = new Name(getFunction(NF_guardWithCatch), gwcArgs);
 907 
 908         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
 909         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
 910         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
 911         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
 912 
 913         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.GUARD_WITH_CATCH);
 914 
 915         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
 916     }
 917 
 918     static MethodHandle makeGuardWithCatch(MethodHandle target,
 919                                     Class<? extends Throwable> exType,
 920                                     MethodHandle catcher) {
 921         MethodType type = target.type();
 922         LambdaForm form = makeGuardWithCatchForm(type.basicType());
 923 
 924         // Prepare auxiliary method handles used during LambdaForm interpretation.
 925         // Box arguments and wrap them into Object[]: ValueConversions.array().
 926         MethodType varargsType = type.changeReturnType(Object[].class);
 927         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
 928         MethodHandle unboxResult = unboxResultHandle(type.returnType());
 929 
 930         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 931         BoundMethodHandle mh;
 932         try {
 933             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) exType,
 934                     (Object) catcher, (Object) collectArgs, (Object) unboxResult);
 935         } catch (Throwable ex) {
 936             throw uncaughtException(ex);
 937         }
 938         assert(mh.type() == type);
 939         return mh;
 940     }
 941 
 942     /**
 943      * Intrinsified during LambdaForm compilation
 944      * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
 945      */
 946     @Hidden
 947     static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
 948                                  Object... av) throws Throwable {
 949         // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
 950         try {
 951             return target.asFixedArity().invokeWithArguments(av);
 952         } catch (Throwable t) {
 953             if (!exType.isInstance(t)) throw t;
 954             return catcher.asFixedArity().invokeWithArguments(prepend(av, t));
 955         }
 956     }
 957 
 958     /** Prepend elements to an array. */
 959     @Hidden
 960     private static Object[] prepend(Object[] array, Object... elems) {
 961         int nArray = array.length;
 962         int nElems = elems.length;
 963         Object[] newArray = new Object[nArray + nElems];
 964         System.arraycopy(elems, 0, newArray, 0, nElems);
 965         System.arraycopy(array, 0, newArray, nElems, nArray);
 966         return newArray;
 967     }
 968 
 969     static MethodHandle throwException(MethodType type) {
 970         assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
 971         int arity = type.parameterCount();
 972         if (arity > 1) {
 973             MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
 974             mh = MethodHandles.dropArguments(mh, 1, Arrays.copyOfRange(type.parameterArray(), 1, arity));
 975             return mh;
 976         }
 977         return makePairwiseConvert(getFunction(NF_throwException).resolvedHandle(), type, false, true);
 978     }
 979 
 980     static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
 981 
 982     static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
 983     static MethodHandle fakeMethodHandleInvoke(MemberName method) {
 984         assert(method.isMethodHandleInvoke());
 985         int idx = switch (method.getName()) {
 986             case "invoke"      -> 0;
 987             case "invokeExact" -> 1;
 988             default -> throw new InternalError(method.getName());
 989         };
 990         MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
 991         if (mh != null)  return mh;
 992         MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
 993                                                 MethodHandle.class, Object[].class);
 994         mh = throwException(type);
 995         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
 996         if (!method.getInvocationType().equals(mh.type()))
 997             throw new InternalError(method.toString());
 998         mh = mh.withInternalMemberName(method, false);
 999         mh = mh.withVarargs(true);
1000         assert(method.isVarargs());
1001         FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
1002         return mh;
1003     }
1004     static MethodHandle fakeVarHandleInvoke(MemberName method) {
1005         // TODO caching, is it necessary?
1006         MethodType type = MethodType.methodType(method.getReturnType(), UnsupportedOperationException.class,
1007                                                 VarHandle.class, Object[].class);
1008         MethodHandle mh = throwException(type);
1009         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke VarHandle"));
1010         if (!method.getInvocationType().equals(mh.type()))
1011             throw new InternalError(method.toString());
1012         mh = mh.withInternalMemberName(method, false);
1013         mh = mh.asVarargsCollector(Object[].class);
1014         assert(method.isVarargs());
1015         return mh;
1016     }
1017 
1018     /**
1019      * Create an alias for the method handle which, when called,
1020      * appears to be called from the same class loader and protection domain
1021      * as hostClass.
1022      * This is an expensive no-op unless the method which is called
1023      * is sensitive to its caller.  A small number of system methods
1024      * are in this category, including Class.forName and Method.invoke.
1025      */
1026     static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1027         return BindCaller.bindCaller(mh, hostClass);
1028     }
1029 
1030     // Put the whole mess into its own nested class.
1031     // That way we can lazily load the code and set up the constants.
1032     private static class BindCaller {
1033         private static MethodType INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1034 
1035         static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1036             // Code in the boot layer should now be careful while creating method handles or
1037             // functional interface instances created from method references to @CallerSensitive  methods,
1038             // it needs to be ensured the handles or interface instances are kept safe and are not passed
1039             // from the boot layer to untrusted code.
1040             if (hostClass == null
1041                 ||    (hostClass.isArray() ||
1042                        hostClass.isPrimitive() ||
1043                        hostClass.getName().startsWith("java.lang.invoke."))) {
1044                 throw new InternalError();  // does not happen, and should not anyway
1045             }
1046             // For simplicity, convert mh to a varargs-like method.
1047             MethodHandle vamh = prepareForInvoker(mh);
1048             // Cache the result of makeInjectedInvoker once per argument class.
1049             MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
1050             return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
1051         }
1052 
1053         private static MethodHandle makeInjectedInvoker(Class<?> targetClass) {
1054             try {
1055                 /*
1056                  * The invoker class defined to the same class loader as the lookup class
1057                  * but in an unnamed package so that the class bytes can be cached and
1058                  * reused for any @CSM.
1059                  *
1060                  * @CSM must be public and exported if called by any module.
1061                  */
1062                 String name = targetClass.getName() + "$$InjectedInvoker";
1063                 if (targetClass.isHidden()) {
1064                     // use the original class name
1065                     name = name.replace('/', '_');
1066                 }
1067                 Class<?> invokerClass = new Lookup(targetClass)
1068                         .makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE)
1069                         .defineClass(true);
1070                 assert checkInjectedInvoker(targetClass, invokerClass);
1071                 return IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1072             } catch (ReflectiveOperationException ex) {
1073                 throw uncaughtException(ex);
1074             }
1075         }
1076 
1077         private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
1078             @Override protected MethodHandle computeValue(Class<?> hostClass) {
1079                 return makeInjectedInvoker(hostClass);
1080             }
1081         };
1082 
1083         // Adapt mh so that it can be called directly from an injected invoker:
1084         private static MethodHandle prepareForInvoker(MethodHandle mh) {
1085             mh = mh.asFixedArity();
1086             MethodType mt = mh.type();
1087             int arity = mt.parameterCount();
1088             MethodHandle vamh = mh.asType(mt.generic());
1089             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1090             vamh = vamh.asSpreader(Object[].class, arity);
1091             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1092             return vamh;
1093         }
1094 
1095         // Undo the adapter effect of prepareForInvoker:
1096         private static MethodHandle restoreToType(MethodHandle vamh,
1097                                                   MethodHandle original,
1098                                                   Class<?> hostClass) {
1099             MethodType type = original.type();
1100             MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
1101             MemberName member = original.internalMemberName();
1102             mh = mh.asType(type);
1103             mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
1104             return mh;
1105         }
1106 
1107         private static boolean checkInjectedInvoker(Class<?> hostClass, Class<?> invokerClass) {
1108             assert (hostClass.getClassLoader() == invokerClass.getClassLoader()) : hostClass.getName()+" (CL)";
1109             try {
1110                 assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
1111             } catch (SecurityException ex) {
1112                 // Self-check was blocked by security manager. This is OK.
1113             }
1114             try {
1115                 // Test the invoker to ensure that it really injects into the right place.
1116                 MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1117                 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
1118                 return (boolean)invoker.invoke(vamh, new Object[]{ invokerClass });
1119             } catch (Throwable ex) {
1120                 throw new InternalError(ex);
1121             }
1122         }
1123 
1124         private static final MethodHandle MH_checkCallerClass;
1125         static {
1126             final Class<?> THIS_CLASS = BindCaller.class;
1127             assert(checkCallerClass(THIS_CLASS));
1128             try {
1129                 MH_checkCallerClass = IMPL_LOOKUP
1130                     .findStatic(THIS_CLASS, "checkCallerClass",
1131                                 MethodType.methodType(boolean.class, Class.class));
1132                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS));
1133             } catch (Throwable ex) {
1134                 throw new InternalError(ex);
1135             }
1136         }
1137 
1138         @CallerSensitive
1139         @ForceInline // to ensure Reflection.getCallerClass optimization
1140         private static boolean checkCallerClass(Class<?> expected) {
1141             // This method is called via MH_checkCallerClass and so it's correct to ask for the immediate caller here.
1142             Class<?> actual = Reflection.getCallerClass();
1143             if (actual != expected)
1144                 throw new InternalError("found " + actual.getName() + ", expected " + expected.getName());
1145             return true;
1146         }
1147 
1148         private static final byte[] INJECTED_INVOKER_TEMPLATE = generateInvokerTemplate();
1149 
1150         /** Produces byte code for a class that is used as an injected invoker. */
1151         private static byte[] generateInvokerTemplate() {
1152             ClassWriter cw = new ClassWriter(0);
1153 
1154             // private static class InjectedInvoker {
1155             //     @Hidden
1156             //     static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
1157             //        return vamh.invokeExact(args);
1158             //     }
1159             // }
1160             cw.visit(CLASSFILE_VERSION, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);
1161 
1162             MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
1163                           "(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
1164                           null, null);
1165 
1166             mv.visitCode();
1167             mv.visitVarInsn(ALOAD, 0);
1168             mv.visitVarInsn(ALOAD, 1);
1169             mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
1170                                "([Ljava/lang/Object;)Ljava/lang/Object;", false);
1171             mv.visitInsn(ARETURN);
1172             mv.visitMaxs(2, 2);
1173             mv.visitEnd();
1174 
1175             cw.visitEnd();
1176             return cw.toByteArray();
1177         }
1178     }
1179 
1180     /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
1181     private static final class WrappedMember extends DelegatingMethodHandle {
1182         private final MethodHandle target;
1183         private final MemberName member;
1184         private final Class<?> callerClass;
1185         private final boolean isInvokeSpecial;
1186 
1187         private WrappedMember(MethodHandle target, MethodType type,
1188                               MemberName member, boolean isInvokeSpecial,
1189                               Class<?> callerClass) {
1190             super(type, target);
1191             this.target = target;
1192             this.member = member;
1193             this.callerClass = callerClass;
1194             this.isInvokeSpecial = isInvokeSpecial;
1195         }
1196 
1197         @Override
1198         MemberName internalMemberName() {
1199             return member;
1200         }
1201         @Override
1202         Class<?> internalCallerClass() {
1203             return callerClass;
1204         }
1205         @Override
1206         boolean isInvokeSpecial() {
1207             return isInvokeSpecial;
1208         }
1209         @Override
1210         protected MethodHandle getTarget() {
1211             return target;
1212         }
1213         @Override
1214         public MethodHandle asTypeUncached(MethodType newType) {
1215             // This MH is an alias for target, except for the MemberName
1216             // Drop the MemberName if there is any conversion.
1217             return target.asType(newType);
1218         }
1219     }
1220 
1221     static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1222         if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
1223             return target;
1224         return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
1225     }
1226 
1227     /** Intrinsic IDs */
1228     /*non-public*/
1229     enum Intrinsic {
1230         SELECT_ALTERNATIVE,
1231         GUARD_WITH_CATCH,
1232         TRY_FINALLY,
1233         TABLE_SWITCH,
1234         LOOP,
1235         ARRAY_LOAD,
1236         ARRAY_STORE,
1237         ARRAY_LENGTH,
1238         IDENTITY,
1239         ZERO,
1240         NONE // no intrinsic associated
1241     }
1242 
1243     /** Mark arbitrary method handle as intrinsic.
1244      * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
1245     static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
1246         private final MethodHandle target;
1247         private final Intrinsic intrinsicName;
1248         private final Object intrinsicData;
1249 
1250         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
1251            this(target, intrinsicName, null);
1252         }
1253 
1254         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1255             super(target.type(), target);
1256             this.target = target;
1257             this.intrinsicName = intrinsicName;
1258             this.intrinsicData = intrinsicData;
1259         }
1260 
1261         @Override
1262         protected MethodHandle getTarget() {
1263             return target;
1264         }
1265 
1266         @Override
1267         Intrinsic intrinsicName() {
1268             return intrinsicName;
1269         }
1270 
1271         @Override
1272         Object intrinsicData() {
1273             return intrinsicData;
1274         }
1275 
1276         @Override
1277         public MethodHandle asTypeUncached(MethodType newType) {
1278             // This MH is an alias for target, except for the intrinsic name
1279             // Drop the name if there is any conversion.
1280             return target.asType(newType);
1281         }
1282 
1283         @Override
1284         String internalProperties() {
1285             return super.internalProperties() +
1286                     "\n& Intrinsic="+intrinsicName;
1287         }
1288 
1289         @Override
1290         public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
1291             if (intrinsicName == Intrinsic.IDENTITY) {
1292                 MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength);
1293                 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
1294                 return newArray.asType(resultType);
1295             }
1296             return super.asCollector(arrayType, arrayLength);
1297         }
1298     }
1299 
1300     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
1301         return makeIntrinsic(target, intrinsicName, null);
1302     }
1303 
1304     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1305         if (intrinsicName == target.intrinsicName())
1306             return target;
1307         return new IntrinsicMethodHandle(target, intrinsicName, intrinsicData);
1308     }
1309 
1310     static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
1311         return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
1312     }
1313 
1314     private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];
1315 
1316     /** Return a method handle that takes the indicated number of Object
1317      *  arguments and returns an Object array of them, as if for varargs.
1318      */
1319     static MethodHandle varargsArray(int nargs) {
1320         MethodHandle mh = ARRAYS[nargs];
1321         if (mh != null) {
1322             return mh;
1323         }
1324         mh = makeCollector(Object[].class, nargs);
1325         assert(assertCorrectArity(mh, nargs));
1326         return ARRAYS[nargs] = mh;
1327     }
1328 
1329     /** Return a method handle that takes the indicated number of
1330      *  typed arguments and returns an array of them.
1331      *  The type argument is the array type.
1332      */
1333     static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
1334         Class<?> elemType = arrayType.getComponentType();
1335         if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
1336         if (nargs >= MAX_JVM_ARITY/2 - 1) {
1337             int slots = nargs;
1338             final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
1339             if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
1340                 slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
1341             if (slots > MAX_ARRAY_SLOTS)
1342                 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
1343         }
1344         if (elemType == Object.class)
1345             return varargsArray(nargs);
1346         // other cases:  primitive arrays, subtypes of Object[]
1347         MethodHandle cache[] = Makers.TYPED_COLLECTORS.get(elemType);
1348         MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
1349         if (mh != null)  return mh;
1350         mh = makeCollector(arrayType, nargs);
1351         assert(assertCorrectArity(mh, nargs));
1352         if (nargs < cache.length)
1353             cache[nargs] = mh;
1354         return mh;
1355     }
1356 
1357     private static boolean assertCorrectArity(MethodHandle mh, int arity) {
1358         assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
1359         return true;
1360     }
1361 
1362     static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
1363 
1364     /*non-public*/
1365     static void assertSame(Object mh1, Object mh2) {
1366         if (mh1 != mh2) {
1367             String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
1368                     mh1, ((MethodHandle)mh1).form,
1369                     mh2, ((MethodHandle)mh2).form);
1370             throw newInternalError(msg);
1371         }
1372     }
1373 
1374     // Local constant functions:
1375 
1376     /* non-public */
1377     static final byte NF_checkSpreadArgument = 0,
1378             NF_guardWithCatch = 1,
1379             NF_throwException = 2,
1380             NF_tryFinally = 3,
1381             NF_loop = 4,
1382             NF_profileBoolean = 5,
1383             NF_tableSwitch = 6,
1384             NF_LIMIT = 7;
1385 
1386     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
1387 
1388     static NamedFunction getFunction(byte func) {
1389         NamedFunction nf = NFS[func];
1390         if (nf != null) {
1391             return nf;
1392         }
1393         return NFS[func] = createFunction(func);
1394     }
1395 
1396     private static NamedFunction createFunction(byte func) {
1397         try {
1398             return switch (func) {
1399                 case NF_checkSpreadArgument -> new NamedFunction(MethodHandleImpl.class
1400                                                 .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
1401                 case NF_guardWithCatch      -> new NamedFunction(MethodHandleImpl.class
1402                                                 .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
1403                                                    MethodHandle.class, Object[].class));
1404                 case NF_tryFinally          -> new NamedFunction(MethodHandleImpl.class
1405                                                 .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class));
1406                 case NF_loop                -> new NamedFunction(MethodHandleImpl.class
1407                                                 .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class));
1408                 case NF_throwException      -> new NamedFunction(MethodHandleImpl.class
1409                                                 .getDeclaredMethod("throwException", Throwable.class));
1410                 case NF_profileBoolean      -> new NamedFunction(MethodHandleImpl.class
1411                                                 .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
1412                 case NF_tableSwitch         -> new NamedFunction(MethodHandleImpl.class
1413                                                 .getDeclaredMethod("tableSwitch", int.class, MethodHandle.class, CasesHolder.class, Object[].class));
1414                 default -> throw new InternalError("Undefined function: " + func);
1415             };
1416         } catch (ReflectiveOperationException ex) {
1417             throw newInternalError(ex);
1418         }
1419     }
1420 
1421     static {
1422         SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
1423             @Override
1424             public Object newMemberName() {
1425                 return new MemberName();
1426             }
1427 
1428             @Override
1429             public String getName(Object mname) {
1430                 MemberName memberName = (MemberName)mname;
1431                 return memberName.getName();
1432             }
1433             @Override
1434             public Class<?> getDeclaringClass(Object mname) {
1435                 MemberName memberName = (MemberName)mname;
1436                 return memberName.getDeclaringClass();
1437             }
1438 
1439             @Override
1440             public MethodType getMethodType(Object mname) {
1441                 MemberName memberName = (MemberName)mname;
1442                 return memberName.getMethodType();
1443             }
1444 
1445             @Override
1446             public String getMethodDescriptor(Object mname) {
1447                 MemberName memberName = (MemberName)mname;
1448                 return memberName.getMethodDescriptor();
1449             }
1450 
1451             @Override
1452             public boolean isNative(Object mname) {
1453                 MemberName memberName = (MemberName)mname;
1454                 return memberName.isNative();
1455             }
1456 
1457             @Override
1458             public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1459                 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1460             }
1461 
1462             @Override
1463             public void ensureCustomized(MethodHandle mh) {
1464                 mh.customize();
1465             }
1466 
1467             @Override
1468             public VarHandle memoryAccessVarHandle(Class<?> carrier, boolean skipAlignmentMaskCheck, long alignmentMask,
1469                                                    ByteOrder order) {
1470                 return VarHandles.makeMemoryAddressViewHandle(carrier, skipAlignmentMaskCheck, alignmentMask, order);
1471             }
1472 
1473             @Override
1474             public MethodHandle nativeMethodHandle(NativeEntryPoint nep, MethodHandle fallback) {
1475                 return NativeMethodHandle.make(nep, fallback);
1476             }
1477 
1478             @Override
1479             public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1480                 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1481             }
1482 
1483             @Override
1484             public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1485                 return VarHandles.filterCoordinates(target, pos, filters);
1486             }
1487 
1488             @Override
1489             public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1490                 return VarHandles.dropCoordinates(target, pos, valueTypes);
1491             }
1492 
1493             @Override
1494             public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1495                 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1496             }
1497 
1498             @Override
1499             public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1500                 return VarHandles.collectCoordinates(target, pos, filter);
1501             }
1502 
1503             @Override
1504             public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1505                 return VarHandles.insertCoordinates(target, pos, values);
1506             }
1507 
1508             @Override
1509             public MethodHandle findStatic(Class<?> cls, String name, MethodType methodType) throws NoSuchMethodException, IllegalAccessException {
1510                 return IMPL_LOOKUP.findStatic(cls, name, methodType);
1511             }
1512 
1513             @Override
1514             public MethodHandle unreflectGetter(Field field) throws IllegalAccessException {
1515                 return IMPL_LOOKUP.unreflectGetter(field);
1516             }
1517         });
1518     }
1519 
1520     /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1521     private static MethodHandle unboxResultHandle(Class<?> returnType) {
1522         if (returnType.isPrimitive()) {
1523             if (returnType == void.class) {
1524                 return ValueConversions.ignore();
1525             } else {
1526                 Wrapper w = Wrapper.forPrimitiveType(returnType);
1527                 return ValueConversions.unboxExact(w);
1528             }
1529         } else {
1530             return MethodHandles.identity(Object.class);
1531         }
1532     }
1533 
1534     /**
1535      * Assembles a loop method handle from the given handles and type information.
1536      *
1537      * @param tloop the return type of the loop.
1538      * @param targs types of the arguments to be passed to the loop.
1539      * @param init sanitized array of initializers for loop-local variables.
1540      * @param step sanitited array of loop bodies.
1541      * @param pred sanitized array of predicates.
1542      * @param fini sanitized array of loop finalizers.
1543      *
1544      * @return a handle that, when invoked, will execute the loop.
1545      */
1546     static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step,
1547                                  List<MethodHandle> pred, List<MethodHandle> fini) {
1548         MethodType type = MethodType.methodType(tloop, targs);
1549         BasicType[] initClauseTypes =
1550                 init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new);
1551         LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
1552 
1553         // Prepare auxiliary method handles used during LambdaForm interpretation.
1554         // Box arguments and wrap them into Object[]: ValueConversions.array().
1555         MethodType varargsType = type.changeReturnType(Object[].class);
1556         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1557         MethodHandle unboxResult = unboxResultHandle(tloop);
1558 
1559         LoopClauses clauseData =
1560                 new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)});
1561         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1562         BoundMethodHandle mh;
1563         try {
1564             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData,
1565                     (Object) collectArgs, (Object) unboxResult);
1566         } catch (Throwable ex) {
1567             throw uncaughtException(ex);
1568         }
1569         assert(mh.type() == type);
1570         return mh;
1571     }
1572 
1573     private static MethodHandle[] toArray(List<MethodHandle> l) {
1574         return l.toArray(new MethodHandle[0]);
1575     }
1576 
1577     /**
1578      * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are
1579      * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one
1580      * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by
1581      * {@code t12}):
1582      * <blockquote><pre>{@code
1583      *  loop=Lambda(a0:L,a1:L)=>{
1584      *    t2:L=BoundMethodHandle$Species_L3.argL0(a0:L);    // LoopClauses holding init, step, pred, fini handles
1585      *    t3:L=BoundMethodHandle$Species_L3.argL1(a0:L);    // helper handle to box the arguments into an Object[]
1586      *    t4:L=BoundMethodHandle$Species_L3.argL2(a0:L);    // helper handle to unbox the result
1587      *    t5:L=MethodHandle.invokeBasic(t3:L,a1:L);         // box the arguments into an Object[]
1588      *    t6:L=MethodHandleImpl.loop(null,t2:L,t3:L);       // call the loop executor
1589      *    t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L}    // unbox the result; return the result
1590      * }</pre></blockquote>
1591      * <p>
1592      * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method
1593      * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps
1594      * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary
1595      * ({@code ValueConversions.unbox()}).
1596      * <p>
1597      * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1598      * forms among loop combinators with the same basic type.
1599      * <p>
1600      * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to
1601      * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is
1602      * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int)
1603      * bytecode generation}.
1604      */
1605     private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
1606         MethodType lambdaType = basicType.invokerType();
1607 
1608         final int THIS_MH = 0;  // the BMH_LLL
1609         final int ARG_BASE = 1; // start of incoming arguments
1610         final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
1611 
1612         int nameCursor = ARG_LIMIT;
1613         final int GET_CLAUSE_DATA = nameCursor++;
1614         final int GET_COLLECT_ARGS = nameCursor++;
1615         final int GET_UNBOX_RESULT = nameCursor++;
1616         final int BOXED_ARGS = nameCursor++;
1617         final int LOOP = nameCursor++;
1618         final int UNBOX_RESULT = nameCursor++;
1619 
1620         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
1621         if (lform == null) {
1622             Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
1623 
1624             BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1625             names[THIS_MH] = names[THIS_MH].withConstraint(data);
1626             names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]);
1627             names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]);
1628             names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]);
1629 
1630             // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
1631             MethodType collectArgsType = basicType.changeReturnType(Object.class);
1632             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
1633             Object[] args = new Object[invokeBasic.type().parameterCount()];
1634             args[0] = names[GET_COLLECT_ARGS];
1635             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
1636             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.LOOP)), args);
1637 
1638             // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L);
1639             Object[] lArgs =
1640                     new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor
1641                             names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
1642             names[LOOP] = new Name(getFunction(NF_loop), lArgs);
1643 
1644             // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
1645             MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
1646             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]};
1647             names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
1648 
1649             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
1650                     new LambdaForm(lambdaType.parameterCount(), names, Kind.LOOP));
1651         }
1652 
1653         // BOXED_ARGS is the index into the names array where the loop idiom starts
1654         return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
1655     }
1656 
1657     static class LoopClauses {
1658         @Stable final MethodHandle[][] clauses;
1659         LoopClauses(MethodHandle[][] clauses) {
1660             assert clauses.length == 4;
1661             this.clauses = clauses;
1662         }
1663         @Override
1664         public String toString() {
1665             StringBuilder sb = new StringBuilder("LoopClauses -- ");
1666             for (int i = 0; i < 4; ++i) {
1667                 if (i > 0) {
1668                     sb.append("       ");
1669                 }
1670                 sb.append('<').append(i).append(">: ");
1671                 MethodHandle[] hs = clauses[i];
1672                 for (int j = 0; j < hs.length; ++j) {
1673                     if (j > 0) {
1674                         sb.append("          ");
1675                     }
1676                     sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
1677                 }
1678             }
1679             sb.append(" --\n");
1680             return sb.toString();
1681         }
1682     }
1683 
1684     /**
1685      * Intrinsified during LambdaForm compilation
1686      * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
1687      */
1688     @Hidden
1689     static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable {
1690         final MethodHandle[] init = clauseData.clauses[0];
1691         final MethodHandle[] step = clauseData.clauses[1];
1692         final MethodHandle[] pred = clauseData.clauses[2];
1693         final MethodHandle[] fini = clauseData.clauses[3];
1694         int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count();
1695         int nArgs = init[0].type().parameterCount();
1696         Object[] varsAndArgs = new Object[varSize + nArgs];
1697         for (int i = 0, v = 0; i < init.length; ++i) {
1698             MethodHandle ih = init[i];
1699             if (ih.type().returnType() == void.class) {
1700                 ih.invokeWithArguments(av);
1701             } else {
1702                 varsAndArgs[v++] = ih.invokeWithArguments(av);
1703             }
1704         }
1705         System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
1706         final int nSteps = step.length;
1707         for (; ; ) {
1708             for (int i = 0, v = 0; i < nSteps; ++i) {
1709                 MethodHandle p = pred[i];
1710                 MethodHandle s = step[i];
1711                 MethodHandle f = fini[i];
1712                 if (s.type().returnType() == void.class) {
1713                     s.invokeWithArguments(varsAndArgs);
1714                 } else {
1715                     varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
1716                 }
1717                 if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
1718                     return f.invokeWithArguments(varsAndArgs);
1719                 }
1720             }
1721         }
1722     }
1723 
1724     /**
1725      * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1726      * MethodHandle) counting loops}.
1727      *
1728      * @param limit the upper bound of the parameter, statically bound at loop creation time.
1729      * @param counter the counter parameter, passed in during loop execution.
1730      *
1731      * @return whether the counter has reached the limit.
1732      */
1733     static boolean countedLoopPredicate(int limit, int counter) {
1734         return counter < limit;
1735     }
1736 
1737     /**
1738      * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1739      * MethodHandle) counting loops} to increment the counter.
1740      *
1741      * @param limit the upper bound of the loop counter (ignored).
1742      * @param counter the loop counter.
1743      *
1744      * @return the loop counter incremented by 1.
1745      */
1746     static int countedLoopStep(int limit, int counter) {
1747         return counter + 1;
1748     }
1749 
1750     /**
1751      * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1752      *
1753      * @param it the {@link Iterable} over which the loop iterates.
1754      *
1755      * @return an {@link Iterator} over the argument's elements.
1756      */
1757     static Iterator<?> initIterator(Iterable<?> it) {
1758         return it.iterator();
1759     }
1760 
1761     /**
1762      * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1763      *
1764      * @param it the iterator to be checked.
1765      *
1766      * @return {@code true} iff there are more elements to iterate over.
1767      */
1768     static boolean iteratePredicate(Iterator<?> it) {
1769         return it.hasNext();
1770     }
1771 
1772     /**
1773      * This method is bound as the step for retrieving the current value from the iterator in {@linkplain
1774      * MethodHandles#iteratedLoop iterating loops}.
1775      *
1776      * @param it the iterator.
1777      *
1778      * @return the next element from the iterator.
1779      */
1780     static Object iterateNext(Iterator<?> it) {
1781         return it.next();
1782     }
1783 
1784     /**
1785      * Makes a {@code try-finally} handle that conforms to the type constraints.
1786      *
1787      * @param target the target to execute in a {@code try-finally} block.
1788      * @param cleanup the cleanup to execute in the {@code finally} block.
1789      * @param rtype the result type of the entire construct.
1790      * @param argTypes the types of the arguments.
1791      *
1792      * @return a handle on the constructed {@code try-finally} block.
1793      */
1794     static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, List<Class<?>> argTypes) {
1795         MethodType type = MethodType.methodType(rtype, argTypes);
1796         LambdaForm form = makeTryFinallyForm(type.basicType());
1797 
1798         // Prepare auxiliary method handles used during LambdaForm interpretation.
1799         // Box arguments and wrap them into Object[]: ValueConversions.array().
1800         MethodType varargsType = type.changeReturnType(Object[].class);
1801         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1802         MethodHandle unboxResult = unboxResultHandle(rtype);
1803 
1804         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
1805         BoundMethodHandle mh;
1806         try {
1807             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup,
1808                     (Object) collectArgs, (Object) unboxResult);
1809         } catch (Throwable ex) {
1810             throw uncaughtException(ex);
1811         }
1812         assert(mh.type() == type);
1813         return mh;
1814     }
1815 
1816     /**
1817      * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in
1818      * {@code a1}, and a reference return type, with the return value represented by {@code t8}):
1819      * <blockquote><pre>{@code
1820      *  tryFinally=Lambda(a0:L,a1:L)=>{
1821      *    t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L);  // target method handle
1822      *    t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L);  // cleanup method handle
1823      *    t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L);  // helper handle to box the arguments into an Object[]
1824      *    t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L);  // helper handle to unbox the result
1825      *    t6:L=MethodHandle.invokeBasic(t4:L,a1:L);         // box the arguments into an Object[]
1826      *    t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor
1827      *    t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L}    // unbox the result; return the result
1828      * }</pre></blockquote>
1829      * <p>
1830      * {@code argL0} and {@code argL1} are the target and cleanup method handles.
1831      * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into
1832      * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary
1833      * ({@code ValueConversions.unbox()}).
1834      * <p>
1835      * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1836      * forms among tryFinally combinators with the same basic type.
1837      */
1838     private static LambdaForm makeTryFinallyForm(MethodType basicType) {
1839         MethodType lambdaType = basicType.invokerType();
1840 
1841         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
1842         if (lform != null) {
1843             return lform;
1844         }
1845         final int THIS_MH      = 0;  // the BMH_LLLL
1846         final int ARG_BASE     = 1;  // start of incoming arguments
1847         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
1848 
1849         int nameCursor = ARG_LIMIT;
1850         final int GET_TARGET       = nameCursor++;
1851         final int GET_CLEANUP      = nameCursor++;
1852         final int GET_COLLECT_ARGS = nameCursor++;
1853         final int GET_UNBOX_RESULT = nameCursor++;
1854         final int BOXED_ARGS       = nameCursor++;
1855         final int TRY_FINALLY      = nameCursor++;
1856         final int UNBOX_RESULT     = nameCursor++;
1857 
1858         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
1859 
1860         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
1861         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
1862         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
1863         names[GET_CLEANUP]      = new Name(data.getterFunction(1), names[THIS_MH]);
1864         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]);
1865         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]);
1866 
1867         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
1868         MethodType collectArgsType = basicType.changeReturnType(Object.class);
1869         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
1870         Object[] args = new Object[invokeBasic.type().parameterCount()];
1871         args[0] = names[GET_COLLECT_ARGS];
1872         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
1873         names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY)), args);
1874 
1875         // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L);
1876         Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]};
1877         names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs);
1878 
1879         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
1880         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
1881         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]};
1882         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
1883 
1884         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TRY_FINALLY);
1885 
1886         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
1887     }
1888 
1889     /**
1890      * Intrinsified during LambdaForm compilation
1891      * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
1892      */
1893     @Hidden
1894     static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable {
1895         Throwable t = null;
1896         Object r = null;
1897         try {
1898             r = target.invokeWithArguments(av);
1899         } catch (Throwable thrown) {
1900             t = thrown;
1901             throw t;
1902         } finally {
1903             Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r);
1904             r = cleanup.invokeWithArguments(args);
1905         }
1906         return r;
1907     }
1908 
1909     // see varargsArray method for chaching/package-private version of this
1910     private static MethodHandle makeCollector(Class<?> arrayType, int parameterCount) {
1911         MethodType type = MethodType.methodType(arrayType, Collections.nCopies(parameterCount, arrayType.componentType()));
1912         MethodHandle newArray = MethodHandles.arrayConstructor(arrayType);
1913 
1914         LambdaForm form = makeCollectorForm(type.basicType(), arrayType);
1915 
1916         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
1917         BoundMethodHandle mh;
1918         try {
1919             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) newArray);
1920         } catch (Throwable ex) {
1921             throw uncaughtException(ex);
1922         }
1923         assert(mh.type() == type);
1924         return mh;
1925     }
1926 
1927     private static LambdaForm makeCollectorForm(MethodType basicType, Class<?> arrayType) {
1928         MethodType lambdaType = basicType.invokerType();
1929         int parameterCount = basicType.parameterCount();
1930 
1931         // Only share the lambda form for empty arrays and reference types.
1932         // Sharing based on the basic type alone doesn't work because
1933         // we need a separate lambda form for byte/short/char/int which
1934         // are all erased to int otherwise.
1935         // Other caching for primitive types happens at the MethodHandle level (see varargsArray).
1936         boolean isReferenceType = !arrayType.componentType().isPrimitive();
1937         boolean isSharedLambdaForm = parameterCount == 0 || isReferenceType;
1938         if (isSharedLambdaForm) {
1939             LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_COLLECTOR);
1940             if (lform != null) {
1941                 return lform;
1942             }
1943         }
1944 
1945         // use erased accessor for reference types
1946         MethodHandle storeFunc = isReferenceType
1947                 ? ArrayAccessor.OBJECT_ARRAY_SETTER
1948                 : makeArrayElementAccessor(arrayType, ArrayAccess.SET);
1949 
1950         final int THIS_MH      = 0;  // the BMH_L
1951         final int ARG_BASE     = 1;  // start of incoming arguments
1952         final int ARG_LIMIT    = ARG_BASE + parameterCount;
1953 
1954         int nameCursor = ARG_LIMIT;
1955         final int GET_NEW_ARRAY       = nameCursor++;
1956         final int CALL_NEW_ARRAY      = nameCursor++;
1957         final int STORE_ELEMENT_BASE  = nameCursor;
1958         final int STORE_ELEMENT_LIMIT = STORE_ELEMENT_BASE + parameterCount;
1959         nameCursor = STORE_ELEMENT_LIMIT;
1960 
1961         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
1962 
1963         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
1964         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
1965         names[GET_NEW_ARRAY]    = new Name(data.getterFunction(0), names[THIS_MH]);
1966 
1967         MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(Object.class, int.class));
1968         names[CALL_NEW_ARRAY] = new Name(new NamedFunction(invokeBasic), names[GET_NEW_ARRAY], parameterCount);
1969         for (int storeIndex = 0,
1970              storeNameCursor = STORE_ELEMENT_BASE,
1971              argCursor = ARG_BASE;
1972              storeNameCursor < STORE_ELEMENT_LIMIT;
1973              storeIndex++, storeNameCursor++, argCursor++){
1974 
1975             names[storeNameCursor] = new Name(new NamedFunction(makeIntrinsic(storeFunc, Intrinsic.ARRAY_STORE)),
1976                     names[CALL_NEW_ARRAY], storeIndex, names[argCursor]);
1977         }
1978 
1979         LambdaForm lform = new LambdaForm(lambdaType.parameterCount(), names, CALL_NEW_ARRAY, Kind.COLLECTOR);
1980         if (isSharedLambdaForm) {
1981             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_COLLECTOR, lform);
1982         }
1983         return lform;
1984     }
1985 
1986     // use a wrapper because we need this array to be @Stable
1987     static class CasesHolder {
1988         @Stable
1989         final MethodHandle[] cases;
1990 
1991         public CasesHolder(MethodHandle[] cases) {
1992             this.cases = cases;
1993         }
1994     }
1995 
1996     static MethodHandle makeTableSwitch(MethodType type, MethodHandle defaultCase, MethodHandle[] caseActions) {
1997         MethodType varargsType = type.changeReturnType(Object[].class);
1998         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1999 
2000         MethodHandle unboxResult = unboxResultHandle(type.returnType());
2001 
2002         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2003         LambdaForm form = makeTableSwitchForm(type.basicType(), data, caseActions.length);
2004         BoundMethodHandle mh;
2005         CasesHolder caseHolder =  new CasesHolder(caseActions);
2006         try {
2007             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) defaultCase, (Object) collectArgs,
2008                                                                 (Object) unboxResult, (Object) caseHolder);
2009         } catch (Throwable ex) {
2010             throw uncaughtException(ex);
2011         }
2012         assert(mh.type() == type);
2013         return mh;
2014     }
2015 
2016     private static class TableSwitchCacheKey {
2017         private static final Map<TableSwitchCacheKey, LambdaForm> CACHE = new ConcurrentHashMap<>();
2018 
2019         private final MethodType basicType;
2020         private final int numberOfCases;
2021 
2022         public TableSwitchCacheKey(MethodType basicType, int numberOfCases) {
2023             this.basicType = basicType;
2024             this.numberOfCases = numberOfCases;
2025         }
2026 
2027         @Override
2028         public boolean equals(Object o) {
2029             if (this == o) return true;
2030             if (o == null || getClass() != o.getClass()) return false;
2031             TableSwitchCacheKey that = (TableSwitchCacheKey) o;
2032             return numberOfCases == that.numberOfCases && Objects.equals(basicType, that.basicType);
2033         }
2034         @Override
2035         public int hashCode() {
2036             return Objects.hash(basicType, numberOfCases);
2037         }
2038     }
2039 
2040     private static LambdaForm makeTableSwitchForm(MethodType basicType, BoundMethodHandle.SpeciesData data,
2041                                                   int numCases) {
2042         MethodType lambdaType = basicType.invokerType();
2043 
2044         // We need to cache based on the basic type X number of cases,
2045         // since the number of cases is used when generating bytecode.
2046         // This also means that we can't use the cache in MethodTypeForm,
2047         // which only uses the basic type as a key.
2048         TableSwitchCacheKey key = new TableSwitchCacheKey(basicType, numCases);
2049         LambdaForm lform = TableSwitchCacheKey.CACHE.get(key);
2050         if (lform != null) {
2051             return lform;
2052         }
2053 
2054         final int THIS_MH       = 0;
2055         final int ARG_BASE      = 1;  // start of incoming arguments
2056         final int ARG_LIMIT     = ARG_BASE + basicType.parameterCount();
2057         final int ARG_SWITCH_ON = ARG_BASE;
2058         assert ARG_SWITCH_ON < ARG_LIMIT;
2059 
2060         int nameCursor = ARG_LIMIT;
2061         final int GET_COLLECT_ARGS  = nameCursor++;
2062         final int GET_DEFAULT_CASE  = nameCursor++;
2063         final int GET_UNBOX_RESULT  = nameCursor++;
2064         final int GET_CASES         = nameCursor++;
2065         final int BOXED_ARGS        = nameCursor++;
2066         final int TABLE_SWITCH      = nameCursor++;
2067         final int UNBOXED_RESULT    = nameCursor++;
2068 
2069         int fieldCursor = 0;
2070         final int FIELD_DEFAULT_CASE  = fieldCursor++;
2071         final int FIELD_COLLECT_ARGS  = fieldCursor++;
2072         final int FIELD_UNBOX_RESULT  = fieldCursor++;
2073         final int FIELD_CASES         = fieldCursor++;
2074 
2075         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
2076 
2077         names[THIS_MH] = names[THIS_MH].withConstraint(data);
2078         names[GET_DEFAULT_CASE] = new Name(data.getterFunction(FIELD_DEFAULT_CASE), names[THIS_MH]);
2079         names[GET_COLLECT_ARGS]  = new Name(data.getterFunction(FIELD_COLLECT_ARGS), names[THIS_MH]);
2080         names[GET_UNBOX_RESULT]  = new Name(data.getterFunction(FIELD_UNBOX_RESULT), names[THIS_MH]);
2081         names[GET_CASES] = new Name(data.getterFunction(FIELD_CASES), names[THIS_MH]);
2082 
2083         {
2084             MethodType collectArgsType = basicType.changeReturnType(Object.class);
2085             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2086             Object[] args = new Object[invokeBasic.type().parameterCount()];
2087             args[0] = names[GET_COLLECT_ARGS];
2088             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
2089             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TABLE_SWITCH, numCases)), args);
2090         }
2091 
2092         {
2093             Object[] tfArgs = new Object[]{
2094                 names[ARG_SWITCH_ON], names[GET_DEFAULT_CASE], names[GET_CASES], names[BOXED_ARGS]};
2095             names[TABLE_SWITCH] = new Name(getFunction(NF_tableSwitch), tfArgs);
2096         }
2097 
2098         {
2099             MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2100             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[TABLE_SWITCH]};
2101             names[UNBOXED_RESULT] = new Name(invokeBasic, unboxArgs);
2102         }
2103 
2104         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TABLE_SWITCH);
2105         LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2106         return prev != null ? prev : lform;
2107     }
2108 
2109     @Hidden
2110     static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2111         MethodHandle[] caseActions = holder.cases;
2112         MethodHandle selectedCase;
2113         if (input < 0 || input >= caseActions.length) {
2114             selectedCase = defaultCase;
2115         } else {
2116             selectedCase = caseActions[input];
2117         }
2118         return selectedCase.invokeWithArguments(args);
2119     }
2120 
2121     // Indexes into constant method handles:
2122     static final int
2123             MH_cast                  = 0,
2124             MH_selectAlternative     = 1,
2125             MH_countedLoopPred       = 2,
2126             MH_countedLoopStep       = 3,
2127             MH_initIterator          = 4,
2128             MH_iteratePred           = 5,
2129             MH_iterateNext           = 6,
2130             MH_Array_newInstance     = 7,
2131             MH_LIMIT                 = 8;
2132 
2133     static MethodHandle getConstantHandle(int idx) {
2134         MethodHandle handle = HANDLES[idx];
2135         if (handle != null) {
2136             return handle;
2137         }
2138         return setCachedHandle(idx, makeConstantHandle(idx));
2139     }
2140 
2141     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2142         // Simulate a CAS, to avoid racy duplication of results.
2143         MethodHandle prev = HANDLES[idx];
2144         if (prev != null) {
2145             return prev;
2146         }
2147         HANDLES[idx] = method;
2148         return method;
2149     }
2150 
2151     // Local constant method handles:
2152     private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
2153 
2154     private static MethodHandle makeConstantHandle(int idx) {
2155         try {
2156             switch (idx) {
2157                 case MH_cast:
2158                     return IMPL_LOOKUP.findVirtual(Class.class, "cast",
2159                             MethodType.methodType(Object.class, Object.class));
2160                 case MH_selectAlternative:
2161                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2162                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2163                 case MH_countedLoopPred:
2164                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2165                             MethodType.methodType(boolean.class, int.class, int.class));
2166                 case MH_countedLoopStep:
2167                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2168                             MethodType.methodType(int.class, int.class, int.class));
2169                 case MH_initIterator:
2170                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2171                             MethodType.methodType(Iterator.class, Iterable.class));
2172                 case MH_iteratePred:
2173                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2174                             MethodType.methodType(boolean.class, Iterator.class));
2175                 case MH_iterateNext:
2176                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2177                             MethodType.methodType(Object.class, Iterator.class));
2178                 case MH_Array_newInstance:
2179                     return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2180                             MethodType.methodType(Object.class, Class.class, int.class));
2181             }
2182         } catch (ReflectiveOperationException ex) {
2183             throw newInternalError(ex);
2184         }
2185         throw newInternalError("Unknown function index: " + idx);
2186     }
2187 }