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