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