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             public boolean isNullRestrictedField(MethodHandle mh) {
1565                 var memberName = mh.internalMemberName();
1566                 assert memberName.isField();
1567                 return memberName.isNullRestricted();
1568             }
1569 
1570             @Override
1571             public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1572                 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1573             }
1574 
1575             @Override
1576             public VarHandle memorySegmentViewHandle(Class<?> carrier, MemoryLayout enclosing, long alignmentMask, ByteOrder order, boolean constantOffset, long offset) {
1577                 return VarHandles.memorySegmentViewHandle(carrier, enclosing, alignmentMask, constantOffset, offset, order);
1578             }
1579 
1580             @Override
1581             public MethodHandle nativeMethodHandle(NativeEntryPoint nep) {
1582                 return NativeMethodHandle.make(nep);
1583             }
1584 
1585             @Override
1586             public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1587                 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1588             }
1589 
1590             @Override
1591             public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1592                 return VarHandles.filterCoordinates(target, pos, filters);
1593             }
1594 
1595             @Override
1596             public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1597                 return VarHandles.dropCoordinates(target, pos, valueTypes);
1598             }
1599 
1600             @Override
1601             public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1602                 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1603             }
1604 
1605             @Override
1606             public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1607                 return VarHandles.collectCoordinates(target, pos, filter);
1608             }
1609 
1610             @Override
1611             public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1612                 return VarHandles.insertCoordinates(target, pos, values);
1613             }
1614 
1615 
1616             @Override
1617             public MethodHandle unreflectConstructor(Constructor<?> ctor) throws IllegalAccessException {
1618                 return IMPL_LOOKUP.unreflectConstructor(ctor);
1619             }
1620 
1621             @Override
1622             public MethodHandle unreflectField(Field field, boolean isSetter) throws IllegalAccessException {
1623                 return isSetter ? IMPL_LOOKUP.unreflectSetter(field) : IMPL_LOOKUP.unreflectGetter(field);
1624             }
1625 
1626             @Override
1627             public MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1628                 try {
1629                     return IMPL_LOOKUP.findVirtual(defc, name, type);
1630                 } catch (NoSuchMethodException e) {
1631                     return null;
1632                 }
1633             }
1634 
1635             @Override
1636             public MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1637                 try {
1638                     return IMPL_LOOKUP.findStatic(defc, name, type);
1639                 } catch (NoSuchMethodException e) {
1640                     return null;
1641                 }
1642             }
1643 
1644             @Override
1645             public MethodHandle reflectiveInvoker(Class<?> caller) {
1646                 Objects.requireNonNull(caller);
1647                 return BindCaller.reflectiveInvoker(caller);
1648             }
1649 
1650             @Override
1651             public Class<?>[] exceptionTypes(MethodHandle handle) {
1652                 return VarHandles.exceptionTypes(handle);
1653             }
1654 
1655             @Override
1656             public MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException {
1657                 return IMPL_LOOKUP.serializableConstructor(decl, ctorToCall);
1658             }
1659 
1660             @Override
1661             public MethodHandle assertAsType(MethodHandle original, MethodType assertedType) {
1662                 return original.viewAsType(assertedType, false);
1663             }
1664         });
1665     }
1666 
1667     /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1668     private static MethodHandle unboxResultHandle(Class<?> returnType) {
1669         if (returnType.isPrimitive()) {
1670             if (returnType == void.class) {
1671                 return ValueConversions.ignore();
1672             } else {
1673                 Wrapper w = Wrapper.forPrimitiveType(returnType);
1674                 return ValueConversions.unboxExact(w);
1675             }
1676         } else {
1677             return MethodHandles.identity(Object.class);
1678         }
1679     }
1680 
1681     /**
1682      * Assembles a loop method handle from the given handles and type information.
1683      *
1684      * @param tloop the return type of the loop.
1685      * @param targs types of the arguments to be passed to the loop.
1686      * @param init sanitized array of initializers for loop-local variables.
1687      * @param step sanitized array of loop bodies.
1688      * @param pred sanitized array of predicates.
1689      * @param fini sanitized array of loop finalizers.
1690      *
1691      * @return a handle that, when invoked, will execute the loop.
1692      */
1693     static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step,
1694                                  List<MethodHandle> pred, List<MethodHandle> fini) {
1695         MethodType type = MethodType.methodType(tloop, targs);
1696         BasicType[] initClauseTypes =
1697                 init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new);
1698         LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
1699 
1700         // Prepare auxiliary method handles used during LambdaForm interpretation.
1701         // Box arguments and wrap them into Object[]: ValueConversions.array().
1702         MethodType varargsType = type.changeReturnType(Object[].class);
1703         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1704         MethodHandle unboxResult = unboxResultHandle(tloop);
1705 
1706         LoopClauses clauseData =
1707                 new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)});
1708         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1709         BoundMethodHandle mh;
1710         try {
1711             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData,
1712                     (Object) collectArgs, (Object) unboxResult);
1713         } catch (Throwable ex) {
1714             throw uncaughtException(ex);
1715         }
1716         assert(mh.type() == type);
1717         return mh;
1718     }
1719 
1720     private static MethodHandle[] toArray(List<MethodHandle> l) {
1721         return l.toArray(new MethodHandle[0]);
1722     }
1723 
1724     /**
1725      * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are
1726      * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one
1727      * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by
1728      * {@code t12}):
1729      * <blockquote><pre>{@code
1730      *  loop=Lambda(a0:L,a1:L)=>{
1731      *    t2:L=BoundMethodHandle$Species_L3.argL0(a0:L);    // LoopClauses holding init, step, pred, fini handles
1732      *    t3:L=BoundMethodHandle$Species_L3.argL1(a0:L);    // helper handle to box the arguments into an Object[]
1733      *    t4:L=BoundMethodHandle$Species_L3.argL2(a0:L);    // helper handle to unbox the result
1734      *    t5:L=MethodHandle.invokeBasic(t3:L,a1:L);         // box the arguments into an Object[]
1735      *    t6:L=MethodHandleImpl.loop(null,t2:L,t3:L);       // call the loop executor
1736      *    t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L}    // unbox the result; return the result
1737      * }</pre></blockquote>
1738      * <p>
1739      * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method
1740      * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps
1741      * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary
1742      * ({@code ValueConversions.unbox()}).
1743      * <p>
1744      * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1745      * forms among loop combinators with the same basic type.
1746      * <p>
1747      * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to
1748      * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is
1749      * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int)
1750      * bytecode generation}.
1751      */
1752     private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
1753         final int THIS_MH = 0;  // the BMH_LLL
1754         final int ARG_BASE = 1; // start of incoming arguments
1755         final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
1756 
1757         int nameCursor = ARG_LIMIT;
1758         final int GET_CLAUSE_DATA = nameCursor++;
1759         final int GET_COLLECT_ARGS = nameCursor++;
1760         final int GET_UNBOX_RESULT = nameCursor++;
1761         final int BOXED_ARGS = nameCursor++;
1762         final int LOOP = nameCursor++;
1763         final int UNBOX_RESULT = nameCursor++;
1764 
1765         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
1766         if (lform == null) {
1767             Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
1768 
1769             BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1770             names[THIS_MH] = names[THIS_MH].withConstraint(data);
1771             names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]);
1772             names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]);
1773             names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]);
1774 
1775             // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
1776             MethodType collectArgsType = basicType.changeReturnType(Object.class);
1777             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
1778             Object[] args = new Object[invokeBasic.type().parameterCount()];
1779             args[0] = names[GET_COLLECT_ARGS];
1780             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
1781             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.LOOP)), args);
1782 
1783             // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L);
1784             Object[] lArgs =
1785                     new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor
1786                             names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
1787             names[LOOP] = new Name(getFunction(NF_loop), lArgs);
1788 
1789             // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
1790             MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
1791             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]};
1792             names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
1793 
1794             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
1795                     LambdaForm.create(basicType.parameterCount() + 1, names, Kind.LOOP));
1796         }
1797 
1798         // BOXED_ARGS is the index into the names array where the loop idiom starts
1799         return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
1800     }
1801 
1802     @AOTSafeClassInitializer
1803     static class LoopClauses {
1804         @Stable final MethodHandle[][] clauses;
1805         LoopClauses(MethodHandle[][] clauses) {
1806             assert clauses.length == 4;
1807             this.clauses = clauses;
1808         }
1809         @Override
1810         public String toString() {
1811             StringBuilder sb = new StringBuilder("LoopClauses -- ");
1812             for (int i = 0; i < 4; ++i) {
1813                 if (i > 0) {
1814                     sb.append("       ");
1815                 }
1816                 sb.append('<').append(i).append(">: ");
1817                 MethodHandle[] hs = clauses[i];
1818                 for (int j = 0; j < hs.length; ++j) {
1819                     if (j > 0) {
1820                         sb.append("          ");
1821                     }
1822                     sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
1823                 }
1824             }
1825             sb.append(" --\n");
1826             return sb.toString();
1827         }
1828     }
1829 
1830     /**
1831      * Intrinsified during LambdaForm compilation
1832      * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
1833      */
1834     @Hidden
1835     static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable {
1836         final MethodHandle[] init = clauseData.clauses[0];
1837         final MethodHandle[] step = clauseData.clauses[1];
1838         final MethodHandle[] pred = clauseData.clauses[2];
1839         final MethodHandle[] fini = clauseData.clauses[3];
1840         int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count();
1841         int nArgs = init[0].type().parameterCount();
1842         Object[] varsAndArgs = new Object[varSize + nArgs];
1843         for (int i = 0, v = 0; i < init.length; ++i) {
1844             MethodHandle ih = init[i];
1845             if (ih.type().returnType() == void.class) {
1846                 ih.invokeWithArguments(av);
1847             } else {
1848                 varsAndArgs[v++] = ih.invokeWithArguments(av);
1849             }
1850         }
1851         System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
1852         final int nSteps = step.length;
1853         for (; ; ) {
1854             for (int i = 0, v = 0; i < nSteps; ++i) {
1855                 MethodHandle p = pred[i];
1856                 MethodHandle s = step[i];
1857                 MethodHandle f = fini[i];
1858                 if (s.type().returnType() == void.class) {
1859                     s.invokeWithArguments(varsAndArgs);
1860                 } else {
1861                     varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
1862                 }
1863                 if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
1864                     return f.invokeWithArguments(varsAndArgs);
1865                 }
1866             }
1867         }
1868     }
1869 
1870     /**
1871      * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1872      * MethodHandle) counting loops}.
1873      *
1874      * @param limit the upper bound of the parameter, statically bound at loop creation time.
1875      * @param counter the counter parameter, passed in during loop execution.
1876      *
1877      * @return whether the counter has reached the limit.
1878      */
1879     static boolean countedLoopPredicate(int limit, int counter) {
1880         return counter < limit;
1881     }
1882 
1883     /**
1884      * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1885      * MethodHandle) counting loops} to increment the counter.
1886      *
1887      * @param limit the upper bound of the loop counter (ignored).
1888      * @param counter the loop counter.
1889      *
1890      * @return the loop counter incremented by 1.
1891      */
1892     static int countedLoopStep(int limit, int counter) {
1893         return counter + 1;
1894     }
1895 
1896     /**
1897      * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1898      *
1899      * @param it the {@link Iterable} over which the loop iterates.
1900      *
1901      * @return an {@link Iterator} over the argument's elements.
1902      */
1903     static Iterator<?> initIterator(Iterable<?> it) {
1904         return it.iterator();
1905     }
1906 
1907     /**
1908      * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1909      *
1910      * @param it the iterator to be checked.
1911      *
1912      * @return {@code true} iff there are more elements to iterate over.
1913      */
1914     static boolean iteratePredicate(Iterator<?> it) {
1915         return it.hasNext();
1916     }
1917 
1918     /**
1919      * This method is bound as the step for retrieving the current value from the iterator in {@linkplain
1920      * MethodHandles#iteratedLoop iterating loops}.
1921      *
1922      * @param it the iterator.
1923      *
1924      * @return the next element from the iterator.
1925      */
1926     static Object iterateNext(Iterator<?> it) {
1927         return it.next();
1928     }
1929 
1930     /**
1931      * Makes a {@code try-finally} handle that conforms to the type constraints.
1932      *
1933      * @param target the target to execute in a {@code try-finally} block.
1934      * @param cleanup the cleanup to execute in the {@code finally} block.
1935      * @param rtype the result type of the entire construct.
1936      * @param argTypes the types of the arguments.
1937      *
1938      * @return a handle on the constructed {@code try-finally} block.
1939      */
1940     static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, Class<?>[] argTypes) {
1941         MethodType type = MethodType.methodType(rtype, argTypes);
1942         LambdaForm form = makeTryFinallyForm(type.basicType());
1943 
1944         // Prepare auxiliary method handles used during LambdaForm interpretation.
1945         // Box arguments and wrap them into Object[]: ValueConversions.array().
1946         MethodType varargsType = type.changeReturnType(Object[].class);
1947         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1948         MethodHandle unboxResult = unboxResultHandle(rtype);
1949 
1950         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
1951         BoundMethodHandle mh;
1952         try {
1953             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup,
1954                     (Object) collectArgs, (Object) unboxResult);
1955         } catch (Throwable ex) {
1956             throw uncaughtException(ex);
1957         }
1958         assert(mh.type() == type);
1959         return mh;
1960     }
1961 
1962     /**
1963      * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in
1964      * {@code a1}, and a reference return type, with the return value represented by {@code t8}):
1965      * <blockquote><pre>{@code
1966      *  tryFinally=Lambda(a0:L,a1:L)=>{
1967      *    t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L);  // target method handle
1968      *    t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L);  // cleanup method handle
1969      *    t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L);  // helper handle to box the arguments into an Object[]
1970      *    t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L);  // helper handle to unbox the result
1971      *    t6:L=MethodHandle.invokeBasic(t4:L,a1:L);         // box the arguments into an Object[]
1972      *    t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor
1973      *    t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L}    // unbox the result; return the result
1974      * }</pre></blockquote>
1975      * <p>
1976      * {@code argL0} and {@code argL1} are the target and cleanup method handles.
1977      * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into
1978      * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary
1979      * ({@code ValueConversions.unbox()}).
1980      * <p>
1981      * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1982      * forms among tryFinally combinators with the same basic type.
1983      */
1984     private static LambdaForm makeTryFinallyForm(MethodType basicType) {
1985         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
1986         if (lform != null) {
1987             return lform;
1988         }
1989         final int THIS_MH      = 0;  // the BMH_LLLL
1990         final int ARG_BASE     = 1;  // start of incoming arguments
1991         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
1992 
1993         int nameCursor = ARG_LIMIT;
1994         final int GET_TARGET       = nameCursor++;
1995         final int GET_CLEANUP      = nameCursor++;
1996         final int GET_COLLECT_ARGS = nameCursor++;
1997         final int GET_UNBOX_RESULT = nameCursor++;
1998         final int BOXED_ARGS       = nameCursor++;
1999         final int TRY_FINALLY      = nameCursor++;
2000         final int UNBOX_RESULT     = nameCursor++;
2001 
2002         Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2003 
2004         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2005         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
2006         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
2007         names[GET_CLEANUP]      = new Name(data.getterFunction(1), names[THIS_MH]);
2008         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]);
2009         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]);
2010 
2011         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
2012         MethodType collectArgsType = basicType.changeReturnType(Object.class);
2013         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2014         Object[] args = new Object[invokeBasic.type().parameterCount()];
2015         args[0] = names[GET_COLLECT_ARGS];
2016         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
2017         names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY)), args);
2018 
2019         // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L);
2020         Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]};
2021         names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs);
2022 
2023         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
2024         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2025         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]};
2026         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
2027 
2028         lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TRY_FINALLY);
2029 
2030         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
2031     }
2032 
2033     /**
2034      * Intrinsified during LambdaForm compilation
2035      * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
2036      */
2037     @Hidden
2038     static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable {
2039         Throwable t = null;
2040         Object r = null;
2041         try {
2042             r = target.invokeWithArguments(av);
2043         } catch (Throwable thrown) {
2044             t = thrown;
2045             throw t;
2046         } finally {
2047             Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r);
2048             r = cleanup.invokeWithArguments(args);
2049         }
2050         return r;
2051     }
2052 
2053     // see varargsArray method for chaching/package-private version of this
2054     private static MethodHandle makeCollector(Class<?> arrayType, int parameterCount) {
2055         MethodType type = MethodType.methodType(arrayType, Collections.nCopies(parameterCount, arrayType.componentType()));
2056         MethodHandle newArray = MethodHandles.arrayConstructor(arrayType);
2057 
2058         LambdaForm form = makeCollectorForm(type.basicType(), arrayType);
2059 
2060         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2061         BoundMethodHandle mh;
2062         try {
2063             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) newArray);
2064         } catch (Throwable ex) {
2065             throw uncaughtException(ex);
2066         }
2067         assert(mh.type() == type);
2068         return mh;
2069     }
2070 
2071     private static LambdaForm makeCollectorForm(MethodType basicType, Class<?> arrayType) {
2072         int parameterCount = basicType.parameterCount();
2073 
2074         // Only share the lambda form for empty arrays and reference types.
2075         // Sharing based on the basic type alone doesn't work because
2076         // we need a separate lambda form for byte/short/char/int which
2077         // are all erased to int otherwise.
2078         // Other caching for primitive types happens at the MethodHandle level (see varargsArray).
2079         boolean isReferenceType = !arrayType.componentType().isPrimitive();
2080         boolean isSharedLambdaForm = parameterCount == 0 || isReferenceType;
2081         if (isSharedLambdaForm) {
2082             LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_COLLECTOR);
2083             if (lform != null) {
2084                 return lform;
2085             }
2086         }
2087 
2088         // use erased accessor for reference types
2089         MethodHandle storeFunc = isReferenceType
2090                 ? ArrayAccessor.OBJECT_ARRAY_SETTER
2091                 : makeArrayElementAccessor(arrayType, ArrayAccess.SET);
2092 
2093         final int THIS_MH      = 0;  // the BMH_L
2094         final int ARG_BASE     = 1;  // start of incoming arguments
2095         final int ARG_LIMIT    = ARG_BASE + parameterCount;
2096 
2097         int nameCursor = ARG_LIMIT;
2098         final int GET_NEW_ARRAY       = nameCursor++;
2099         final int CALL_NEW_ARRAY      = nameCursor++;
2100         final int STORE_ELEMENT_BASE  = nameCursor;
2101         final int STORE_ELEMENT_LIMIT = STORE_ELEMENT_BASE + parameterCount;
2102         nameCursor = STORE_ELEMENT_LIMIT;
2103 
2104         Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2105 
2106         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2107         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
2108         names[GET_NEW_ARRAY]    = new Name(data.getterFunction(0), names[THIS_MH]);
2109 
2110         MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(Object.class, int.class));
2111         names[CALL_NEW_ARRAY] = new Name(new NamedFunction(invokeBasic), names[GET_NEW_ARRAY], parameterCount);
2112         for (int storeIndex = 0,
2113              storeNameCursor = STORE_ELEMENT_BASE,
2114              argCursor = ARG_BASE;
2115              storeNameCursor < STORE_ELEMENT_LIMIT;
2116              storeIndex++, storeNameCursor++, argCursor++){
2117 
2118             names[storeNameCursor] = new Name(new NamedFunction(makeIntrinsic(storeFunc, Intrinsic.ARRAY_STORE)),
2119                     names[CALL_NEW_ARRAY], storeIndex, names[argCursor]);
2120         }
2121 
2122         LambdaForm lform = LambdaForm.create(basicType.parameterCount() + 1, names, CALL_NEW_ARRAY, Kind.COLLECTOR);
2123         if (isSharedLambdaForm) {
2124             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_COLLECTOR, lform);
2125         }
2126         return lform;
2127     }
2128 
2129     // use a wrapper because we need this array to be @Stable
2130     @AOTSafeClassInitializer
2131     static class CasesHolder {
2132         @Stable
2133         final MethodHandle[] cases;
2134 
2135         public CasesHolder(MethodHandle[] cases) {
2136             this.cases = cases;
2137         }
2138     }
2139 
2140     static MethodHandle makeTableSwitch(MethodType type, MethodHandle defaultCase, MethodHandle[] caseActions) {
2141         MethodType varargsType = type.changeReturnType(Object[].class);
2142         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
2143 
2144         MethodHandle unboxResult = unboxResultHandle(type.returnType());
2145 
2146         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2147         LambdaForm form = makeTableSwitchForm(type.basicType(), data, caseActions.length);
2148         BoundMethodHandle mh;
2149         CasesHolder caseHolder =  new CasesHolder(caseActions);
2150         try {
2151             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) defaultCase, (Object) collectArgs,
2152                                                                 (Object) unboxResult, (Object) caseHolder);
2153         } catch (Throwable ex) {
2154             throw uncaughtException(ex);
2155         }
2156         assert(mh.type() == type);
2157         return mh;
2158     }
2159 
2160     @AOTSafeClassInitializer
2161     private static class TableSwitchCacheKey {
2162         private static final Map<TableSwitchCacheKey, LambdaForm> CACHE = new ConcurrentHashMap<>();
2163 
2164         private final MethodType basicType;
2165         private final int numberOfCases;
2166 
2167         public TableSwitchCacheKey(MethodType basicType, int numberOfCases) {
2168             this.basicType = basicType;
2169             this.numberOfCases = numberOfCases;
2170         }
2171 
2172         @Override
2173         public boolean equals(Object o) {
2174             if (this == o) return true;
2175             if (o == null || getClass() != o.getClass()) return false;
2176             TableSwitchCacheKey that = (TableSwitchCacheKey) o;
2177             return numberOfCases == that.numberOfCases && Objects.equals(basicType, that.basicType);
2178         }
2179         @Override
2180         public int hashCode() {
2181             return Objects.hash(basicType, numberOfCases);
2182         }
2183     }
2184 
2185     private static LambdaForm makeTableSwitchForm(MethodType basicType, BoundMethodHandle.SpeciesData data,
2186                                                   int numCases) {
2187         // We need to cache based on the basic type X number of cases,
2188         // since the number of cases is used when generating bytecode.
2189         // This also means that we can't use the cache in MethodTypeForm,
2190         // which only uses the basic type as a key.
2191         TableSwitchCacheKey key = new TableSwitchCacheKey(basicType, numCases);
2192         LambdaForm lform = TableSwitchCacheKey.CACHE.get(key);
2193         if (lform != null) {
2194             return lform;
2195         }
2196 
2197         final int THIS_MH       = 0;
2198         final int ARG_BASE      = 1;  // start of incoming arguments
2199         final int ARG_LIMIT     = ARG_BASE + basicType.parameterCount();
2200         final int ARG_SWITCH_ON = ARG_BASE;
2201         assert ARG_SWITCH_ON < ARG_LIMIT;
2202 
2203         int nameCursor = ARG_LIMIT;
2204         final int GET_COLLECT_ARGS  = nameCursor++;
2205         final int GET_DEFAULT_CASE  = nameCursor++;
2206         final int GET_UNBOX_RESULT  = nameCursor++;
2207         final int GET_CASES         = nameCursor++;
2208         final int BOXED_ARGS        = nameCursor++;
2209         final int TABLE_SWITCH      = nameCursor++;
2210         final int UNBOXED_RESULT    = nameCursor++;
2211 
2212         int fieldCursor = 0;
2213         final int FIELD_DEFAULT_CASE  = fieldCursor++;
2214         final int FIELD_COLLECT_ARGS  = fieldCursor++;
2215         final int FIELD_UNBOX_RESULT  = fieldCursor++;
2216         final int FIELD_CASES         = fieldCursor++;
2217 
2218         Name[] names = invokeArguments(nameCursor - ARG_LIMIT, basicType);
2219 
2220         names[THIS_MH] = names[THIS_MH].withConstraint(data);
2221         names[GET_DEFAULT_CASE] = new Name(data.getterFunction(FIELD_DEFAULT_CASE), names[THIS_MH]);
2222         names[GET_COLLECT_ARGS]  = new Name(data.getterFunction(FIELD_COLLECT_ARGS), names[THIS_MH]);
2223         names[GET_UNBOX_RESULT]  = new Name(data.getterFunction(FIELD_UNBOX_RESULT), names[THIS_MH]);
2224         names[GET_CASES] = new Name(data.getterFunction(FIELD_CASES), names[THIS_MH]);
2225 
2226         {
2227             MethodType collectArgsType = basicType.changeReturnType(Object.class);
2228             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2229             Object[] args = new Object[invokeBasic.type().parameterCount()];
2230             args[0] = names[GET_COLLECT_ARGS];
2231             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
2232             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TABLE_SWITCH, numCases)), args);
2233         }
2234 
2235         {
2236             Object[] tfArgs = new Object[]{
2237                 names[ARG_SWITCH_ON], names[GET_DEFAULT_CASE], names[GET_CASES], names[BOXED_ARGS]};
2238             names[TABLE_SWITCH] = new Name(getFunction(NF_tableSwitch), tfArgs);
2239         }
2240 
2241         {
2242             MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2243             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[TABLE_SWITCH]};
2244             names[UNBOXED_RESULT] = new Name(invokeBasic, unboxArgs);
2245         }
2246 
2247         lform = LambdaForm.create(basicType.parameterCount() + 1, names, Kind.TABLE_SWITCH);
2248         LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2249         return prev != null ? prev : lform;
2250     }
2251 
2252     @Hidden
2253     static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2254         MethodHandle[] caseActions = holder.cases;
2255         MethodHandle selectedCase;
2256         if (input < 0 || input >= caseActions.length) {
2257             selectedCase = defaultCase;
2258         } else {
2259             selectedCase = caseActions[input];
2260         }
2261         return selectedCase.invokeWithArguments(args);
2262     }
2263 
2264     // type is validated, value is not
2265     static MethodHandle makeConstantReturning(Class<?> type, Object value) {
2266         var callType = MethodType.methodType(type);
2267         var basicType = BasicType.basicType(type);
2268         var form = constantForm(basicType);
2269 
2270         if (type.isPrimitive()) {
2271             assert type != void.class;
2272             var wrapper = Wrapper.forPrimitiveType(type);
2273             var v = wrapper.convert(value, type); // throws CCE
2274             return switch (wrapper) {
2275                 case INT    -> BoundMethodHandle.bindSingleI(callType, form, (int) v);
2276                 case LONG   -> BoundMethodHandle.bindSingleJ(callType, form, (long) v);
2277                 case FLOAT  -> BoundMethodHandle.bindSingleF(callType, form, (float) v);
2278                 case DOUBLE -> BoundMethodHandle.bindSingleD(callType, form, (double) v);
2279                 default -> BoundMethodHandle.bindSingleI(callType, form, ValueConversions.widenSubword(v));
2280             };
2281         }
2282 
2283         var v = type.cast(value); // throws CCE
2284         return BoundMethodHandle.bindSingleL(callType, form, v);
2285     }
2286 
2287     // Indexes into constant method handles:
2288     static final int
2289             MH_cast                  =              0,
2290             MH_selectAlternative     =              1,
2291             MH_countedLoopPred       =              2,
2292             MH_countedLoopStep       =              3,
2293             MH_initIterator          =              4,
2294             MH_iteratePred           =              5,
2295             MH_iterateNext           =              6,
2296             MH_Array_newInstance     =              7,
2297             MH_VarHandles_handleCheckedExceptions = 8,
2298             MH_LIMIT                 =              9;
2299 
2300     static MethodHandle getConstantHandle(int idx) {
2301         MethodHandle handle = HANDLES[idx];
2302         if (handle != null) {
2303             return handle;
2304         }
2305         return setCachedHandle(idx, makeConstantHandle(idx));
2306     }
2307 
2308     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2309         // Simulate a CAS, to avoid racy duplication of results.
2310         MethodHandle prev = HANDLES[idx];
2311         if (prev != null) {
2312             return prev;
2313         }
2314         HANDLES[idx] = method;
2315         return method;
2316     }
2317 
2318     // Local constant method handles:
2319     private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
2320 
2321     private static MethodHandle makeConstantHandle(int idx) {
2322         try {
2323             switch (idx) {
2324                 case MH_cast:
2325                     return IMPL_LOOKUP.findVirtual(Class.class, "cast",
2326                             MethodType.methodType(Object.class, Object.class));
2327                 case MH_selectAlternative:
2328                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2329                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2330                 case MH_countedLoopPred:
2331                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2332                             MethodType.methodType(boolean.class, int.class, int.class));
2333                 case MH_countedLoopStep:
2334                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2335                             MethodType.methodType(int.class, int.class, int.class));
2336                 case MH_initIterator:
2337                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2338                             MethodType.methodType(Iterator.class, Iterable.class));
2339                 case MH_iteratePred:
2340                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2341                             MethodType.methodType(boolean.class, Iterator.class));
2342                 case MH_iterateNext:
2343                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2344                             MethodType.methodType(Object.class, Iterator.class));
2345                 case MH_Array_newInstance:
2346                     return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2347                             MethodType.methodType(Object.class, Class.class, int.class));
2348                 case MH_VarHandles_handleCheckedExceptions:
2349                     return IMPL_LOOKUP.findStatic(VarHandles.class, "handleCheckedExceptions",
2350                             MethodType.methodType(void.class, Throwable.class));
2351             }
2352         } catch (ReflectiveOperationException ex) {
2353             throw newInternalError(ex);
2354         }
2355         throw newInternalError("Unknown function index: " + idx);
2356     }
2357 }