1 /*
   2  * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.access.JavaLangInvokeAccess;
  29 import jdk.internal.access.SharedSecrets;
  30 import jdk.internal.invoke.NativeEntryPoint;
  31 import jdk.internal.org.objectweb.asm.ClassWriter;
  32 import jdk.internal.org.objectweb.asm.MethodVisitor;
  33 import jdk.internal.reflect.CallerSensitive;
  34 import jdk.internal.reflect.Reflection;
  35 import jdk.internal.vm.annotation.ForceInline;
  36 import jdk.internal.vm.annotation.Hidden;
  37 import jdk.internal.vm.annotation.Stable;
  38 import sun.invoke.empty.Empty;
  39 import sun.invoke.util.ValueConversions;
  40 import sun.invoke.util.VerifyType;
  41 import sun.invoke.util.Wrapper;
  42 
  43 import java.lang.invoke.MethodHandles.Lookup;
  44 import java.lang.reflect.Array;
  45 import java.lang.reflect.Constructor;
  46 import java.lang.reflect.Field;
  47 import java.nio.ByteOrder;
  48 import java.util.Arrays;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.Iterator;
  52 import java.util.List;
  53 import java.util.Map;
  54 import java.util.Objects;
  55 import java.util.Set;
  56 import java.util.concurrent.ConcurrentHashMap;
  57 import java.util.function.Function;
  58 import java.util.stream.Stream;
  59 
  60 import static java.lang.invoke.LambdaForm.*;
  61 import static java.lang.invoke.MethodHandleStatics.*;
  62 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  63 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
  64 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  65 
  66 /**
  67  * Trusted implementation code for MethodHandle.
  68  * @author jrose
  69  */
  70 /*non-public*/
  71 abstract class MethodHandleImpl {
  72 
  73     /// Factory methods to create method handles:
  74 
  75     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, ArrayAccess access) {
  76         if (arrayClass == Object[].class) {
  77             return ArrayAccess.objectAccessor(access);
  78         }
  79         if (!arrayClass.isArray())
  80             throw newIllegalArgumentException("not an array: "+arrayClass);
  81         MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
  82         int cacheIndex = ArrayAccess.cacheIndex(access);
  83         MethodHandle mh = cache[cacheIndex];
  84         if (mh != null)  return mh;
  85         mh = ArrayAccessor.getAccessor(arrayClass, access);
  86         MethodType correctType = ArrayAccessor.correctType(arrayClass, access);
  87         if (mh.type() != correctType) {
  88             assert(mh.type().parameterType(0) == Object[].class);
  89             /* if access == SET */ assert(access != ArrayAccess.SET || mh.type().parameterType(2) == Object.class);
  90             /* if access == GET */ assert(access != ArrayAccess.GET ||
  91                     (mh.type().returnType() == Object.class &&
  92                      correctType.parameterType(0).getComponentType() == correctType.returnType()));
  93             // safe to view non-strictly, because element type follows from array type
  94             mh = mh.viewAsType(correctType, false);
  95         }
  96         mh = makeIntrinsic(mh, ArrayAccess.intrinsic(access));
  97         // Atomically update accessor cache.
  98         synchronized(cache) {
  99             if (cache[cacheIndex] == null) {
 100                 cache[cacheIndex] = mh;
 101             } else {
 102                 // Throw away newly constructed accessor and use cached version.
 103                 mh = cache[cacheIndex];
 104             }
 105         }
 106         return mh;
 107     }
 108 
 109     enum ArrayAccess {
 110         GET, SET, LENGTH;
 111 
 112         // As ArrayAccess and ArrayAccessor have a circular dependency, the ArrayAccess properties cannot be stored in
 113         // final fields.
 114 
 115         static String opName(ArrayAccess a) {
 116             return switch (a) {
 117                 case GET    -> "getElement";
 118                 case SET    -> "setElement";
 119                 case LENGTH -> "length";
 120                 default -> throw unmatchedArrayAccess(a);
 121             };
 122         }
 123 
 124         static MethodHandle objectAccessor(ArrayAccess a) {
 125             return switch (a) {
 126                 case GET    -> ArrayAccessor.OBJECT_ARRAY_GETTER;
 127                 case SET    -> ArrayAccessor.OBJECT_ARRAY_SETTER;
 128                 case LENGTH -> ArrayAccessor.OBJECT_ARRAY_LENGTH;
 129                 default -> throw unmatchedArrayAccess(a);
 130             };
 131         }
 132 
 133         static int cacheIndex(ArrayAccess a) {
 134             return switch (a) {
 135                 case GET    -> ArrayAccessor.GETTER_INDEX;
 136                 case SET    -> ArrayAccessor.SETTER_INDEX;
 137                 case LENGTH -> ArrayAccessor.LENGTH_INDEX;
 138                 default -> throw unmatchedArrayAccess(a);
 139             };
 140         }
 141 
 142         static Intrinsic intrinsic(ArrayAccess a) {
 143             return switch (a) {
 144                 case GET    -> Intrinsic.ARRAY_LOAD;
 145                 case SET    -> Intrinsic.ARRAY_STORE;
 146                 case LENGTH -> Intrinsic.ARRAY_LENGTH;
 147                 default -> throw unmatchedArrayAccess(a);
 148             };
 149         }
 150     }
 151 
 152     static InternalError unmatchedArrayAccess(ArrayAccess a) {
 153         return newInternalError("should not reach here (unmatched ArrayAccess: " + a + ")");
 154     }
 155 
 156     static final class ArrayAccessor {
 157         /// Support for array element and length access
 158         static final int GETTER_INDEX = 0, SETTER_INDEX = 1, LENGTH_INDEX = 2, INDEX_LIMIT = 3;
 159         static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
 160                 = new ClassValue<MethodHandle[]>() {
 161                     @Override
 162                     protected MethodHandle[] computeValue(Class<?> type) {
 163                         return new MethodHandle[INDEX_LIMIT];
 164                     }
 165                 };
 166         static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER, OBJECT_ARRAY_LENGTH;
 167         static {
 168             MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
 169             cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.GET),    Intrinsic.ARRAY_LOAD);
 170             cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.SET),    Intrinsic.ARRAY_STORE);
 171             cache[LENGTH_INDEX] = OBJECT_ARRAY_LENGTH = makeIntrinsic(getAccessor(Object[].class, ArrayAccess.LENGTH), Intrinsic.ARRAY_LENGTH);
 172 
 173             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
 174             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
 175             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_LENGTH.internalMemberName()));
 176         }
 177 
 178         static int     getElementI(int[]     a, int i)            { return              a[i]; }
 179         static long    getElementJ(long[]    a, int i)            { return              a[i]; }
 180         static float   getElementF(float[]   a, int i)            { return              a[i]; }
 181         static double  getElementD(double[]  a, int i)            { return              a[i]; }
 182         static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
 183         static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
 184         static short   getElementS(short[]   a, int i)            { return              a[i]; }
 185         static char    getElementC(char[]    a, int i)            { return              a[i]; }
 186         static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
 187 
 188         static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
 189         static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
 190         static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
 191         static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
 192         static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
 193         static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
 194         static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
 195         static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
 196         static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
 197 
 198         static int     lengthI(int[]     a)                       { return a.length; }
 199         static int     lengthJ(long[]    a)                       { return a.length; }
 200         static int     lengthF(float[]   a)                       { return a.length; }
 201         static int     lengthD(double[]  a)                       { return a.length; }
 202         static int     lengthZ(boolean[] a)                       { return a.length; }
 203         static int     lengthB(byte[]    a)                       { return a.length; }
 204         static int     lengthS(short[]   a)                       { return a.length; }
 205         static int     lengthC(char[]    a)                       { return a.length; }
 206         static int     lengthL(Object[]  a)                       { return a.length; }
 207 
 208         static String name(Class<?> arrayClass, ArrayAccess access) {
 209             Class<?> elemClass = arrayClass.getComponentType();
 210             if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
 211             return ArrayAccess.opName(access) + Wrapper.basicTypeChar(elemClass);
 212         }
 213         static MethodType type(Class<?> arrayClass, ArrayAccess access) {
 214             Class<?> elemClass = arrayClass.getComponentType();
 215             Class<?> arrayArgClass = arrayClass;
 216             if (!elemClass.isPrimitive()) {
 217                 arrayArgClass = Object[].class;
 218                 elemClass = Object.class;
 219             }
 220             return switch (access) {
 221                 case GET    -> MethodType.methodType(elemClass, arrayArgClass, int.class);
 222                 case SET    -> MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
 223                 case LENGTH -> MethodType.methodType(int.class, arrayArgClass);
 224                 default -> throw unmatchedArrayAccess(access);
 225             };
 226         }
 227         static MethodType correctType(Class<?> arrayClass, ArrayAccess access) {
 228             Class<?> elemClass = arrayClass.getComponentType();
 229             return switch (access) {
 230                 case GET    -> MethodType.methodType(elemClass, arrayClass, int.class);
 231                 case SET    -> MethodType.methodType(void.class, arrayClass, int.class, elemClass);
 232                 case LENGTH -> MethodType.methodType(int.class, arrayClass);
 233                 default -> throw unmatchedArrayAccess(access);
 234             };
 235         }
 236         static MethodHandle getAccessor(Class<?> arrayClass, ArrayAccess access) {
 237             String     name = name(arrayClass, access);
 238             MethodType type = type(arrayClass, access);
 239             try {
 240                 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
 241             } catch (ReflectiveOperationException ex) {
 242                 throw uncaughtException(ex);
 243             }
 244         }
 245     }
 246 
 247     /**
 248      * Create a JVM-level adapter method handle to conform the given method
 249      * handle to the similar newType, using only pairwise argument conversions.
 250      * For each argument, convert incoming argument to the exact type needed.
 251      * The argument conversions allowed are casting, boxing and unboxing,
 252      * integral widening or narrowing, and floating point widening or narrowing.
 253      * @param srcType required call type
 254      * @param target original method handle
 255      * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
 256      * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
 257      * @return an adapter to the original handle with the desired new type,
 258      *          or the original target if the types are already identical
 259      *          or null if the adaptation cannot be made
 260      */
 261     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 262                                             boolean strict, boolean monobox) {
 263         MethodType dstType = target.type();
 264         if (srcType == dstType)
 265             return target;
 266         return makePairwiseConvertByEditor(target, srcType, strict, monobox);
 267     }
 268 
 269     private static int countNonNull(Object[] array) {
 270         int count = 0;
 271         if (array != null) {
 272             for (Object x : array) {
 273                 if (x != null) ++count;
 274             }
 275         }
 276         return count;
 277     }
 278 
 279     static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
 280                                                     boolean strict, boolean monobox) {
 281         // In method types arguments start at index 0, while the LF
 282         // editor have the MH receiver at position 0 - adjust appropriately.
 283         final int MH_RECEIVER_OFFSET = 1;
 284         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 285         int convCount = countNonNull(convSpecs);
 286         if (convCount == 0)
 287             return target.viewAsType(srcType, strict);
 288         MethodType basicSrcType = srcType.basicType();
 289         MethodType midType = target.type().basicType();
 290         BoundMethodHandle mh = target.rebind();
 291 
 292         // Match each unique conversion to the positions at which it is to be applied
 293         var convSpecMap = new HashMap<Object, int[]>(((4 * convCount) / 3) + 1);
 294         for (int i = 0; i < convSpecs.length - MH_RECEIVER_OFFSET; i++) {
 295             Object convSpec = convSpecs[i];
 296             if (convSpec == null) continue;
 297             int[] positions = convSpecMap.get(convSpec);
 298             if (positions == null) {
 299                 positions = new int[] { i + MH_RECEIVER_OFFSET };
 300             } else {
 301                 positions = Arrays.copyOf(positions, positions.length + 1);
 302                 positions[positions.length - 1] = i + MH_RECEIVER_OFFSET;
 303             }
 304             convSpecMap.put(convSpec, positions);
 305         }
 306         for (var entry : convSpecMap.entrySet()) {
 307             Object convSpec = entry.getKey();
 308 
 309             MethodHandle fn;
 310             if (convSpec instanceof Class) {
 311                 fn = getConstantHandle(MH_cast).bindTo(convSpec);
 312             } else {
 313                 fn = (MethodHandle) convSpec;
 314             }
 315             int[] positions = entry.getValue();
 316             Class<?> newType = basicSrcType.parameterType(positions[0] - MH_RECEIVER_OFFSET);
 317             BasicType newBasicType = BasicType.basicType(newType);
 318             convCount -= positions.length;
 319             if (convCount == 0) {
 320                 midType = srcType;
 321             } else {
 322                 Class<?>[] ptypes = midType.ptypes().clone();
 323                 for (int pos : positions) {
 324                     ptypes[pos - 1] = newType;
 325                 }
 326                 midType = MethodType.makeImpl(midType.rtype(), ptypes, true);
 327             }
 328             LambdaForm form2;
 329             if (positions.length > 1) {
 330                 form2 = mh.editor().filterRepeatedArgumentForm(newBasicType, positions);
 331             } else {
 332                 form2 = mh.editor().filterArgumentForm(positions[0], newBasicType);
 333             }
 334             mh = mh.copyWithExtendL(midType, form2, fn);
 335         }
 336         Object convSpec = convSpecs[convSpecs.length - 1];
 337         if (convSpec != null) {
 338             MethodHandle fn;
 339             if (convSpec instanceof Class) {
 340                 if (convSpec == void.class)
 341                     fn = null;
 342                 else
 343                     fn = getConstantHandle(MH_cast).bindTo(convSpec);
 344             } else {
 345                 fn = (MethodHandle) convSpec;
 346             }
 347             Class<?> newType = basicSrcType.returnType();
 348             assert(--convCount == 0);
 349             midType = srcType;
 350             if (fn != null) {
 351                 mh = mh.rebind();  // rebind if too complex
 352                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
 353                 mh = mh.copyWithExtendL(midType, form2, fn);
 354             } else {
 355                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
 356                 mh = mh.copyWith(midType, form2);
 357             }
 358         }
 359         assert(convCount == 0);
 360         assert(mh.type().equals(srcType));
 361         return mh;
 362     }
 363 
 364     static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
 365                                             boolean strict, boolean monobox) {
 366         final int INARG_COUNT = srcType.parameterCount();
 367         Object[] convSpecs = null;
 368         for (int i = 0; i <= INARG_COUNT; i++) {
 369             boolean isRet = (i == INARG_COUNT);
 370             Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
 371             Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
 372             if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
 373                 if (convSpecs == null) {
 374                     convSpecs = new Object[INARG_COUNT + 1];
 375                 }
 376                 convSpecs[i] = valueConversion(src, dst, strict, monobox);
 377             }
 378         }
 379         return convSpecs;
 380     }
 381     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 382                                             boolean strict) {
 383         return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
 384     }
 385 
 386     /**
 387      * Find a conversion function from the given source to the given destination.
 388      * This conversion function will be used as a LF NamedFunction.
 389      * Return a Class object if a simple cast is needed.
 390      * Return void.class if void is involved.
 391      */
 392     static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
 393         assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
 394         if (dst == void.class)
 395             return dst;
 396         MethodHandle fn;
 397         if (src.isPrimitive()) {
 398             if (src == void.class) {
 399                 return void.class;  // caller must recognize this specially
 400             } else if (dst.isPrimitive()) {
 401                 // Examples: int->byte, byte->int, boolean->int (!strict)
 402                 fn = ValueConversions.convertPrimitive(src, dst);
 403             } else {
 404                 // Examples: int->Integer, boolean->Object, float->Number
 405                 Wrapper wsrc = Wrapper.forPrimitiveType(src);
 406                 fn = ValueConversions.boxExact(wsrc);
 407                 assert(fn.type().parameterType(0) == wsrc.primitiveType());
 408                 assert(fn.type().returnType() == wsrc.wrapperType());
 409                 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
 410                     // Corner case, such as int->Long, which will probably fail.
 411                     MethodType mt = MethodType.methodType(dst, src);
 412                     if (strict)
 413                         fn = fn.asType(mt);
 414                     else
 415                         fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
 416                 }
 417             }
 418         } else if (dst.isPrimitive()) {
 419             Wrapper wdst = Wrapper.forPrimitiveType(dst);
 420             if (monobox || src == wdst.wrapperType()) {
 421                 // Use a strongly-typed unboxer, if possible.
 422                 fn = ValueConversions.unboxExact(wdst, strict);
 423             } else {
 424                 // Examples:  Object->int, Number->int, Comparable->int, Byte->int
 425                 // must include additional conversions
 426                 // src must be examined at runtime, to detect Byte, Character, etc.
 427                 fn = (strict
 428                         ? ValueConversions.unboxWiden(wdst)
 429                         : ValueConversions.unboxCast(wdst));
 430             }
 431         } else {
 432             // Simple reference conversion.
 433             // Note:  Do not check for a class hierarchy relation
 434             // between src and dst.  In all cases a 'null' argument
 435             // will pass the cast conversion.
 436             return dst;
 437         }
 438         assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
 439         return fn;
 440     }
 441 
 442     static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
 443         MethodType type = target.type();
 444         int last = type.parameterCount() - 1;
 445         if (type.parameterType(last) != arrayType)
 446             target = target.asType(type.changeParameterType(last, arrayType));
 447         target = target.asFixedArity();  // make sure this attribute is turned off
 448         return new AsVarargsCollector(target, arrayType);
 449     }
 450 
 451     private static final class AsVarargsCollector extends DelegatingMethodHandle {
 452         private final MethodHandle target;
 453         private final Class<?> arrayType;
 454         private @Stable MethodHandle asCollectorCache;
 455 
 456         AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
 457             this(target.type(), target, arrayType);
 458         }
 459         AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
 460             super(type, target);
 461             this.target = target;
 462             this.arrayType = arrayType;
 463         }
 464 
 465         @Override
 466         public boolean isVarargsCollector() {
 467             return true;
 468         }
 469 
 470         @Override
 471         protected MethodHandle getTarget() {
 472             return target;
 473         }
 474 
 475         @Override
 476         public MethodHandle asFixedArity() {
 477             return target;
 478         }
 479 
 480         @Override
 481         MethodHandle setVarargs(MemberName member) {
 482             if (member.isVarargs())  return this;
 483             return asFixedArity();
 484         }
 485 
 486         @Override
 487         public MethodHandle withVarargs(boolean makeVarargs) {
 488             if (makeVarargs)  return this;
 489             return asFixedArity();
 490         }
 491 
 492         @Override
 493         public MethodHandle asTypeUncached(MethodType newType) {
 494             MethodType type = this.type();
 495             int collectArg = type.parameterCount() - 1;
 496             int newArity = newType.parameterCount();
 497             if (newArity == collectArg+1 &&
 498                 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
 499                 // if arity and trailing parameter are compatible, do normal thing
 500                 return asFixedArity().asType(newType);
 501             }
 502             // check cache
 503             MethodHandle acc = asCollectorCache;
 504             if (acc != null && acc.type().parameterCount() == newArity)
 505                 return acc.asType(newType);
 506             // build and cache a collector
 507             int arrayLength = newArity - collectArg;
 508             MethodHandle collector;
 509             try {
 510                 collector = asFixedArity().asCollector(arrayType, arrayLength);
 511                 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
 512             } catch (IllegalArgumentException ex) {
 513                 throw new WrongMethodTypeException("cannot build collector", ex);
 514             }
 515             asCollectorCache = collector;
 516             return collector.asType(newType);
 517         }
 518 
 519         @Override
 520         boolean viewAsTypeChecks(MethodType newType, boolean strict) {
 521             super.viewAsTypeChecks(newType, true);
 522             if (strict) return true;
 523             // extra assertion for non-strict checks:
 524             assert (type().lastParameterType().getComponentType()
 525                     .isAssignableFrom(
 526                             newType.lastParameterType().getComponentType()))
 527                     : Arrays.asList(this, newType);
 528             return true;
 529         }
 530 
 531         @Override
 532         public Object invokeWithArguments(Object... arguments) throws Throwable {
 533             MethodType type = this.type();
 534             int argc;
 535             final int MAX_SAFE = 127;  // 127 longs require 254 slots, which is safe to spread
 536             if (arguments == null
 537                     || (argc = arguments.length) <= MAX_SAFE
 538                     || argc < type.parameterCount()) {
 539                 return super.invokeWithArguments(arguments);
 540             }
 541 
 542             // a jumbo invocation requires more explicit reboxing of the trailing arguments
 543             int uncollected = type.parameterCount() - 1;
 544             Class<?> elemType = arrayType.getComponentType();
 545             int collected = argc - uncollected;
 546             Object collArgs = (elemType == Object.class)
 547                 ? new Object[collected] : Array.newInstance(elemType, collected);
 548             if (!elemType.isPrimitive()) {
 549                 // simple cast:  just do some casting
 550                 try {
 551                     System.arraycopy(arguments, uncollected, collArgs, 0, collected);
 552                 } catch (ArrayStoreException ex) {
 553                     return super.invokeWithArguments(arguments);
 554                 }
 555             } else {
 556                 // corner case of flat array requires reflection (or specialized copy loop)
 557                 MethodHandle arraySetter = MethodHandles.arrayElementSetter(arrayType);
 558                 try {
 559                     for (int i = 0; i < collected; i++) {
 560                         arraySetter.invoke(collArgs, i, arguments[uncollected + i]);
 561                     }
 562                 } catch (WrongMethodTypeException|ClassCastException ex) {
 563                     return super.invokeWithArguments(arguments);
 564                 }
 565             }
 566 
 567             // chop the jumbo list down to size and call in non-varargs mode
 568             Object[] newArgs = new Object[uncollected + 1];
 569             System.arraycopy(arguments, 0, newArgs, 0, uncollected);
 570             newArgs[uncollected] = collArgs;
 571             return asFixedArity().invokeWithArguments(newArgs);
 572         }
 573     }
 574 
 575     static void checkSpreadArgument(Object av, int n) {
 576         if (av == null && n == 0) {
 577             return;
 578         } else if (av == null) {
 579             throw new NullPointerException("null array reference");
 580         } else if (av instanceof Object[]) {
 581             int len = ((Object[])av).length;
 582             if (len == n)  return;
 583         } else {
 584             int len = java.lang.reflect.Array.getLength(av);
 585             if (len == n)  return;
 586         }
 587         // fall through to error:
 588         throw newIllegalArgumentException("array is not of length "+n);
 589     }
 590 
 591     @Hidden
 592     static MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
 593         if (testResult) {
 594             return target;
 595         } else {
 596             return fallback;
 597         }
 598     }
 599 
 600     // Intrinsified by C2. Counters are used during parsing to calculate branch frequencies.
 601     @Hidden
 602     @jdk.internal.vm.annotation.IntrinsicCandidate
 603     static boolean profileBoolean(boolean result, int[] counters) {
 604         // Profile is int[2] where [0] and [1] correspond to false and true occurrences respectively.
 605         int idx = result ? 1 : 0;
 606         try {
 607             counters[idx] = Math.addExact(counters[idx], 1);
 608         } catch (ArithmeticException e) {
 609             // Avoid continuous overflow by halving the problematic count.
 610             counters[idx] = counters[idx] / 2;
 611         }
 612         return result;
 613     }
 614 
 615     // Intrinsified by C2. Returns true if obj is a compile-time constant.
 616     @Hidden
 617     @jdk.internal.vm.annotation.IntrinsicCandidate
 618     static boolean isCompileConstant(Object obj) {
 619         return false;
 620     }
 621 
 622     static MethodHandle makeGuardWithTest(MethodHandle test,
 623                                    MethodHandle target,
 624                                    MethodHandle fallback) {
 625         MethodType type = target.type();
 626         assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
 627         MethodType basicType = type.basicType();
 628         LambdaForm form = makeGuardWithTestForm(basicType);
 629         BoundMethodHandle mh;
 630         try {
 631             if (PROFILE_GWT) {
 632                 int[] counts = new int[2];
 633                 mh = (BoundMethodHandle)
 634                         BoundMethodHandle.speciesData_LLLL().factory().invokeBasic(type, form,
 635                                 (Object) test, (Object) profile(target), (Object) profile(fallback), counts);
 636             } else {
 637                 mh = (BoundMethodHandle)
 638                         BoundMethodHandle.speciesData_LLL().factory().invokeBasic(type, form,
 639                                 (Object) test, (Object) profile(target), (Object) profile(fallback));
 640             }
 641         } catch (Throwable ex) {
 642             throw uncaughtException(ex);
 643         }
 644         assert(mh.type() == type);
 645         return mh;
 646     }
 647 
 648 
 649     static MethodHandle profile(MethodHandle target) {
 650         if (DONT_INLINE_THRESHOLD >= 0) {
 651             return makeBlockInliningWrapper(target);
 652         } else {
 653             return target;
 654         }
 655     }
 656 
 657     /**
 658      * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
 659      * Corresponding LambdaForm has @DontInline when compiled into bytecode.
 660      */
 661     static MethodHandle makeBlockInliningWrapper(MethodHandle target) {
 662         LambdaForm lform;
 663         if (DONT_INLINE_THRESHOLD > 0) {
 664             lform = Makers.PRODUCE_BLOCK_INLINING_FORM.apply(target);
 665         } else {
 666             lform = Makers.PRODUCE_REINVOKER_FORM.apply(target);
 667         }
 668         return new CountingWrapper(target, lform,
 669                 Makers.PRODUCE_BLOCK_INLINING_FORM, Makers.PRODUCE_REINVOKER_FORM,
 670                                    DONT_INLINE_THRESHOLD);
 671     }
 672 
 673     private static final class Makers {
 674         /** Constructs reinvoker lambda form which block inlining during JIT-compilation for a particular method handle */
 675         static final Function<MethodHandle, LambdaForm> PRODUCE_BLOCK_INLINING_FORM = new Function<MethodHandle, LambdaForm>() {
 676             @Override
 677             public LambdaForm apply(MethodHandle target) {
 678                 return DelegatingMethodHandle.makeReinvokerForm(target,
 679                                    MethodTypeForm.LF_DELEGATE_BLOCK_INLINING, CountingWrapper.class, false,
 680                                    DelegatingMethodHandle.NF_getTarget, CountingWrapper.NF_maybeStopCounting);
 681             }
 682         };
 683 
 684         /** Constructs simple reinvoker lambda form for a particular method handle */
 685         static final Function<MethodHandle, LambdaForm> PRODUCE_REINVOKER_FORM = new Function<MethodHandle, LambdaForm>() {
 686             @Override
 687             public LambdaForm apply(MethodHandle target) {
 688                 return DelegatingMethodHandle.makeReinvokerForm(target,
 689                         MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, DelegatingMethodHandle.NF_getTarget);
 690             }
 691         };
 692 
 693         /** Maker of type-polymorphic varargs */
 694         static final ClassValue<MethodHandle[]> TYPED_COLLECTORS = new ClassValue<MethodHandle[]>() {
 695             @Override
 696             protected MethodHandle[] computeValue(Class<?> type) {
 697                 return new MethodHandle[MAX_JVM_ARITY + 1];
 698             }
 699         };
 700     }
 701 
 702     /**
 703      * Counting method handle. It has 2 states: counting and non-counting.
 704      * It is in counting state for the first n invocations and then transitions to non-counting state.
 705      * Behavior in counting and non-counting states is determined by lambda forms produced by
 706      * countingFormProducer & nonCountingFormProducer respectively.
 707      */
 708     static class CountingWrapper extends DelegatingMethodHandle {
 709         private final MethodHandle target;
 710         private int count;
 711         private Function<MethodHandle, LambdaForm> countingFormProducer;
 712         private Function<MethodHandle, LambdaForm> nonCountingFormProducer;
 713         private volatile boolean isCounting;
 714 
 715         private CountingWrapper(MethodHandle target, LambdaForm lform,
 716                                 Function<MethodHandle, LambdaForm> countingFromProducer,
 717                                 Function<MethodHandle, LambdaForm> nonCountingFormProducer,
 718                                 int count) {
 719             super(target.type(), lform);
 720             this.target = target;
 721             this.count = count;
 722             this.countingFormProducer = countingFromProducer;
 723             this.nonCountingFormProducer = nonCountingFormProducer;
 724             this.isCounting = (count > 0);
 725         }
 726 
 727         @Hidden
 728         @Override
 729         protected MethodHandle getTarget() {
 730             return target;
 731         }
 732 
 733         @Override
 734         public MethodHandle asTypeUncached(MethodType newType) {
 735             MethodHandle newTarget = target.asType(newType);
 736             MethodHandle wrapper;
 737             if (isCounting) {
 738                 LambdaForm lform;
 739                 lform = countingFormProducer.apply(newTarget);
 740                 wrapper = new CountingWrapper(newTarget, lform, countingFormProducer, nonCountingFormProducer, DONT_INLINE_THRESHOLD);
 741             } else {
 742                 wrapper = newTarget; // no need for a counting wrapper anymore
 743             }
 744             return wrapper;
 745         }
 746 
 747         boolean countDown() {
 748             int c = count;
 749             target.maybeCustomize(); // customize if counting happens for too long
 750             if (c <= 1) {
 751                 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
 752                 if (isCounting) {
 753                     isCounting = false;
 754                     return true;
 755                 } else {
 756                     return false;
 757                 }
 758             } else {
 759                 count = c - 1;
 760                 return false;
 761             }
 762         }
 763 
 764         @Hidden
 765         static void maybeStopCounting(Object o1) {
 766              final CountingWrapper wrapper = (CountingWrapper) o1;
 767              if (wrapper.countDown()) {
 768                  // Reached invocation threshold. Replace counting behavior with a non-counting one.
 769                  wrapper.updateForm(new Function<>() {
 770                      public LambdaForm apply(LambdaForm oldForm) {
 771                          LambdaForm lform = wrapper.nonCountingFormProducer.apply(wrapper.target);
 772                          lform.compileToBytecode(); // speed up warmup by avoiding LF interpretation again after transition
 773                          return lform;
 774                      }});
 775              }
 776         }
 777 
 778         static final NamedFunction NF_maybeStopCounting;
 779         static {
 780             Class<?> THIS_CLASS = CountingWrapper.class;
 781             try {
 782                 NF_maybeStopCounting = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeStopCounting", Object.class));
 783             } catch (ReflectiveOperationException ex) {
 784                 throw newInternalError(ex);
 785             }
 786         }
 787     }
 788 
 789     static LambdaForm makeGuardWithTestForm(MethodType basicType) {
 790         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
 791         if (lform != null)  return lform;
 792         final int THIS_MH      = 0;  // the BMH_LLL
 793         final int ARG_BASE     = 1;  // start of incoming arguments
 794         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 795         int nameCursor = ARG_LIMIT;
 796         final int GET_TEST     = nameCursor++;
 797         final int GET_TARGET   = nameCursor++;
 798         final int GET_FALLBACK = nameCursor++;
 799         final int GET_COUNTERS = PROFILE_GWT ? nameCursor++ : -1;
 800         final int CALL_TEST    = nameCursor++;
 801         final int PROFILE      = (GET_COUNTERS != -1) ? nameCursor++ : -1;
 802         final int TEST         = nameCursor-1; // previous statement: either PROFILE or CALL_TEST
 803         final int SELECT_ALT   = nameCursor++;
 804         final int CALL_TARGET  = nameCursor++;
 805         assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
 806 
 807         MethodType lambdaType = basicType.invokerType();
 808         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 809 
 810         BoundMethodHandle.SpeciesData data =
 811                 (GET_COUNTERS != -1) ? BoundMethodHandle.speciesData_LLLL()
 812                                      : BoundMethodHandle.speciesData_LLL();
 813         names[THIS_MH] = names[THIS_MH].withConstraint(data);
 814         names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
 815         names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
 816         names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
 817         if (GET_COUNTERS != -1) {
 818             names[GET_COUNTERS] = new Name(data.getterFunction(3), names[THIS_MH]);
 819         }
 820         Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
 821 
 822         // call test
 823         MethodType testType = basicType.changeReturnType(boolean.class).basicType();
 824         invokeArgs[0] = names[GET_TEST];
 825         names[CALL_TEST] = new Name(testType, invokeArgs);
 826 
 827         // profile branch
 828         if (PROFILE != -1) {
 829             names[PROFILE] = new Name(getFunction(NF_profileBoolean), names[CALL_TEST], names[GET_COUNTERS]);
 830         }
 831         // call selectAlternative
 832         names[SELECT_ALT] = new Name(new NamedFunction(
 833                 makeIntrinsic(getConstantHandle(MH_selectAlternative), Intrinsic.SELECT_ALTERNATIVE)),
 834                 names[TEST], names[GET_TARGET], names[GET_FALLBACK]);
 835 
 836         // call target or fallback
 837         invokeArgs[0] = names[SELECT_ALT];
 838         names[CALL_TARGET] = new Name(basicType, invokeArgs);
 839 
 840         lform = new LambdaForm(lambdaType.parameterCount(), names, /*forceInline=*/true, Kind.GUARD);
 841 
 842         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
 843     }
 844 
 845     /**
 846      * The LambdaForm shape for catchException combinator is the following:
 847      * <blockquote><pre>{@code
 848      *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
 849      *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
 850      *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
 851      *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
 852      *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
 853      *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
 854      *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
 855      *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
 856      *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
 857      * }</pre></blockquote>
 858      *
 859      * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
 860      * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
 861      * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
 862      *
 863      * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
 864      * among catchException combinators with the same basic type.
 865      */
 866     private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
 867         MethodType lambdaType = basicType.invokerType();
 868 
 869         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
 870         if (lform != null) {
 871             return lform;
 872         }
 873         final int THIS_MH      = 0;  // the BMH_LLLLL
 874         final int ARG_BASE     = 1;  // start of incoming arguments
 875         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 876 
 877         int nameCursor = ARG_LIMIT;
 878         final int GET_TARGET       = nameCursor++;
 879         final int GET_CLASS        = nameCursor++;
 880         final int GET_CATCHER      = nameCursor++;
 881         final int GET_COLLECT_ARGS = nameCursor++;
 882         final int GET_UNBOX_RESULT = nameCursor++;
 883         final int BOXED_ARGS       = nameCursor++;
 884         final int TRY_CATCH        = nameCursor++;
 885         final int UNBOX_RESULT     = nameCursor++;
 886 
 887         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 888 
 889         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 890         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
 891         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
 892         names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
 893         names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
 894         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
 895         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
 896 
 897         // FIXME: rework argument boxing/result unboxing logic for LF interpretation
 898 
 899         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
 900         MethodType collectArgsType = basicType.changeReturnType(Object.class);
 901         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
 902         Object[] args = new Object[invokeBasic.type().parameterCount()];
 903         args[0] = names[GET_COLLECT_ARGS];
 904         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
 905         names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH)), args);
 906 
 907         // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
 908         Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
 909         names[TRY_CATCH] = new Name(getFunction(NF_guardWithCatch), gwcArgs);
 910 
 911         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
 912         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
 913         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
 914         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
 915 
 916         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.GUARD_WITH_CATCH);
 917 
 918         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
 919     }
 920 
 921     static MethodHandle makeGuardWithCatch(MethodHandle target,
 922                                     Class<? extends Throwable> exType,
 923                                     MethodHandle catcher) {
 924         MethodType type = target.type();
 925         LambdaForm form = makeGuardWithCatchForm(type.basicType());
 926 
 927         // Prepare auxiliary method handles used during LambdaForm interpretation.
 928         // Box arguments and wrap them into Object[]: ValueConversions.array().
 929         MethodType varargsType = type.changeReturnType(Object[].class);
 930         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
 931         MethodHandle unboxResult = unboxResultHandle(type.returnType());
 932 
 933         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 934         BoundMethodHandle mh;
 935         try {
 936             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) exType,
 937                     (Object) catcher, (Object) collectArgs, (Object) unboxResult);
 938         } catch (Throwable ex) {
 939             throw uncaughtException(ex);
 940         }
 941         assert(mh.type() == type);
 942         return mh;
 943     }
 944 
 945     /**
 946      * Intrinsified during LambdaForm compilation
 947      * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
 948      */
 949     @Hidden
 950     static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
 951                                  Object... av) throws Throwable {
 952         // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
 953         try {
 954             return target.asFixedArity().invokeWithArguments(av);
 955         } catch (Throwable t) {
 956             if (!exType.isInstance(t)) throw t;
 957             return catcher.asFixedArity().invokeWithArguments(prepend(av, t));
 958         }
 959     }
 960 
 961     /** Prepend elements to an array. */
 962     @Hidden
 963     private static Object[] prepend(Object[] array, Object... elems) {
 964         int nArray = array.length;
 965         int nElems = elems.length;
 966         Object[] newArray = new Object[nArray + nElems];
 967         System.arraycopy(elems, 0, newArray, 0, nElems);
 968         System.arraycopy(array, 0, newArray, nElems, nArray);
 969         return newArray;
 970     }
 971 
 972     static MethodHandle throwException(MethodType type) {
 973         assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
 974         int arity = type.parameterCount();
 975         if (arity > 1) {
 976             MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
 977             mh = MethodHandles.dropArguments(mh, 1, Arrays.copyOfRange(type.parameterArray(), 1, arity));
 978             return mh;
 979         }
 980         return makePairwiseConvert(getFunction(NF_throwException).resolvedHandle(), type, false, true);
 981     }
 982 
 983     static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
 984 
 985     static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
 986     static MethodHandle fakeMethodHandleInvoke(MemberName method) {
 987         assert(method.isMethodHandleInvoke());
 988         int idx = switch (method.getName()) {
 989             case "invoke"      -> 0;
 990             case "invokeExact" -> 1;
 991             default -> throw new InternalError(method.getName());
 992         };
 993         MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
 994         if (mh != null)  return mh;
 995         MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
 996                                                 MethodHandle.class, Object[].class);
 997         mh = throwException(type);
 998         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
 999         if (!method.getInvocationType().equals(mh.type()))
1000             throw new InternalError(method.toString());
1001         mh = mh.withInternalMemberName(method, false);
1002         mh = mh.withVarargs(true);
1003         assert(method.isVarargs());
1004         FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
1005         return mh;
1006     }
1007     static MethodHandle fakeVarHandleInvoke(MemberName method) {
1008         // TODO caching, is it necessary?
1009         MethodType type = MethodType.methodType(method.getReturnType(), UnsupportedOperationException.class,
1010                                                 VarHandle.class, Object[].class);
1011         MethodHandle mh = throwException(type);
1012         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke VarHandle"));
1013         if (!method.getInvocationType().equals(mh.type()))
1014             throw new InternalError(method.toString());
1015         mh = mh.withInternalMemberName(method, false);
1016         mh = mh.asVarargsCollector(Object[].class);
1017         assert(method.isVarargs());
1018         return mh;
1019     }
1020 
1021     /**
1022      * Create an alias for the method handle which, when called,
1023      * appears to be called from the same class loader and protection domain
1024      * as hostClass.
1025      * This is an expensive no-op unless the method which is called
1026      * is sensitive to its caller.  A small number of system methods
1027      * are in this category, including Class.forName and Method.invoke.
1028      */
1029     static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1030         return BindCaller.bindCaller(mh, hostClass);
1031     }
1032 
1033     // Put the whole mess into its own nested class.
1034     // That way we can lazily load the code and set up the constants.
1035     private static class BindCaller {
1036         private static MethodType INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1037         private static MethodType REFLECT_INVOKER_MT = MethodType.methodType(Object.class, MethodHandle.class, Object.class, Object[].class);
1038 
1039         static MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1040             // Code in the boot layer should now be careful while creating method handles or
1041             // functional interface instances created from method references to @CallerSensitive  methods,
1042             // it needs to be ensured the handles or interface instances are kept safe and are not passed
1043             // from the boot layer to untrusted code.
1044             if (hostClass == null
1045                 ||    (hostClass.isArray() ||
1046                        hostClass.isPrimitive() ||
1047                        hostClass.getName().startsWith("java.lang.invoke."))) {
1048                 throw new InternalError();  // does not happen, and should not anyway
1049             }
1050 
1051             MemberName member = mh.internalMemberName();
1052             if (member != null) {
1053                 // Look up the CSM adapter method with the same method name
1054                 // but with an additional caller class parameter.  If present,
1055                 // bind the adapter's method handle with the lookup class as
1056                 // the caller class argument
1057                 MemberName csmAdapter = IMPL_LOOKUP.resolveOrNull(member.getReferenceKind(),
1058                         new MemberName(member.getDeclaringClass(),
1059                                        member.getName(),
1060                                        member.getMethodType().appendParameterTypes(Class.class),
1061                                        member.getReferenceKind()));
1062                 if (csmAdapter != null) {
1063                     assert !csmAdapter.isCallerSensitive();
1064                     MethodHandle dmh = DirectMethodHandle.make(csmAdapter);
1065                     dmh = MethodHandles.insertArguments(dmh, dmh.type().parameterCount() - 1, hostClass);
1066                     dmh = new WrappedMember(dmh, mh.type(), member, mh.isInvokeSpecial(), hostClass);
1067                     return dmh;
1068                 }
1069             }
1070 
1071             // If no adapter method for CSM with an additional Class parameter
1072             // is present, then inject an invoker class that is the caller
1073             // invoking the method handle of the CSM
1074             try {
1075                 return bindCallerWithInjectedInvoker(mh, hostClass);
1076             } catch (ReflectiveOperationException ex) {
1077                 throw uncaughtException(ex);
1078             }
1079         }
1080 
1081         private static MethodHandle bindCallerWithInjectedInvoker(MethodHandle mh, Class<?> hostClass)
1082                 throws ReflectiveOperationException
1083         {
1084             // For simplicity, convert mh to a varargs-like method.
1085             MethodHandle vamh = prepareForInvoker(mh);
1086             // Cache the result of makeInjectedInvoker once per argument class.
1087             MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass).invoker();
1088             return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
1089         }
1090 
1091         private static Class<?> makeInjectedInvoker(Class<?> targetClass) {

1092                 /*
1093                  * The invoker class defined to the same class loader as the lookup class
1094                  * but in an unnamed package so that the class bytes can be cached and
1095                  * reused for any @CSM.
1096                  *
1097                  * @CSM must be public and exported if called by any module.
1098                  */
1099                 String name = targetClass.getName() + "$$InjectedInvoker";
1100                 if (targetClass.isHidden()) {
1101                     // use the original class name
1102                     name = name.replace('/', '_');
1103                 }
1104                 Class<?> invokerClass = new Lookup(targetClass)
1105                         .makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE, Set.of(NESTMATE))
1106                         .defineClass(true, targetClass);
1107                 assert checkInjectedInvoker(targetClass, invokerClass);
1108                 return invokerClass;



1109         }
1110 
1111         private static ClassValue<InjectedInvokerHolder> CV_makeInjectedInvoker = new ClassValue<>() {
1112             @Override
1113             protected InjectedInvokerHolder computeValue(Class<?> hostClass) {
1114                 return new InjectedInvokerHolder(makeInjectedInvoker(hostClass));
1115             }
1116         };
1117 
1118         /*
1119          * Returns a method handle of an invoker class injected for reflection
1120          * implementation use with the following signature:
1121          *     reflect_invoke_V(MethodHandle mh, Object target, Object[] args)
1122          *
1123          * Method::invoke on a caller-sensitive method will call
1124          * MethodAccessorImpl::invoke(Object, Object[]) through reflect_invoke_V
1125          *     target.csm(args)
1126          *     NativeMethodAccesssorImpl::invoke(target, args)
1127          *     MethodAccessImpl::invoke(target, args)
1128          *     InjectedInvoker::reflect_invoke_V(vamh, target, args);
1129          *     method::invoke(target, args)
1130          *     p.Foo::m
1131          *
1132          * An injected invoker class is a hidden class which has the same
1133          * defining class loader, runtime package, and protection domain
1134          * as the given caller class.
1135          */
1136         static MethodHandle reflectiveInvoker(Class<?> caller) {
1137             return BindCaller.CV_makeInjectedInvoker.get(caller).reflectInvoker();
1138         }
1139 
1140         private static final class InjectedInvokerHolder {
1141             private final Class<?> invokerClass;
1142             // lazily resolved and cached DMH(s) of invoke_V methods
1143             private MethodHandle invoker;
1144             private MethodHandle reflectInvoker;
1145 
1146             private InjectedInvokerHolder(Class<?> invokerClass) {
1147                 this.invokerClass = invokerClass;
1148             }
1149 
1150             private MethodHandle invoker() {
1151                 var mh = invoker;
1152                 if (mh == null) {
1153                     try {
1154                         invoker = mh = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1155                     } catch (Error | RuntimeException ex) {
1156                         throw ex;
1157                     } catch (Throwable ex) {
1158                         throw new InternalError(ex);
1159                     }
1160                 }
1161                 return mh;
1162             }
1163 
1164             private MethodHandle reflectInvoker() {
1165                 var mh = reflectInvoker;
1166                 if (mh == null) {
1167                     try {
1168                         reflectInvoker = mh = IMPL_LOOKUP.findStatic(invokerClass, "reflect_invoke_V", REFLECT_INVOKER_MT);
1169                     } catch (Error | RuntimeException ex) {
1170                         throw ex;
1171                     } catch (Throwable ex) {
1172                         throw new InternalError(ex);
1173                     }
1174                 }
1175                 return mh;
1176             }
1177         }
1178 
1179         // Adapt mh so that it can be called directly from an injected invoker:
1180         private static MethodHandle prepareForInvoker(MethodHandle mh) {
1181             mh = mh.asFixedArity();
1182             MethodType mt = mh.type();
1183             int arity = mt.parameterCount();
1184             MethodHandle vamh = mh.asType(mt.generic());
1185             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1186             vamh = vamh.asSpreader(Object[].class, arity);
1187             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1188             return vamh;
1189         }
1190 
1191         // Undo the adapter effect of prepareForInvoker:
1192         private static MethodHandle restoreToType(MethodHandle vamh,
1193                                                   MethodHandle original,
1194                                                   Class<?> hostClass) {
1195             MethodType type = original.type();
1196             MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
1197             MemberName member = original.internalMemberName();
1198             mh = mh.asType(type);
1199             mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
1200             return mh;
1201         }
1202 
1203         private static boolean checkInjectedInvoker(Class<?> hostClass, Class<?> invokerClass) {
1204             assert (hostClass.getClassLoader() == invokerClass.getClassLoader()) : hostClass.getName()+" (CL)";
1205             try {
1206                 assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
1207             } catch (SecurityException ex) {
1208                 // Self-check was blocked by security manager. This is OK.
1209             }
1210             try {
1211                 // Test the invoker to ensure that it really injects into the right place.
1212                 MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
1213                 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
1214                 return (boolean)invoker.invoke(vamh, new Object[]{ invokerClass });
1215             } catch (Error|RuntimeException ex) {
1216                 throw ex;
1217             } catch (Throwable ex) {
1218                 throw new InternalError(ex);
1219             }
1220         }
1221 
1222         private static final MethodHandle MH_checkCallerClass;
1223         static {
1224             final Class<?> THIS_CLASS = BindCaller.class;
1225             assert(checkCallerClass(THIS_CLASS));
1226             try {
1227                 MH_checkCallerClass = IMPL_LOOKUP
1228                     .findStatic(THIS_CLASS, "checkCallerClass",
1229                                 MethodType.methodType(boolean.class, Class.class));
1230                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS));
1231             } catch (Throwable ex) {
1232                 throw new InternalError(ex);
1233             }
1234         }
1235 
1236         @CallerSensitive
1237         @ForceInline // to ensure Reflection.getCallerClass optimization
1238         private static boolean checkCallerClass(Class<?> expected) {
1239             // This method is called via MH_checkCallerClass and so it's correct to ask for the immediate caller here.
1240             Class<?> actual = Reflection.getCallerClass();
1241             if (actual != expected)
1242                 throw new InternalError("found " + actual.getName() + ", expected " + expected.getName());
1243             return true;
1244         }
1245 
1246         private static final byte[] INJECTED_INVOKER_TEMPLATE = generateInvokerTemplate();
1247 
1248         /** Produces byte code for a class that is used as an injected invoker. */
1249         private static byte[] generateInvokerTemplate() {
1250             ClassWriter cw = new ClassWriter(0);
1251 
1252             // private static class InjectedInvoker {
1253             //     /* this is used to wrap DMH(s) of caller-sensitive methods */
1254             //     @Hidden
1255             //     static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
1256             //        return vamh.invokeExact(args);
1257             //     }
1258             //     /* this is used in caller-sensitive reflective method accessor */
1259             //     @Hidden
1260             //     static Object reflect_invoke_V(MethodHandle vamh, Object target, Object[] args) throws Throwable {
1261             //        return vamh.invokeExact(target, args);
1262             //     }
1263             // }
1264             // }
1265             cw.visit(CLASSFILE_VERSION, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);
1266             {
1267                 var mv = cw.visitMethod(ACC_STATIC, "invoke_V",
1268                         "(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
1269                         null, null);
1270 
1271                 mv.visitCode();
1272                 mv.visitVarInsn(ALOAD, 0);
1273                 mv.visitVarInsn(ALOAD, 1);
1274                 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
1275                         "([Ljava/lang/Object;)Ljava/lang/Object;", false);
1276                 mv.visitInsn(ARETURN);
1277                 mv.visitMaxs(2, 2);
1278                 mv.visitEnd();
1279 
1280                 cw.visitEnd();
1281             }
1282 
1283             {
1284                 var mv = cw.visitMethod(ACC_STATIC, "reflect_invoke_V",
1285                         "(Ljava/lang/invoke/MethodHandle;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;",
1286                         null, null);
1287                 mv.visitCode();
1288                 mv.visitVarInsn(ALOAD, 0);
1289                 mv.visitVarInsn(ALOAD, 1);
1290                 mv.visitVarInsn(ALOAD, 2);
1291                 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
1292                         "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
1293                 mv.visitInsn(ARETURN);
1294                 mv.visitMaxs(3, 3);
1295                 mv.visitEnd();
1296             }
1297             return cw.toByteArray();
1298         }
1299     }
1300 
1301     /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
1302     private static final class WrappedMember extends DelegatingMethodHandle {
1303         private final MethodHandle target;
1304         private final MemberName member;
1305         private final Class<?> callerClass;
1306         private final boolean isInvokeSpecial;
1307 
1308         private WrappedMember(MethodHandle target, MethodType type,
1309                               MemberName member, boolean isInvokeSpecial,
1310                               Class<?> callerClass) {
1311             super(type, target);
1312             this.target = target;
1313             this.member = member;
1314             this.callerClass = callerClass;
1315             this.isInvokeSpecial = isInvokeSpecial;
1316         }
1317 
1318         @Override
1319         MemberName internalMemberName() {
1320             return member;
1321         }
1322         @Override
1323         Class<?> internalCallerClass() {
1324             return callerClass;
1325         }
1326         @Override
1327         boolean isInvokeSpecial() {
1328             return isInvokeSpecial;
1329         }
1330         @Override
1331         protected MethodHandle getTarget() {
1332             return target;
1333         }
1334         @Override
1335         public MethodHandle asTypeUncached(MethodType newType) {
1336             // This MH is an alias for target, except for the MemberName
1337             // Drop the MemberName if there is any conversion.
1338             return target.asType(newType);
1339         }
1340     }
1341 
1342     static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1343         if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
1344             return target;
1345         return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
1346     }
1347 
1348     /** Intrinsic IDs */
1349     /*non-public*/
1350     enum Intrinsic {
1351         SELECT_ALTERNATIVE,
1352         GUARD_WITH_CATCH,
1353         TRY_FINALLY,
1354         TABLE_SWITCH,
1355         LOOP,
1356         ARRAY_LOAD,
1357         ARRAY_STORE,
1358         ARRAY_LENGTH,
1359         IDENTITY,
1360         ZERO,
1361         NONE // no intrinsic associated
1362     }
1363 
1364     /** Mark arbitrary method handle as intrinsic.
1365      * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
1366     static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
1367         private final MethodHandle target;
1368         private final Intrinsic intrinsicName;
1369         private final Object intrinsicData;
1370 
1371         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
1372            this(target, intrinsicName, null);
1373         }
1374 
1375         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1376             super(target.type(), target);
1377             this.target = target;
1378             this.intrinsicName = intrinsicName;
1379             this.intrinsicData = intrinsicData;
1380         }
1381 
1382         @Override
1383         protected MethodHandle getTarget() {
1384             return target;
1385         }
1386 
1387         @Override
1388         Intrinsic intrinsicName() {
1389             return intrinsicName;
1390         }
1391 
1392         @Override
1393         Object intrinsicData() {
1394             return intrinsicData;
1395         }
1396 
1397         @Override
1398         public MethodHandle asTypeUncached(MethodType newType) {
1399             // This MH is an alias for target, except for the intrinsic name
1400             // Drop the name if there is any conversion.
1401             return target.asType(newType);
1402         }
1403 
1404         @Override
1405         String internalProperties() {
1406             return super.internalProperties() +
1407                     "\n& Intrinsic="+intrinsicName;
1408         }
1409 
1410         @Override
1411         public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
1412             if (intrinsicName == Intrinsic.IDENTITY) {
1413                 MethodType resultType = type().asCollectorType(arrayType, type().parameterCount() - 1, arrayLength);
1414                 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
1415                 return newArray.asType(resultType);
1416             }
1417             return super.asCollector(arrayType, arrayLength);
1418         }
1419     }
1420 
1421     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
1422         return makeIntrinsic(target, intrinsicName, null);
1423     }
1424 
1425     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName, Object intrinsicData) {
1426         if (intrinsicName == target.intrinsicName())
1427             return target;
1428         return new IntrinsicMethodHandle(target, intrinsicName, intrinsicData);
1429     }
1430 
1431     static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
1432         return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
1433     }
1434 
1435     private static final @Stable MethodHandle[] ARRAYS = new MethodHandle[MAX_ARITY + 1];
1436 
1437     /** Return a method handle that takes the indicated number of Object
1438      *  arguments and returns an Object array of them, as if for varargs.
1439      */
1440     static MethodHandle varargsArray(int nargs) {
1441         MethodHandle mh = ARRAYS[nargs];
1442         if (mh != null) {
1443             return mh;
1444         }
1445         mh = makeCollector(Object[].class, nargs);
1446         assert(assertCorrectArity(mh, nargs));
1447         return ARRAYS[nargs] = mh;
1448     }
1449 
1450     /** Return a method handle that takes the indicated number of
1451      *  typed arguments and returns an array of them.
1452      *  The type argument is the array type.
1453      */
1454     static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
1455         Class<?> elemType = arrayType.getComponentType();
1456         if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
1457         if (nargs >= MAX_JVM_ARITY/2 - 1) {
1458             int slots = nargs;
1459             final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
1460             if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
1461                 slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
1462             if (slots > MAX_ARRAY_SLOTS)
1463                 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
1464         }
1465         if (elemType == Object.class)
1466             return varargsArray(nargs);
1467         // other cases:  primitive arrays, subtypes of Object[]
1468         MethodHandle cache[] = Makers.TYPED_COLLECTORS.get(elemType);
1469         MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
1470         if (mh != null)  return mh;
1471         mh = makeCollector(arrayType, nargs);
1472         assert(assertCorrectArity(mh, nargs));
1473         if (nargs < cache.length)
1474             cache[nargs] = mh;
1475         return mh;
1476     }
1477 
1478     private static boolean assertCorrectArity(MethodHandle mh, int arity) {
1479         assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
1480         return true;
1481     }
1482 
1483     static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
1484 
1485     /*non-public*/
1486     static void assertSame(Object mh1, Object mh2) {
1487         if (mh1 != mh2) {
1488             String msg = String.format("mh1 != mh2: mh1 = %s (form: %s); mh2 = %s (form: %s)",
1489                     mh1, ((MethodHandle)mh1).form,
1490                     mh2, ((MethodHandle)mh2).form);
1491             throw newInternalError(msg);
1492         }
1493     }
1494 
1495     // Local constant functions:
1496 
1497     /* non-public */
1498     static final byte NF_checkSpreadArgument = 0,
1499             NF_guardWithCatch = 1,
1500             NF_throwException = 2,
1501             NF_tryFinally = 3,
1502             NF_loop = 4,
1503             NF_profileBoolean = 5,
1504             NF_tableSwitch = 6,
1505             NF_LIMIT = 7;
1506 
1507     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
1508 
1509     static NamedFunction getFunction(byte func) {
1510         NamedFunction nf = NFS[func];
1511         if (nf != null) {
1512             return nf;
1513         }
1514         return NFS[func] = createFunction(func);
1515     }
1516 
1517     private static NamedFunction createFunction(byte func) {
1518         try {
1519             return switch (func) {
1520                 case NF_checkSpreadArgument -> new NamedFunction(MethodHandleImpl.class
1521                                                 .getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
1522                 case NF_guardWithCatch      -> new NamedFunction(MethodHandleImpl.class
1523                                                 .getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
1524                                                    MethodHandle.class, Object[].class));
1525                 case NF_tryFinally          -> new NamedFunction(MethodHandleImpl.class
1526                                                 .getDeclaredMethod("tryFinally", MethodHandle.class, MethodHandle.class, Object[].class));
1527                 case NF_loop                -> new NamedFunction(MethodHandleImpl.class
1528                                                 .getDeclaredMethod("loop", BasicType[].class, LoopClauses.class, Object[].class));
1529                 case NF_throwException      -> new NamedFunction(MethodHandleImpl.class
1530                                                 .getDeclaredMethod("throwException", Throwable.class));
1531                 case NF_profileBoolean      -> new NamedFunction(MethodHandleImpl.class
1532                                                 .getDeclaredMethod("profileBoolean", boolean.class, int[].class));
1533                 case NF_tableSwitch         -> new NamedFunction(MethodHandleImpl.class
1534                                                 .getDeclaredMethod("tableSwitch", int.class, MethodHandle.class, CasesHolder.class, Object[].class));
1535                 default -> throw new InternalError("Undefined function: " + func);
1536             };
1537         } catch (ReflectiveOperationException ex) {
1538             throw newInternalError(ex);
1539         }
1540     }
1541 
1542     static {
1543         SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
1544             @Override
1545             public Object newMemberName() {
1546                 return new MemberName();
1547             }
1548 
1549             @Override
1550             public String getName(Object mname) {
1551                 MemberName memberName = (MemberName)mname;
1552                 return memberName.getName();
1553             }
1554             @Override
1555             public Class<?> getDeclaringClass(Object mname) {
1556                 MemberName memberName = (MemberName)mname;
1557                 return memberName.getDeclaringClass();
1558             }
1559 
1560             @Override
1561             public MethodType getMethodType(Object mname) {
1562                 MemberName memberName = (MemberName)mname;
1563                 return memberName.getMethodType();
1564             }
1565 
1566             @Override
1567             public String getMethodDescriptor(Object mname) {
1568                 MemberName memberName = (MemberName)mname;
1569                 return memberName.getMethodDescriptor();
1570             }
1571 
1572             @Override
1573             public boolean isNative(Object mname) {
1574                 MemberName memberName = (MemberName)mname;
1575                 return memberName.isNative();
1576             }
1577 
1578             @Override
1579             public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1580                 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1581             }
1582 
1583             @Override
1584             public void ensureCustomized(MethodHandle mh) {
1585                 mh.customize();
1586             }
1587 
1588             @Override
1589             public VarHandle memoryAccessVarHandle(Class<?> carrier, boolean skipAlignmentMaskCheck, long alignmentMask,
1590                                                    ByteOrder order) {
1591                 return VarHandles.makeMemoryAddressViewHandle(carrier, skipAlignmentMaskCheck, alignmentMask, order);
1592             }
1593 
1594             @Override
1595             public MethodHandle nativeMethodHandle(NativeEntryPoint nep, MethodHandle fallback) {
1596                 return NativeMethodHandle.make(nep, fallback);
1597             }
1598 
1599             @Override
1600             public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1601                 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1602             }
1603 
1604             @Override
1605             public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1606                 return VarHandles.filterCoordinates(target, pos, filters);
1607             }
1608 
1609             @Override
1610             public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1611                 return VarHandles.dropCoordinates(target, pos, valueTypes);
1612             }
1613 
1614             @Override
1615             public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1616                 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1617             }
1618 
1619             @Override
1620             public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1621                 return VarHandles.collectCoordinates(target, pos, filter);
1622             }
1623 
1624             @Override
1625             public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1626                 return VarHandles.insertCoordinates(target, pos, values);
1627             }
1628 
1629 
1630             @Override
1631             public MethodHandle unreflectConstructor(Constructor<?> ctor) throws IllegalAccessException {
1632                 return IMPL_LOOKUP.unreflectConstructor(ctor);
1633             }
1634 
1635             @Override
1636             public MethodHandle unreflectField(Field field, boolean isSetter) throws IllegalAccessException {
1637                 return isSetter ? IMPL_LOOKUP.unreflectSetter(field) : IMPL_LOOKUP.unreflectGetter(field);
1638             }
1639 
1640             @Override
1641             public MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1642                 try {
1643                     return IMPL_LOOKUP.findVirtual(defc, name, type);
1644                 } catch (NoSuchMethodException e) {
1645                     return null;
1646                 }
1647             }
1648 
1649             @Override
1650             public MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws IllegalAccessException {
1651                 try {
1652                     return IMPL_LOOKUP.findStatic(defc, name, type);
1653                 } catch (NoSuchMethodException e) {
1654                     return null;
1655                 }
1656             }
1657 
1658             @Override
1659             public MethodHandle reflectiveInvoker(Class<?> caller) {
1660                 Objects.requireNonNull(caller);
1661                 return BindCaller.reflectiveInvoker(caller);
1662             }
1663 
1664             @Override
1665             public Lookup defineHiddenClassWithClassData(Lookup caller, String name, byte[] bytes, Object classData, boolean initialize) {
1666                 // skip name and access flags validation
1667                 return caller.makeHiddenClassDefiner(name, bytes, Set.of()).defineClassAsLookup(initialize, classData);
1668             }
1669 
1670         });
1671     }
1672 
1673     /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1674     private static MethodHandle unboxResultHandle(Class<?> returnType) {
1675         if (returnType.isPrimitive()) {
1676             if (returnType == void.class) {
1677                 return ValueConversions.ignore();
1678             } else {
1679                 Wrapper w = Wrapper.forPrimitiveType(returnType);
1680                 return ValueConversions.unboxExact(w);
1681             }
1682         } else {
1683             return MethodHandles.identity(Object.class);
1684         }
1685     }
1686 
1687     /**
1688      * Assembles a loop method handle from the given handles and type information.
1689      *
1690      * @param tloop the return type of the loop.
1691      * @param targs types of the arguments to be passed to the loop.
1692      * @param init sanitized array of initializers for loop-local variables.
1693      * @param step sanitited array of loop bodies.
1694      * @param pred sanitized array of predicates.
1695      * @param fini sanitized array of loop finalizers.
1696      *
1697      * @return a handle that, when invoked, will execute the loop.
1698      */
1699     static MethodHandle makeLoop(Class<?> tloop, List<Class<?>> targs, List<MethodHandle> init, List<MethodHandle> step,
1700                                  List<MethodHandle> pred, List<MethodHandle> fini) {
1701         MethodType type = MethodType.methodType(tloop, targs);
1702         BasicType[] initClauseTypes =
1703                 init.stream().map(h -> h.type().returnType()).map(BasicType::basicType).toArray(BasicType[]::new);
1704         LambdaForm form = makeLoopForm(type.basicType(), initClauseTypes);
1705 
1706         // Prepare auxiliary method handles used during LambdaForm interpretation.
1707         // Box arguments and wrap them into Object[]: ValueConversions.array().
1708         MethodType varargsType = type.changeReturnType(Object[].class);
1709         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1710         MethodHandle unboxResult = unboxResultHandle(tloop);
1711 
1712         LoopClauses clauseData =
1713                 new LoopClauses(new MethodHandle[][]{toArray(init), toArray(step), toArray(pred), toArray(fini)});
1714         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1715         BoundMethodHandle mh;
1716         try {
1717             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) clauseData,
1718                     (Object) collectArgs, (Object) unboxResult);
1719         } catch (Throwable ex) {
1720             throw uncaughtException(ex);
1721         }
1722         assert(mh.type() == type);
1723         return mh;
1724     }
1725 
1726     private static MethodHandle[] toArray(List<MethodHandle> l) {
1727         return l.toArray(new MethodHandle[0]);
1728     }
1729 
1730     /**
1731      * Loops introduce some complexity as they can have additional local state. Hence, LambdaForms for loops are
1732      * generated from a template. The LambdaForm template shape for the loop combinator is as follows (assuming one
1733      * reference parameter passed in {@code a1}, and a reference return type, with the return value represented by
1734      * {@code t12}):
1735      * <blockquote><pre>{@code
1736      *  loop=Lambda(a0:L,a1:L)=>{
1737      *    t2:L=BoundMethodHandle$Species_L3.argL0(a0:L);    // LoopClauses holding init, step, pred, fini handles
1738      *    t3:L=BoundMethodHandle$Species_L3.argL1(a0:L);    // helper handle to box the arguments into an Object[]
1739      *    t4:L=BoundMethodHandle$Species_L3.argL2(a0:L);    // helper handle to unbox the result
1740      *    t5:L=MethodHandle.invokeBasic(t3:L,a1:L);         // box the arguments into an Object[]
1741      *    t6:L=MethodHandleImpl.loop(null,t2:L,t3:L);       // call the loop executor
1742      *    t7:L=MethodHandle.invokeBasic(t4:L,t6:L);t7:L}    // unbox the result; return the result
1743      * }</pre></blockquote>
1744      * <p>
1745      * {@code argL0} is a LoopClauses instance holding, in a 2-dimensional array, the init, step, pred, and fini method
1746      * handles. {@code argL1} and {@code argL2} are auxiliary method handles: {@code argL1} boxes arguments and wraps
1747      * them into {@code Object[]} ({@code ValueConversions.array()}), and {@code argL2} unboxes the result if necessary
1748      * ({@code ValueConversions.unbox()}).
1749      * <p>
1750      * Having {@code t3} and {@code t4} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1751      * forms among loop combinators with the same basic type.
1752      * <p>
1753      * The above template is instantiated by using the {@link LambdaFormEditor} to replace the {@code null} argument to
1754      * the {@code loop} invocation with the {@code BasicType} array describing the loop clause types. This argument is
1755      * ignored in the loop invoker, but will be extracted and used in {@linkplain InvokerBytecodeGenerator#emitLoop(int)
1756      * bytecode generation}.
1757      */
1758     private static LambdaForm makeLoopForm(MethodType basicType, BasicType[] localVarTypes) {
1759         MethodType lambdaType = basicType.invokerType();
1760 
1761         final int THIS_MH = 0;  // the BMH_LLL
1762         final int ARG_BASE = 1; // start of incoming arguments
1763         final int ARG_LIMIT = ARG_BASE + basicType.parameterCount();
1764 
1765         int nameCursor = ARG_LIMIT;
1766         final int GET_CLAUSE_DATA = nameCursor++;
1767         final int GET_COLLECT_ARGS = nameCursor++;
1768         final int GET_UNBOX_RESULT = nameCursor++;
1769         final int BOXED_ARGS = nameCursor++;
1770         final int LOOP = nameCursor++;
1771         final int UNBOX_RESULT = nameCursor++;
1772 
1773         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_LOOP);
1774         if (lform == null) {
1775             Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
1776 
1777             BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
1778             names[THIS_MH] = names[THIS_MH].withConstraint(data);
1779             names[GET_CLAUSE_DATA] = new Name(data.getterFunction(0), names[THIS_MH]);
1780             names[GET_COLLECT_ARGS] = new Name(data.getterFunction(1), names[THIS_MH]);
1781             names[GET_UNBOX_RESULT] = new Name(data.getterFunction(2), names[THIS_MH]);
1782 
1783             // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
1784             MethodType collectArgsType = basicType.changeReturnType(Object.class);
1785             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
1786             Object[] args = new Object[invokeBasic.type().parameterCount()];
1787             args[0] = names[GET_COLLECT_ARGS];
1788             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
1789             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.LOOP)), args);
1790 
1791             // t_{i+1}:L=MethodHandleImpl.loop(localTypes:L,clauses:L,t_{i}:L);
1792             Object[] lArgs =
1793                     new Object[]{null, // placeholder for BasicType[] localTypes - will be added by LambdaFormEditor
1794                             names[GET_CLAUSE_DATA], names[BOXED_ARGS]};
1795             names[LOOP] = new Name(getFunction(NF_loop), lArgs);
1796 
1797             // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
1798             MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
1799             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[LOOP]};
1800             names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
1801 
1802             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_LOOP,
1803                     new LambdaForm(lambdaType.parameterCount(), names, Kind.LOOP));
1804         }
1805 
1806         // BOXED_ARGS is the index into the names array where the loop idiom starts
1807         return lform.editor().noteLoopLocalTypesForm(BOXED_ARGS, localVarTypes);
1808     }
1809 
1810     static class LoopClauses {
1811         @Stable final MethodHandle[][] clauses;
1812         LoopClauses(MethodHandle[][] clauses) {
1813             assert clauses.length == 4;
1814             this.clauses = clauses;
1815         }
1816         @Override
1817         public String toString() {
1818             StringBuilder sb = new StringBuilder("LoopClauses -- ");
1819             for (int i = 0; i < 4; ++i) {
1820                 if (i > 0) {
1821                     sb.append("       ");
1822                 }
1823                 sb.append('<').append(i).append(">: ");
1824                 MethodHandle[] hs = clauses[i];
1825                 for (int j = 0; j < hs.length; ++j) {
1826                     if (j > 0) {
1827                         sb.append("          ");
1828                     }
1829                     sb.append('*').append(j).append(": ").append(hs[j]).append('\n');
1830                 }
1831             }
1832             sb.append(" --\n");
1833             return sb.toString();
1834         }
1835     }
1836 
1837     /**
1838      * Intrinsified during LambdaForm compilation
1839      * (see {@link InvokerBytecodeGenerator#emitLoop(int)}).
1840      */
1841     @Hidden
1842     static Object loop(BasicType[] localTypes, LoopClauses clauseData, Object... av) throws Throwable {
1843         final MethodHandle[] init = clauseData.clauses[0];
1844         final MethodHandle[] step = clauseData.clauses[1];
1845         final MethodHandle[] pred = clauseData.clauses[2];
1846         final MethodHandle[] fini = clauseData.clauses[3];
1847         int varSize = (int) Stream.of(init).filter(h -> h.type().returnType() != void.class).count();
1848         int nArgs = init[0].type().parameterCount();
1849         Object[] varsAndArgs = new Object[varSize + nArgs];
1850         for (int i = 0, v = 0; i < init.length; ++i) {
1851             MethodHandle ih = init[i];
1852             if (ih.type().returnType() == void.class) {
1853                 ih.invokeWithArguments(av);
1854             } else {
1855                 varsAndArgs[v++] = ih.invokeWithArguments(av);
1856             }
1857         }
1858         System.arraycopy(av, 0, varsAndArgs, varSize, nArgs);
1859         final int nSteps = step.length;
1860         for (; ; ) {
1861             for (int i = 0, v = 0; i < nSteps; ++i) {
1862                 MethodHandle p = pred[i];
1863                 MethodHandle s = step[i];
1864                 MethodHandle f = fini[i];
1865                 if (s.type().returnType() == void.class) {
1866                     s.invokeWithArguments(varsAndArgs);
1867                 } else {
1868                     varsAndArgs[v++] = s.invokeWithArguments(varsAndArgs);
1869                 }
1870                 if (!(boolean) p.invokeWithArguments(varsAndArgs)) {
1871                     return f.invokeWithArguments(varsAndArgs);
1872                 }
1873             }
1874         }
1875     }
1876 
1877     /**
1878      * This method is bound as the predicate in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1879      * MethodHandle) counting loops}.
1880      *
1881      * @param limit the upper bound of the parameter, statically bound at loop creation time.
1882      * @param counter the counter parameter, passed in during loop execution.
1883      *
1884      * @return whether the counter has reached the limit.
1885      */
1886     static boolean countedLoopPredicate(int limit, int counter) {
1887         return counter < limit;
1888     }
1889 
1890     /**
1891      * This method is bound as the step function in {@linkplain MethodHandles#countedLoop(MethodHandle, MethodHandle,
1892      * MethodHandle) counting loops} to increment the counter.
1893      *
1894      * @param limit the upper bound of the loop counter (ignored).
1895      * @param counter the loop counter.
1896      *
1897      * @return the loop counter incremented by 1.
1898      */
1899     static int countedLoopStep(int limit, int counter) {
1900         return counter + 1;
1901     }
1902 
1903     /**
1904      * This is bound to initialize the loop-local iterator in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1905      *
1906      * @param it the {@link Iterable} over which the loop iterates.
1907      *
1908      * @return an {@link Iterator} over the argument's elements.
1909      */
1910     static Iterator<?> initIterator(Iterable<?> it) {
1911         return it.iterator();
1912     }
1913 
1914     /**
1915      * This method is bound as the predicate in {@linkplain MethodHandles#iteratedLoop iterating loops}.
1916      *
1917      * @param it the iterator to be checked.
1918      *
1919      * @return {@code true} iff there are more elements to iterate over.
1920      */
1921     static boolean iteratePredicate(Iterator<?> it) {
1922         return it.hasNext();
1923     }
1924 
1925     /**
1926      * This method is bound as the step for retrieving the current value from the iterator in {@linkplain
1927      * MethodHandles#iteratedLoop iterating loops}.
1928      *
1929      * @param it the iterator.
1930      *
1931      * @return the next element from the iterator.
1932      */
1933     static Object iterateNext(Iterator<?> it) {
1934         return it.next();
1935     }
1936 
1937     /**
1938      * Makes a {@code try-finally} handle that conforms to the type constraints.
1939      *
1940      * @param target the target to execute in a {@code try-finally} block.
1941      * @param cleanup the cleanup to execute in the {@code finally} block.
1942      * @param rtype the result type of the entire construct.
1943      * @param argTypes the types of the arguments.
1944      *
1945      * @return a handle on the constructed {@code try-finally} block.
1946      */
1947     static MethodHandle makeTryFinally(MethodHandle target, MethodHandle cleanup, Class<?> rtype, List<Class<?>> argTypes) {
1948         MethodType type = MethodType.methodType(rtype, argTypes);
1949         LambdaForm form = makeTryFinallyForm(type.basicType());
1950 
1951         // Prepare auxiliary method handles used during LambdaForm interpretation.
1952         // Box arguments and wrap them into Object[]: ValueConversions.array().
1953         MethodType varargsType = type.changeReturnType(Object[].class);
1954         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
1955         MethodHandle unboxResult = unboxResultHandle(rtype);
1956 
1957         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
1958         BoundMethodHandle mh;
1959         try {
1960             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) target, (Object) cleanup,
1961                     (Object) collectArgs, (Object) unboxResult);
1962         } catch (Throwable ex) {
1963             throw uncaughtException(ex);
1964         }
1965         assert(mh.type() == type);
1966         return mh;
1967     }
1968 
1969     /**
1970      * The LambdaForm shape for the tryFinally combinator is as follows (assuming one reference parameter passed in
1971      * {@code a1}, and a reference return type, with the return value represented by {@code t8}):
1972      * <blockquote><pre>{@code
1973      *  tryFinally=Lambda(a0:L,a1:L)=>{
1974      *    t2:L=BoundMethodHandle$Species_LLLL.argL0(a0:L);  // target method handle
1975      *    t3:L=BoundMethodHandle$Species_LLLL.argL1(a0:L);  // cleanup method handle
1976      *    t4:L=BoundMethodHandle$Species_LLLL.argL2(a0:L);  // helper handle to box the arguments into an Object[]
1977      *    t5:L=BoundMethodHandle$Species_LLLL.argL3(a0:L);  // helper handle to unbox the result
1978      *    t6:L=MethodHandle.invokeBasic(t4:L,a1:L);         // box the arguments into an Object[]
1979      *    t7:L=MethodHandleImpl.tryFinally(t2:L,t3:L,t6:L); // call the tryFinally executor
1980      *    t8:L=MethodHandle.invokeBasic(t5:L,t7:L);t8:L}    // unbox the result; return the result
1981      * }</pre></blockquote>
1982      * <p>
1983      * {@code argL0} and {@code argL1} are the target and cleanup method handles.
1984      * {@code argL2} and {@code argL3} are auxiliary method handles: {@code argL2} boxes arguments and wraps them into
1985      * {@code Object[]} ({@code ValueConversions.array()}), and {@code argL3} unboxes the result if necessary
1986      * ({@code ValueConversions.unbox()}).
1987      * <p>
1988      * Having {@code t4} and {@code t5} passed in via a BMH and not hardcoded in the lambda form allows to share lambda
1989      * forms among tryFinally combinators with the same basic type.
1990      */
1991     private static LambdaForm makeTryFinallyForm(MethodType basicType) {
1992         MethodType lambdaType = basicType.invokerType();
1993 
1994         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_TF);
1995         if (lform != null) {
1996             return lform;
1997         }
1998         final int THIS_MH      = 0;  // the BMH_LLLL
1999         final int ARG_BASE     = 1;  // start of incoming arguments
2000         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
2001 
2002         int nameCursor = ARG_LIMIT;
2003         final int GET_TARGET       = nameCursor++;
2004         final int GET_CLEANUP      = nameCursor++;
2005         final int GET_COLLECT_ARGS = nameCursor++;
2006         final int GET_UNBOX_RESULT = nameCursor++;
2007         final int BOXED_ARGS       = nameCursor++;
2008         final int TRY_FINALLY      = nameCursor++;
2009         final int UNBOX_RESULT     = nameCursor++;
2010 
2011         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
2012 
2013         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2014         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
2015         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
2016         names[GET_CLEANUP]      = new Name(data.getterFunction(1), names[THIS_MH]);
2017         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(2), names[THIS_MH]);
2018         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(3), names[THIS_MH]);
2019 
2020         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
2021         MethodType collectArgsType = basicType.changeReturnType(Object.class);
2022         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2023         Object[] args = new Object[invokeBasic.type().parameterCount()];
2024         args[0] = names[GET_COLLECT_ARGS];
2025         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
2026         names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TRY_FINALLY)), args);
2027 
2028         // t_{i+1}:L=MethodHandleImpl.tryFinally(target:L,exType:L,catcher:L,t_{i}:L);
2029         Object[] tfArgs = new Object[] {names[GET_TARGET], names[GET_CLEANUP], names[BOXED_ARGS]};
2030         names[TRY_FINALLY] = new Name(getFunction(NF_tryFinally), tfArgs);
2031 
2032         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
2033         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2034         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_FINALLY]};
2035         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
2036 
2037         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TRY_FINALLY);
2038 
2039         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_TF, lform);
2040     }
2041 
2042     /**
2043      * Intrinsified during LambdaForm compilation
2044      * (see {@link InvokerBytecodeGenerator#emitTryFinally emitTryFinally}).
2045      */
2046     @Hidden
2047     static Object tryFinally(MethodHandle target, MethodHandle cleanup, Object... av) throws Throwable {
2048         Throwable t = null;
2049         Object r = null;
2050         try {
2051             r = target.invokeWithArguments(av);
2052         } catch (Throwable thrown) {
2053             t = thrown;
2054             throw t;
2055         } finally {
2056             Object[] args = target.type().returnType() == void.class ? prepend(av, t) : prepend(av, t, r);
2057             r = cleanup.invokeWithArguments(args);
2058         }
2059         return r;
2060     }
2061 
2062     // see varargsArray method for chaching/package-private version of this
2063     private static MethodHandle makeCollector(Class<?> arrayType, int parameterCount) {
2064         MethodType type = MethodType.methodType(arrayType, Collections.nCopies(parameterCount, arrayType.componentType()));
2065         MethodHandle newArray = MethodHandles.arrayConstructor(arrayType);
2066 
2067         LambdaForm form = makeCollectorForm(type.basicType(), arrayType);
2068 
2069         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2070         BoundMethodHandle mh;
2071         try {
2072             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) newArray);
2073         } catch (Throwable ex) {
2074             throw uncaughtException(ex);
2075         }
2076         assert(mh.type() == type);
2077         return mh;
2078     }
2079 
2080     private static LambdaForm makeCollectorForm(MethodType basicType, Class<?> arrayType) {
2081         MethodType lambdaType = basicType.invokerType();
2082         int parameterCount = basicType.parameterCount();
2083 
2084         // Only share the lambda form for empty arrays and reference types.
2085         // Sharing based on the basic type alone doesn't work because
2086         // we need a separate lambda form for byte/short/char/int which
2087         // are all erased to int otherwise.
2088         // Other caching for primitive types happens at the MethodHandle level (see varargsArray).
2089         boolean isReferenceType = !arrayType.componentType().isPrimitive();
2090         boolean isSharedLambdaForm = parameterCount == 0 || isReferenceType;
2091         if (isSharedLambdaForm) {
2092             LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_COLLECTOR);
2093             if (lform != null) {
2094                 return lform;
2095             }
2096         }
2097 
2098         // use erased accessor for reference types
2099         MethodHandle storeFunc = isReferenceType
2100                 ? ArrayAccessor.OBJECT_ARRAY_SETTER
2101                 : makeArrayElementAccessor(arrayType, ArrayAccess.SET);
2102 
2103         final int THIS_MH      = 0;  // the BMH_L
2104         final int ARG_BASE     = 1;  // start of incoming arguments
2105         final int ARG_LIMIT    = ARG_BASE + parameterCount;
2106 
2107         int nameCursor = ARG_LIMIT;
2108         final int GET_NEW_ARRAY       = nameCursor++;
2109         final int CALL_NEW_ARRAY      = nameCursor++;
2110         final int STORE_ELEMENT_BASE  = nameCursor;
2111         final int STORE_ELEMENT_LIMIT = STORE_ELEMENT_BASE + parameterCount;
2112         nameCursor = STORE_ELEMENT_LIMIT;
2113 
2114         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
2115 
2116         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_L();
2117         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
2118         names[GET_NEW_ARRAY]    = new Name(data.getterFunction(0), names[THIS_MH]);
2119 
2120         MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(Object.class, int.class));
2121         names[CALL_NEW_ARRAY] = new Name(new NamedFunction(invokeBasic), names[GET_NEW_ARRAY], parameterCount);
2122         for (int storeIndex = 0,
2123              storeNameCursor = STORE_ELEMENT_BASE,
2124              argCursor = ARG_BASE;
2125              storeNameCursor < STORE_ELEMENT_LIMIT;
2126              storeIndex++, storeNameCursor++, argCursor++){
2127 
2128             names[storeNameCursor] = new Name(new NamedFunction(makeIntrinsic(storeFunc, Intrinsic.ARRAY_STORE)),
2129                     names[CALL_NEW_ARRAY], storeIndex, names[argCursor]);
2130         }
2131 
2132         LambdaForm lform = new LambdaForm(lambdaType.parameterCount(), names, CALL_NEW_ARRAY, Kind.COLLECTOR);
2133         if (isSharedLambdaForm) {
2134             lform = basicType.form().setCachedLambdaForm(MethodTypeForm.LF_COLLECTOR, lform);
2135         }
2136         return lform;
2137     }
2138 
2139     // use a wrapper because we need this array to be @Stable
2140     static class CasesHolder {
2141         @Stable
2142         final MethodHandle[] cases;
2143 
2144         public CasesHolder(MethodHandle[] cases) {
2145             this.cases = cases;
2146         }
2147     }
2148 
2149     static MethodHandle makeTableSwitch(MethodType type, MethodHandle defaultCase, MethodHandle[] caseActions) {
2150         MethodType varargsType = type.changeReturnType(Object[].class);
2151         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
2152 
2153         MethodHandle unboxResult = unboxResultHandle(type.returnType());
2154 
2155         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLL();
2156         LambdaForm form = makeTableSwitchForm(type.basicType(), data, caseActions.length);
2157         BoundMethodHandle mh;
2158         CasesHolder caseHolder =  new CasesHolder(caseActions);
2159         try {
2160             mh = (BoundMethodHandle) data.factory().invokeBasic(type, form, (Object) defaultCase, (Object) collectArgs,
2161                                                                 (Object) unboxResult, (Object) caseHolder);
2162         } catch (Throwable ex) {
2163             throw uncaughtException(ex);
2164         }
2165         assert(mh.type() == type);
2166         return mh;
2167     }
2168 
2169     private static class TableSwitchCacheKey {
2170         private static final Map<TableSwitchCacheKey, LambdaForm> CACHE = new ConcurrentHashMap<>();
2171 
2172         private final MethodType basicType;
2173         private final int numberOfCases;
2174 
2175         public TableSwitchCacheKey(MethodType basicType, int numberOfCases) {
2176             this.basicType = basicType;
2177             this.numberOfCases = numberOfCases;
2178         }
2179 
2180         @Override
2181         public boolean equals(Object o) {
2182             if (this == o) return true;
2183             if (o == null || getClass() != o.getClass()) return false;
2184             TableSwitchCacheKey that = (TableSwitchCacheKey) o;
2185             return numberOfCases == that.numberOfCases && Objects.equals(basicType, that.basicType);
2186         }
2187         @Override
2188         public int hashCode() {
2189             return Objects.hash(basicType, numberOfCases);
2190         }
2191     }
2192 
2193     private static LambdaForm makeTableSwitchForm(MethodType basicType, BoundMethodHandle.SpeciesData data,
2194                                                   int numCases) {
2195         MethodType lambdaType = basicType.invokerType();
2196 
2197         // We need to cache based on the basic type X number of cases,
2198         // since the number of cases is used when generating bytecode.
2199         // This also means that we can't use the cache in MethodTypeForm,
2200         // which only uses the basic type as a key.
2201         TableSwitchCacheKey key = new TableSwitchCacheKey(basicType, numCases);
2202         LambdaForm lform = TableSwitchCacheKey.CACHE.get(key);
2203         if (lform != null) {
2204             return lform;
2205         }
2206 
2207         final int THIS_MH       = 0;
2208         final int ARG_BASE      = 1;  // start of incoming arguments
2209         final int ARG_LIMIT     = ARG_BASE + basicType.parameterCount();
2210         final int ARG_SWITCH_ON = ARG_BASE;
2211         assert ARG_SWITCH_ON < ARG_LIMIT;
2212 
2213         int nameCursor = ARG_LIMIT;
2214         final int GET_COLLECT_ARGS  = nameCursor++;
2215         final int GET_DEFAULT_CASE  = nameCursor++;
2216         final int GET_UNBOX_RESULT  = nameCursor++;
2217         final int GET_CASES         = nameCursor++;
2218         final int BOXED_ARGS        = nameCursor++;
2219         final int TABLE_SWITCH      = nameCursor++;
2220         final int UNBOXED_RESULT    = nameCursor++;
2221 
2222         int fieldCursor = 0;
2223         final int FIELD_DEFAULT_CASE  = fieldCursor++;
2224         final int FIELD_COLLECT_ARGS  = fieldCursor++;
2225         final int FIELD_UNBOX_RESULT  = fieldCursor++;
2226         final int FIELD_CASES         = fieldCursor++;
2227 
2228         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
2229 
2230         names[THIS_MH] = names[THIS_MH].withConstraint(data);
2231         names[GET_DEFAULT_CASE] = new Name(data.getterFunction(FIELD_DEFAULT_CASE), names[THIS_MH]);
2232         names[GET_COLLECT_ARGS]  = new Name(data.getterFunction(FIELD_COLLECT_ARGS), names[THIS_MH]);
2233         names[GET_UNBOX_RESULT]  = new Name(data.getterFunction(FIELD_UNBOX_RESULT), names[THIS_MH]);
2234         names[GET_CASES] = new Name(data.getterFunction(FIELD_CASES), names[THIS_MH]);
2235 
2236         {
2237             MethodType collectArgsType = basicType.changeReturnType(Object.class);
2238             MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
2239             Object[] args = new Object[invokeBasic.type().parameterCount()];
2240             args[0] = names[GET_COLLECT_ARGS];
2241             System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT - ARG_BASE);
2242             names[BOXED_ARGS] = new Name(new NamedFunction(makeIntrinsic(invokeBasic, Intrinsic.TABLE_SWITCH, numCases)), args);
2243         }
2244 
2245         {
2246             Object[] tfArgs = new Object[]{
2247                 names[ARG_SWITCH_ON], names[GET_DEFAULT_CASE], names[GET_CASES], names[BOXED_ARGS]};
2248             names[TABLE_SWITCH] = new Name(getFunction(NF_tableSwitch), tfArgs);
2249         }
2250 
2251         {
2252             MethodHandle invokeBasic = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
2253             Object[] unboxArgs = new Object[]{names[GET_UNBOX_RESULT], names[TABLE_SWITCH]};
2254             names[UNBOXED_RESULT] = new Name(invokeBasic, unboxArgs);
2255         }
2256 
2257         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TABLE_SWITCH);
2258         LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2259         return prev != null ? prev : lform;
2260     }
2261 
2262     @Hidden
2263     static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2264         MethodHandle[] caseActions = holder.cases;
2265         MethodHandle selectedCase;
2266         if (input < 0 || input >= caseActions.length) {
2267             selectedCase = defaultCase;
2268         } else {
2269             selectedCase = caseActions[input];
2270         }
2271         return selectedCase.invokeWithArguments(args);
2272     }
2273 
2274     // Indexes into constant method handles:
2275     static final int
2276             MH_cast                  = 0,
2277             MH_selectAlternative     = 1,
2278             MH_countedLoopPred       = 2,
2279             MH_countedLoopStep       = 3,
2280             MH_initIterator          = 4,
2281             MH_iteratePred           = 5,
2282             MH_iterateNext           = 6,
2283             MH_Array_newInstance     = 7,
2284             MH_LIMIT                 = 8;
2285 
2286     static MethodHandle getConstantHandle(int idx) {
2287         MethodHandle handle = HANDLES[idx];
2288         if (handle != null) {
2289             return handle;
2290         }
2291         return setCachedHandle(idx, makeConstantHandle(idx));
2292     }
2293 
2294     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2295         // Simulate a CAS, to avoid racy duplication of results.
2296         MethodHandle prev = HANDLES[idx];
2297         if (prev != null) {
2298             return prev;
2299         }
2300         HANDLES[idx] = method;
2301         return method;
2302     }
2303 
2304     // Local constant method handles:
2305     private static final @Stable MethodHandle[] HANDLES = new MethodHandle[MH_LIMIT];
2306 
2307     private static MethodHandle makeConstantHandle(int idx) {
2308         try {
2309             switch (idx) {
2310                 case MH_cast:
2311                     return IMPL_LOOKUP.findVirtual(Class.class, "cast",
2312                             MethodType.methodType(Object.class, Object.class));
2313                 case MH_selectAlternative:
2314                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2315                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2316                 case MH_countedLoopPred:
2317                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2318                             MethodType.methodType(boolean.class, int.class, int.class));
2319                 case MH_countedLoopStep:
2320                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2321                             MethodType.methodType(int.class, int.class, int.class));
2322                 case MH_initIterator:
2323                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2324                             MethodType.methodType(Iterator.class, Iterable.class));
2325                 case MH_iteratePred:
2326                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2327                             MethodType.methodType(boolean.class, Iterator.class));
2328                 case MH_iterateNext:
2329                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2330                             MethodType.methodType(Object.class, Iterator.class));
2331                 case MH_Array_newInstance:
2332                     return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2333                             MethodType.methodType(Object.class, Class.class, int.class));
2334             }
2335         } catch (ReflectiveOperationException ex) {
2336             throw newInternalError(ex);
2337         }
2338         throw newInternalError("Unknown function index: " + idx);
2339     }
2340 }
--- EOF ---