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