1 /*
   2  * Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.perf.PerfCounter;
  29 import jdk.internal.vm.annotation.DontInline;
  30 import jdk.internal.vm.annotation.Hidden;
  31 import jdk.internal.vm.annotation.Stable;
  32 import sun.invoke.util.Wrapper;
  33 
  34 import java.lang.annotation.ElementType;
  35 import java.lang.annotation.Retention;
  36 import java.lang.annotation.RetentionPolicy;
  37 import java.lang.annotation.Target;
  38 import java.lang.reflect.Method;
  39 import java.util.Arrays;
  40 import java.util.HashMap;
  41 
  42 import static java.lang.invoke.LambdaForm.BasicType.*;
  43 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  44 import static java.lang.invoke.MethodHandleStatics.*;
  45 
  46 /**
  47  * The symbolic, non-executable form of a method handle's invocation semantics.
  48  * It consists of a series of names.
  49  * The first N (N=arity) names are parameters,
  50  * while any remaining names are temporary values.
  51  * Each temporary specifies the application of a function to some arguments.
  52  * The functions are method handles, while the arguments are mixes of
  53  * constant values and local names.
  54  * The result of the lambda is defined as one of the names, often the last one.
  55  * <p>
  56  * Here is an approximate grammar:
  57  * <blockquote><pre>{@code
  58  * LambdaForm = "(" ArgName* ")=>{" TempName* Result "}"
  59  * ArgName = "a" N ":" T
  60  * TempName = "t" N ":" T "=" Function "(" Argument* ");"
  61  * Function = ConstantValue
  62  * Argument = NameRef | ConstantValue
  63  * Result = NameRef | "void"
  64  * NameRef = "a" N | "t" N
  65  * N = (any whole number)
  66  * T = "L" | "I" | "J" | "F" | "D" | "V"
  67  * }</pre></blockquote>
  68  * Names are numbered consecutively from left to right starting at zero.
  69  * (The letters are merely a taste of syntax sugar.)
  70  * Thus, the first temporary (if any) is always numbered N (where N=arity).
  71  * Every occurrence of a name reference in an argument list must refer to
  72  * a name previously defined within the same lambda.
  73  * A lambda has a void result if and only if its result index is -1.
  74  * If a temporary has the type "V", it cannot be the subject of a NameRef,
  75  * even though possesses a number.
  76  * Note that all reference types are erased to "L", which stands for {@code Object}.
  77  * All subword types (boolean, byte, short, char) are erased to "I" which is {@code int}.
  78  * The other types stand for the usual primitive types.
  79  * <p>
  80  * Function invocation closely follows the static rules of the Java verifier.
  81  * Arguments and return values must exactly match when their "Name" types are
  82  * considered.
  83  * Conversions are allowed only if they do not change the erased type.
  84  * <ul>
  85  * <li>L = Object: casts are used freely to convert into and out of reference types
  86  * <li>I = int: subword types are forcibly narrowed when passed as arguments (see {@code explicitCastArguments})
  87  * <li>J = long: no implicit conversions
  88  * <li>F = float: no implicit conversions
  89  * <li>D = double: no implicit conversions
  90  * <li>V = void: a function result may be void if and only if its Name is of type "V"
  91  * </ul>
  92  * Although implicit conversions are not allowed, explicit ones can easily be
  93  * encoded by using temporary expressions which call type-transformed identity functions.
  94  * <p>
  95  * Examples:
  96  * <blockquote><pre>{@code
  97  * (a0:J)=>{ a0 }
  98  *     == identity(long)
  99  * (a0:I)=>{ t1:V = System.out#println(a0); void }
 100  *     == System.out#println(int)
 101  * (a0:L)=>{ t1:V = System.out#println(a0); a0 }
 102  *     == identity, with printing side-effect
 103  * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
 104  *                 t3:L = BoundMethodHandle#target(a0);
 105  *                 t4:L = MethodHandle#invoke(t3, t2, a1); t4 }
 106  *     == general invoker for unary insertArgument combination
 107  * (a0:L, a1:L)=>{ t2:L = FilterMethodHandle#filter(a0);
 108  *                 t3:L = MethodHandle#invoke(t2, a1);
 109  *                 t4:L = FilterMethodHandle#target(a0);
 110  *                 t5:L = MethodHandle#invoke(t4, t3); t5 }
 111  *     == general invoker for unary filterArgument combination
 112  * (a0:L, a1:L)=>{ ...(same as previous example)...
 113  *                 t5:L = MethodHandle#invoke(t4, t3, a1); t5 }
 114  *     == general invoker for unary/unary foldArgument combination
 115  * (a0:L, a1:I)=>{ t2:I = identity(long).asType((int)->long)(a1); t2 }
 116  *     == invoker for identity method handle which performs i2l
 117  * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
 118  *                 t3:L = Class#cast(t2,a1); t3 }
 119  *     == invoker for identity method handle which performs cast
 120  * }</pre></blockquote>
 121  * <p>
 122  * @author John Rose, JSR 292 EG
 123  */
 124 class LambdaForm {
 125     final int arity;
 126     final int result;
 127     final boolean forceInline;
 128     final MethodHandle customized;
 129     @Stable final Name[] names;
 130     final Kind kind;
 131     MemberName vmentry;   // low-level behavior, or null if not yet prepared
 132     private boolean isCompiled;
 133 
 134     // Either a LambdaForm cache (managed by LambdaFormEditor) or a link to uncustomized version (for customized LF)
 135     volatile Object transformCache;
 136 
 137     public static final int VOID_RESULT = -1, LAST_RESULT = -2;
 138 
 139     enum BasicType {
 140         L_TYPE('L', Object.class, Wrapper.OBJECT),  // all reference types
 141         I_TYPE('I', int.class,    Wrapper.INT),
 142         J_TYPE('J', long.class,   Wrapper.LONG),
 143         F_TYPE('F', float.class,  Wrapper.FLOAT),
 144         D_TYPE('D', double.class, Wrapper.DOUBLE),  // all primitive types
 145         V_TYPE('V', void.class,   Wrapper.VOID);    // not valid in all contexts
 146 
 147         static final @Stable BasicType[] ALL_TYPES = BasicType.values();
 148         static final @Stable BasicType[] ARG_TYPES = Arrays.copyOf(ALL_TYPES, ALL_TYPES.length-1);
 149 
 150         static final int ARG_TYPE_LIMIT = ARG_TYPES.length;
 151         static final int TYPE_LIMIT = ALL_TYPES.length;
 152 
 153         final char btChar;
 154         final Class<?> btClass;
 155         final Wrapper btWrapper;
 156 
 157         private BasicType(char btChar, Class<?> btClass, Wrapper wrapper) {
 158             this.btChar = btChar;
 159             this.btClass = btClass;
 160             this.btWrapper = wrapper;
 161         }
 162 
 163         char basicTypeChar() {
 164             return btChar;
 165         }
 166         Class<?> basicTypeClass() {
 167             return btClass;
 168         }
 169         Wrapper basicTypeWrapper() {
 170             return btWrapper;
 171         }
 172         int basicTypeSlots() {
 173             return btWrapper.stackSlots();
 174         }
 175 
 176         static BasicType basicType(byte type) {
 177             return ALL_TYPES[type];
 178         }
 179         static BasicType basicType(char type) {
 180             return switch (type) {
 181                 case 'L' -> L_TYPE;
 182                 case 'I' -> I_TYPE;
 183                 case 'J' -> J_TYPE;
 184                 case 'F' -> F_TYPE;
 185                 case 'D' -> D_TYPE;
 186                 case 'V' -> V_TYPE;
 187                 // all subword types are represented as ints
 188                 case 'Z', 'B', 'S', 'C' -> I_TYPE;
 189                 default -> throw newInternalError("Unknown type char: '" + type + "'");
 190             };
 191         }
 192         static BasicType basicType(Class<?> type) {
 193             return basicType(Wrapper.basicTypeChar(type));
 194         }
 195         static int[] basicTypeOrds(BasicType[] types) {
 196             if (types == null) {
 197                 return null;
 198             }
 199             int[] a = new int[types.length];
 200             for(int i = 0; i < types.length; ++i) {
 201                 a[i] = types[i].ordinal();
 202             }
 203             return a;
 204         }
 205 
 206         static char basicTypeChar(Class<?> type) {
 207             return basicType(type).btChar;
 208         }
 209 
 210         static int[] basicTypesOrd(Class<?>[] types) {
 211             int[] ords = new int[types.length];
 212             for (int i = 0; i < ords.length; i++) {
 213                 ords[i] = basicType(types[i]).ordinal();
 214             }
 215             return ords;
 216         }
 217 
 218         static boolean isBasicTypeChar(char c) {
 219             return "LIJFDV".indexOf(c) >= 0;
 220         }
 221         static boolean isArgBasicTypeChar(char c) {
 222             return "LIJFD".indexOf(c) >= 0;
 223         }
 224 
 225         static { assert(checkBasicType()); }
 226         private static boolean checkBasicType() {
 227             for (int i = 0; i < ARG_TYPE_LIMIT; i++) {
 228                 assert ARG_TYPES[i].ordinal() == i;
 229                 assert ARG_TYPES[i] == ALL_TYPES[i];
 230             }
 231             for (int i = 0; i < TYPE_LIMIT; i++) {
 232                 assert ALL_TYPES[i].ordinal() == i;
 233             }
 234             assert ALL_TYPES[TYPE_LIMIT - 1] == V_TYPE;
 235             assert !Arrays.asList(ARG_TYPES).contains(V_TYPE);
 236             return true;
 237         }
 238     }
 239 
 240     enum Kind {
 241         GENERIC("invoke"),
 242         ZERO("zero"),
 243         IDENTITY("identity"),
 244         BOUND_REINVOKER("BMH.reinvoke", "reinvoke"),
 245         REINVOKER("MH.reinvoke", "reinvoke"),
 246         DELEGATE("MH.delegate", "delegate"),
 247         EXACT_LINKER("MH.invokeExact_MT", "invokeExact_MT"),
 248         EXACT_INVOKER("MH.exactInvoker", "exactInvoker"),
 249         GENERIC_LINKER("MH.invoke_MT", "invoke_MT"),
 250         GENERIC_INVOKER("MH.invoker", "invoker"),
 251         LINK_TO_TARGET_METHOD("linkToTargetMethod"),
 252         LINK_TO_CALL_SITE("linkToCallSite"),
 253         DIRECT_INVOKE_VIRTUAL("DMH.invokeVirtual", "invokeVirtual"),
 254         DIRECT_INVOKE_SPECIAL("DMH.invokeSpecial", "invokeSpecial"),
 255         DIRECT_INVOKE_SPECIAL_IFC("DMH.invokeSpecialIFC", "invokeSpecialIFC"),
 256         DIRECT_INVOKE_STATIC("DMH.invokeStatic", "invokeStatic"),
 257         DIRECT_NEW_INVOKE_SPECIAL("DMH.newInvokeSpecial", "newInvokeSpecial"),
 258         DIRECT_INVOKE_INTERFACE("DMH.invokeInterface", "invokeInterface"),
 259         DIRECT_INVOKE_STATIC_INIT("DMH.invokeStaticInit", "invokeStaticInit"),
 260         GET_REFERENCE("getReference"),
 261         PUT_REFERENCE("putReference"),
 262         GET_REFERENCE_VOLATILE("getReferenceVolatile"),
 263         PUT_REFERENCE_VOLATILE("putReferenceVolatile"),
 264         GET_VALUE("getValue"),
 265         PUT_VALUE("putValue"),
 266         GET_VALUE_VOLATILE("getValueVolatile"),
 267         PUT_VALUE_VOLATILE("putValueVolatile"),
 268         GET_INT("getInt"),
 269         PUT_INT("putInt"),
 270         GET_INT_VOLATILE("getIntVolatile"),
 271         PUT_INT_VOLATILE("putIntVolatile"),
 272         GET_BOOLEAN("getBoolean"),
 273         PUT_BOOLEAN("putBoolean"),
 274         GET_BOOLEAN_VOLATILE("getBooleanVolatile"),
 275         PUT_BOOLEAN_VOLATILE("putBooleanVolatile"),
 276         GET_BYTE("getByte"),
 277         PUT_BYTE("putByte"),
 278         GET_BYTE_VOLATILE("getByteVolatile"),
 279         PUT_BYTE_VOLATILE("putByteVolatile"),
 280         GET_CHAR("getChar"),
 281         PUT_CHAR("putChar"),
 282         GET_CHAR_VOLATILE("getCharVolatile"),
 283         PUT_CHAR_VOLATILE("putCharVolatile"),
 284         GET_SHORT("getShort"),
 285         PUT_SHORT("putShort"),
 286         GET_SHORT_VOLATILE("getShortVolatile"),
 287         PUT_SHORT_VOLATILE("putShortVolatile"),
 288         GET_LONG("getLong"),
 289         PUT_LONG("putLong"),
 290         GET_LONG_VOLATILE("getLongVolatile"),
 291         PUT_LONG_VOLATILE("putLongVolatile"),
 292         GET_FLOAT("getFloat"),
 293         PUT_FLOAT("putFloat"),
 294         GET_FLOAT_VOLATILE("getFloatVolatile"),
 295         PUT_FLOAT_VOLATILE("putFloatVolatile"),
 296         GET_DOUBLE("getDouble"),
 297         PUT_DOUBLE("putDouble"),
 298         GET_DOUBLE_VOLATILE("getDoubleVolatile"),
 299         PUT_DOUBLE_VOLATILE("putDoubleVolatile"),
 300         TRY_FINALLY("tryFinally"),
 301         TABLE_SWITCH("tableSwitch"),
 302         COLLECT("collect"),
 303         COLLECTOR("collector"),
 304         CONVERT("convert"),
 305         SPREAD("spread"),
 306         LOOP("loop"),
 307         FIELD("field"),
 308         GUARD("guard"),
 309         GUARD_WITH_CATCH("guardWithCatch"),
 310         VARHANDLE_EXACT_INVOKER("VH.exactInvoker"),
 311         VARHANDLE_INVOKER("VH.invoker", "invoker"),
 312         VARHANDLE_LINKER("VH.invoke_MT", "invoke_MT");
 313 
 314         final String defaultLambdaName;
 315         final String methodName;
 316 
 317         private Kind(String defaultLambdaName) {
 318             this(defaultLambdaName, defaultLambdaName);
 319         }
 320 
 321         private Kind(String defaultLambdaName, String methodName) {
 322             this.defaultLambdaName = defaultLambdaName;
 323             this.methodName = methodName;
 324         }
 325     }
 326 
 327     // private version that doesn't do checks or defensive copies
 328     private LambdaForm(int arity, int result, boolean forceInline, MethodHandle customized, Name[] names, Kind kind) {
 329         this.arity = arity;
 330         this.result = result;
 331         this.forceInline = forceInline;
 332         this.customized = customized;
 333         this.names = names;
 334         this.kind = kind;
 335         this.vmentry = null;
 336         this.isCompiled = false;
 337     }
 338 
 339     // root factory pre/post processing and calls simple constructor
 340     private static LambdaForm create(int arity, Name[] names, int result, boolean forceInline, MethodHandle customized, Kind kind) {
 341         names = names.clone();
 342         assert(namesOK(arity, names));
 343         result = fixResult(result, names);
 344 
 345         boolean canInterpret = normalizeNames(arity, names);
 346         LambdaForm form = new LambdaForm(arity, result, forceInline, customized, names, kind);
 347         assert(form.nameRefsAreLegal());
 348         if (!canInterpret) {
 349             form.compileToBytecode();
 350         }
 351         return form;
 352     }
 353 
 354     // derived factories with defaults
 355     private static final int DEFAULT_RESULT = LAST_RESULT;
 356     private static final boolean DEFAULT_FORCE_INLINE = true;
 357     private static final MethodHandle DEFAULT_CUSTOMIZED = null;
 358     private static final Kind DEFAULT_KIND = Kind.GENERIC;
 359 
 360     static LambdaForm create(int arity, Name[] names, int result) {
 361         return create(arity, names, result, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, DEFAULT_KIND);
 362     }
 363     static LambdaForm create(int arity, Name[] names, int result, Kind kind) {
 364         return create(arity, names, result, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, kind);
 365     }
 366     static LambdaForm create(int arity, Name[] names) {
 367         return create(arity, names, DEFAULT_RESULT, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, DEFAULT_KIND);
 368     }
 369     static LambdaForm create(int arity, Name[] names, Kind kind) {
 370         return create(arity, names, DEFAULT_RESULT, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, kind);
 371     }
 372     static LambdaForm create(int arity, Name[] names, boolean forceInline, Kind kind) {
 373         return create(arity, names, DEFAULT_RESULT, forceInline, DEFAULT_CUSTOMIZED, kind);
 374     }
 375 
 376     private static LambdaForm createBlankForType(MethodType mt) {
 377         // Make a blank lambda form, which returns a constant zero or null.
 378         // It is used as a template for managing the invocation of similar forms that are non-empty.
 379         // Called only from getPreparedForm.
 380         int arity = mt.parameterCount();
 381         int result = (mt.returnType() == void.class || mt.returnType() == Void.class) ? VOID_RESULT : arity;
 382         Name[] names = buildEmptyNames(arity, mt, result == VOID_RESULT);
 383         boolean canInterpret = normalizeNames(arity, names);
 384         LambdaForm form = new LambdaForm(arity, result, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, names, Kind.ZERO);
 385         assert(form.nameRefsAreLegal() && form.isEmpty() && isValidSignature(form.basicTypeSignature()));
 386         if (!canInterpret) {
 387             form.compileToBytecode();
 388         }
 389         return form;
 390     }
 391 
 392     private static Name[] buildEmptyNames(int arity, MethodType mt, boolean isVoid) {
 393         Name[] names = arguments(isVoid ? 0 : 1, mt);
 394         if (!isVoid) {
 395             Name zero = new Name(constantZero(basicType(mt.returnType())));
 396             names[arity] = zero.newIndex(arity);
 397         }
 398         assert(namesOK(arity, names));
 399         return names;
 400     }
 401 
 402     private static int fixResult(int result, Name[] names) {
 403         if (result == LAST_RESULT)
 404             result = names.length - 1;  // might still be void
 405         if (result >= 0 && names[result].type == V_TYPE)
 406             result = VOID_RESULT;
 407         return result;
 408     }
 409 
 410     static boolean debugNames() {
 411         return DEBUG_NAME_COUNTERS != null;
 412     }
 413 
 414     static void associateWithDebugName(LambdaForm form, String name) {
 415         assert (debugNames());
 416         synchronized (DEBUG_NAMES) {
 417             DEBUG_NAMES.put(form, name);
 418         }
 419     }
 420 
 421     String lambdaName() {
 422         if (DEBUG_NAMES != null) {
 423             synchronized (DEBUG_NAMES) {
 424                 String name = DEBUG_NAMES.get(this);
 425                 if (name == null) {
 426                     name = generateDebugName();
 427                 }
 428                 return name;
 429             }
 430         }
 431         return kind.defaultLambdaName;
 432     }
 433 
 434     private String generateDebugName() {
 435         assert (debugNames());
 436         String debugNameStem = kind.defaultLambdaName;
 437         Integer ctr = DEBUG_NAME_COUNTERS.getOrDefault(debugNameStem, 0);
 438         DEBUG_NAME_COUNTERS.put(debugNameStem, ctr + 1);
 439         StringBuilder buf = new StringBuilder(debugNameStem);
 440         int leadingZero = buf.length();
 441         buf.append((int) ctr);
 442         for (int i = buf.length() - leadingZero; i < 3; i++) {
 443             buf.insert(leadingZero, '0');
 444         }
 445         buf.append('_');
 446         buf.append(basicTypeSignature());
 447         String name = buf.toString();
 448         associateWithDebugName(this, name);
 449         return name;
 450     }
 451 
 452     private static boolean namesOK(int arity, Name[] names) {
 453         for (int i = 0; i < names.length; i++) {
 454             Name n = names[i];
 455             assert(n != null) : "n is null";
 456             if (i < arity)
 457                 assert( n.isParam()) : n + " is not param at " + i;
 458             else
 459                 assert(!n.isParam()) : n + " is param at " + i;
 460         }
 461         return true;
 462     }
 463 
 464     /** Customize LambdaForm for a particular MethodHandle */
 465     LambdaForm customize(MethodHandle mh) {
 466         if (customized == mh) {
 467             return this;
 468         }
 469         LambdaForm customForm = LambdaForm.create(arity, names, result, forceInline, mh, kind);
 470         if (COMPILE_THRESHOLD >= 0 && isCompiled) {
 471             // If shared LambdaForm has been compiled, compile customized version as well.
 472             customForm.compileToBytecode();
 473         }
 474         customForm.transformCache = this; // LambdaFormEditor should always use uncustomized form.
 475         return customForm;
 476     }
 477 
 478     /** Get uncustomized flavor of the LambdaForm */
 479     LambdaForm uncustomize() {
 480         if (customized == null) {
 481             return this;
 482         }
 483         assert(transformCache != null); // Customized LambdaForm should always has a link to uncustomized version.
 484         LambdaForm uncustomizedForm = (LambdaForm)transformCache;
 485         if (COMPILE_THRESHOLD >= 0 && isCompiled) {
 486             // If customized LambdaForm has been compiled, compile uncustomized version as well.
 487             uncustomizedForm.compileToBytecode();
 488         }
 489         return uncustomizedForm;
 490     }
 491 
 492     /** Renumber and/or replace params so that they are interned and canonically numbered.
 493      *  @return true if we can interpret
 494      */
 495     private static boolean normalizeNames(int arity, Name[] names) {
 496         Name[] oldNames = null;
 497         int maxOutArity = 0;
 498         int changesStart = 0;
 499         for (int i = 0; i < names.length; i++) {
 500             Name n = names[i];
 501             if (!n.initIndex(i)) {
 502                 if (oldNames == null) {
 503                     oldNames = names.clone();
 504                     changesStart = i;
 505                 }
 506                 names[i] = n.cloneWithIndex(i);
 507             }
 508             if (n.arguments != null && maxOutArity < n.arguments.length)
 509                 maxOutArity = n.arguments.length;
 510         }
 511         if (oldNames != null) {
 512             int startFixing = arity;
 513             if (startFixing <= changesStart)
 514                 startFixing = changesStart+1;
 515             for (int i = startFixing; i < names.length; i++) {
 516                 Name fixed = names[i].replaceNames(oldNames, names, changesStart, i);
 517                 names[i] = fixed.newIndex(i);
 518             }
 519         }
 520         int maxInterned = Math.min(arity, INTERNED_ARGUMENT_LIMIT);
 521         boolean needIntern = false;
 522         for (int i = 0; i < maxInterned; i++) {
 523             Name n = names[i], n2 = internArgument(n);
 524             if (n != n2) {
 525                 names[i] = n2;
 526                 needIntern = true;
 527             }
 528         }
 529         if (needIntern) {
 530             for (int i = arity; i < names.length; i++) {
 531                 names[i].internArguments();
 532             }
 533         }
 534 
 535         // return true if we can interpret
 536         if (maxOutArity > MethodType.MAX_MH_INVOKER_ARITY) {
 537             // Cannot use LF interpreter on very high arity expressions.
 538             assert(maxOutArity <= MethodType.MAX_JVM_ARITY);
 539             return false;
 540         }
 541         return true;
 542     }
 543 
 544     /**
 545      * Check that all embedded Name references are localizable to this lambda,
 546      * and are properly ordered after their corresponding definitions.
 547      * <p>
 548      * Note that a Name can be local to multiple lambdas, as long as
 549      * it possesses the same index in each use site.
 550      * This allows Name references to be freely reused to construct
 551      * fresh lambdas, without confusion.
 552      */
 553     boolean nameRefsAreLegal() {
 554         assert(arity >= 0 && arity <= names.length);
 555         assert(result >= -1 && result < names.length);
 556         // Do all names possess an index consistent with their local definition order?
 557         for (int i = 0; i < arity; i++) {
 558             Name n = names[i];
 559             assert(n.index() == i) : Arrays.asList(n.index(), i);
 560             assert(n.isParam());
 561         }
 562         // Also, do all local name references
 563         for (int i = arity; i < names.length; i++) {
 564             Name n = names[i];
 565             assert(n.index() == i);
 566             for (Object arg : n.arguments) {
 567                 if (arg instanceof Name n2) {
 568                     int i2 = n2.index;
 569                     assert(0 <= i2 && i2 < names.length) : n.debugString() + ": 0 <= i2 && i2 < names.length: 0 <= " + i2 + " < " + names.length;
 570                     assert(names[i2] == n2) : Arrays.asList("-1-", i, "-2-", n.debugString(), "-3-", i2, "-4-", n2.debugString(), "-5-", names[i2].debugString(), "-6-", this);
 571                     assert(i2 < i);  // ref must come after def!
 572                 }
 573             }
 574         }
 575         return true;
 576     }
 577 
 578     /** Invoke this form on the given arguments. */
 579     // final Object invoke(Object... args) throws Throwable {
 580     //     // NYI: fit this into the fast path?
 581     //     return interpretWithArguments(args);
 582     // }
 583 
 584     /** Report the return type. */
 585     BasicType returnType() {
 586         if (result < 0)  return V_TYPE;
 587         Name n = names[result];
 588         return n.type;
 589     }
 590 
 591     /** Report the N-th argument type. */
 592     BasicType parameterType(int n) {
 593         return parameter(n).type;
 594     }
 595 
 596     /** Report the N-th argument name. */
 597     Name parameter(int n) {
 598         Name param = names[n];
 599         assert(n < arity && param.isParam());
 600         return param;
 601     }
 602 
 603     /** Report the N-th argument type constraint. */
 604     Object parameterConstraint(int n) {
 605         return parameter(n).constraint;
 606     }
 607 
 608     /** Report the arity. */
 609     int arity() {
 610         return arity;
 611     }
 612 
 613     /** Report the number of expressions (non-parameter names). */
 614     int expressionCount() {
 615         return names.length - arity;
 616     }
 617 
 618     /** Return the method type corresponding to my basic type signature. */
 619     MethodType methodType() {
 620         Class<?>[] ptypes = new Class<?>[arity];
 621         for (int i = 0; i < arity; ++i) {
 622             ptypes[i] = parameterType(i).btClass;
 623         }
 624         return MethodType.methodType(returnType().btClass, ptypes, true);
 625     }
 626 
 627     /** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
 628     final String basicTypeSignature() {
 629         StringBuilder buf = new StringBuilder(arity() + 3);
 630         for (int i = 0, a = arity(); i < a; i++)
 631             buf.append(parameterType(i).basicTypeChar());
 632         return buf.append('_').append(returnType().basicTypeChar()).toString();
 633     }
 634     static int signatureArity(String sig) {
 635         assert(isValidSignature(sig));
 636         return sig.indexOf('_');
 637     }
 638     static boolean isValidSignature(String sig) {
 639         int arity = sig.indexOf('_');
 640         if (arity < 0)  return false;  // must be of the form *_*
 641         int siglen = sig.length();
 642         if (siglen != arity + 2)  return false;  // *_X
 643         for (int i = 0; i < siglen; i++) {
 644             if (i == arity)  continue;  // skip '_'
 645             char c = sig.charAt(i);
 646             if (c == 'V')
 647                 return (i == siglen - 1 && arity == siglen - 2);
 648             if (!isArgBasicTypeChar(c))  return false; // must be [LIJFD]
 649         }
 650         return true;  // [LIJFD]*_[LIJFDV]
 651     }
 652 
 653     /**
 654      * Check if i-th name is a call to MethodHandleImpl.selectAlternative.
 655      */
 656     boolean isSelectAlternative(int pos) {
 657         // selectAlternative idiom:
 658         //   t_{n}:L=MethodHandleImpl.selectAlternative(...)
 659         //   t_{n+1}:?=MethodHandle.invokeBasic(t_{n}, ...)
 660         if (pos+1 >= names.length)  return false;
 661         Name name0 = names[pos];
 662         Name name1 = names[pos+1];
 663         return name0.refersTo(MethodHandleImpl.class, "selectAlternative") &&
 664                 name1.isInvokeBasic() &&
 665                 name1.lastUseIndex(name0) == 0 && // t_{n+1}:?=MethodHandle.invokeBasic(t_{n}, ...)
 666                 lastUseIndex(name0) == pos+1;     // t_{n} is local: used only in t_{n+1}
 667     }
 668 
 669     private boolean isMatchingIdiom(int pos, String idiomName, int nArgs) {
 670         if (pos+2 >= names.length)  return false;
 671         Name name0 = names[pos];
 672         Name name1 = names[pos+1];
 673         Name name2 = names[pos+2];
 674         return name1.refersTo(MethodHandleImpl.class, idiomName) &&
 675                 name0.isInvokeBasic() &&
 676                 name2.isInvokeBasic() &&
 677                 name1.lastUseIndex(name0) == nArgs && // t_{n+1}:L=MethodHandleImpl.<invoker>(<args>, t_{n});
 678                 lastUseIndex(name0) == pos+1 &&       // t_{n} is local: used only in t_{n+1}
 679                 name2.lastUseIndex(name1) == 1 &&     // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 680                 lastUseIndex(name1) == pos+2;         // t_{n+1} is local: used only in t_{n+2}
 681     }
 682 
 683     /**
 684      * Check if i-th name is a start of GuardWithCatch idiom.
 685      */
 686     boolean isGuardWithCatch(int pos) {
 687         // GuardWithCatch idiom:
 688         //   t_{n}:L=MethodHandle.invokeBasic(...)
 689         //   t_{n+1}:L=MethodHandleImpl.guardWithCatch(*, *, *, t_{n});
 690         //   t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 691         return isMatchingIdiom(pos, "guardWithCatch", 3);
 692     }
 693 
 694     /**
 695      * Check if i-th name is a start of the tryFinally idiom.
 696      */
 697     boolean isTryFinally(int pos) {
 698         // tryFinally idiom:
 699         //   t_{n}:L=MethodHandle.invokeBasic(...)
 700         //   t_{n+1}:L=MethodHandleImpl.tryFinally(*, *, t_{n})
 701         //   t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 702         return isMatchingIdiom(pos, "tryFinally", 2);
 703     }
 704 
 705     /**
 706      * Check if i-th name is a start of the tableSwitch idiom.
 707      */
 708     boolean isTableSwitch(int pos) {
 709         // tableSwitch idiom:
 710         //   t_{n}:L=MethodHandle.invokeBasic(...)     // args
 711         //   t_{n+1}:L=MethodHandleImpl.tableSwitch(*, *, *, t_{n})
 712         //   t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 713         if (pos + 2 >= names.length)  return false;
 714 
 715         final int POS_COLLECT_ARGS = pos;
 716         final int POS_TABLE_SWITCH = pos + 1;
 717         final int POS_UNBOX_RESULT = pos + 2;
 718 
 719         Name collectArgs = names[POS_COLLECT_ARGS];
 720         Name tableSwitch = names[POS_TABLE_SWITCH];
 721         Name unboxResult = names[POS_UNBOX_RESULT];
 722         return tableSwitch.refersTo(MethodHandleImpl.class, "tableSwitch") &&
 723                 collectArgs.isInvokeBasic() &&
 724                 unboxResult.isInvokeBasic() &&
 725                 tableSwitch.lastUseIndex(collectArgs) == 3 &&     // t_{n+1}:L=MethodHandleImpl.<invoker>(*, *, *, t_{n});
 726                 lastUseIndex(collectArgs) == POS_TABLE_SWITCH &&  // t_{n} is local: used only in t_{n+1}
 727                 unboxResult.lastUseIndex(tableSwitch) == 1 &&     // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 728                 lastUseIndex(tableSwitch) == POS_UNBOX_RESULT;    // t_{n+1} is local: used only in t_{n+2}
 729     }
 730 
 731     /**
 732      * Check if i-th name is a start of the loop idiom.
 733      */
 734     boolean isLoop(int pos) {
 735         // loop idiom:
 736         //   t_{n}:L=MethodHandle.invokeBasic(...)
 737         //   t_{n+1}:L=MethodHandleImpl.loop(types, *, t_{n})
 738         //   t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
 739         return isMatchingIdiom(pos, "loop", 2);
 740     }
 741 
 742     /*
 743      * Code generation issues:
 744      *
 745      * Compiled LFs should be reusable in general.
 746      * The biggest issue is how to decide when to pull a name into
 747      * the bytecode, versus loading a reified form from the MH data.
 748      *
 749      * For example, an asType wrapper may require execution of a cast
 750      * after a call to a MH.  The target type of the cast can be placed
 751      * as a constant in the LF itself.  This will force the cast type
 752      * to be compiled into the bytecodes and native code for the MH.
 753      * Or, the target type of the cast can be erased in the LF, and
 754      * loaded from the MH data.  (Later on, if the MH as a whole is
 755      * inlined, the data will flow into the inlined instance of the LF,
 756      * as a constant, and the end result will be an optimal cast.)
 757      *
 758      * This erasure of cast types can be done with any use of
 759      * reference types.  It can also be done with whole method
 760      * handles.  Erasing a method handle might leave behind
 761      * LF code that executes correctly for any MH of a given
 762      * type, and load the required MH from the enclosing MH's data.
 763      * Or, the erasure might even erase the expected MT.
 764      *
 765      * Also, for direct MHs, the MemberName of the target
 766      * could be erased, and loaded from the containing direct MH.
 767      * As a simple case, a LF for all int-valued non-static
 768      * field getters would perform a cast on its input argument
 769      * (to non-constant base type derived from the MemberName)
 770      * and load an integer value from the input object
 771      * (at a non-constant offset also derived from the MemberName).
 772      * Such MN-erased LFs would be inlinable back to optimized
 773      * code, whenever a constant enclosing DMH is available
 774      * to supply a constant MN from its data.
 775      *
 776      * The main problem here is to keep LFs reasonably generic,
 777      * while ensuring that hot spots will inline good instances.
 778      * "Reasonably generic" means that we don't end up with
 779      * repeated versions of bytecode or machine code that do
 780      * not differ in their optimized form.  Repeated versions
 781      * of machine would have the undesirable overheads of
 782      * (a) redundant compilation work and (b) extra I$ pressure.
 783      * To control repeated versions, we need to be ready to
 784      * erase details from LFs and move them into MH data,
 785      * whenever those details are not relevant to significant
 786      * optimization.  "Significant" means optimization of
 787      * code that is actually hot.
 788      *
 789      * Achieving this may require dynamic splitting of MHs, by replacing
 790      * a generic LF with a more specialized one, on the same MH,
 791      * if (a) the MH is frequently executed and (b) the MH cannot
 792      * be inlined into a containing caller, such as an invokedynamic.
 793      *
 794      * Compiled LFs that are no longer used should be GC-able.
 795      * If they contain non-BCP references, they should be properly
 796      * interlinked with the class loader(s) that their embedded types
 797      * depend on.  This probably means that reusable compiled LFs
 798      * will be tabulated (indexed) on relevant class loaders,
 799      * or else that the tables that cache them will have weak links.
 800      */
 801 
 802     /**
 803      * Make this LF directly executable, as part of a MethodHandle.
 804      * Invariant:  Every MH which is invoked must prepare its LF
 805      * before invocation.
 806      * (In principle, the JVM could do this very lazily,
 807      * as a sort of pre-invocation linkage step.)
 808      */
 809     public void prepare() {
 810         if (COMPILE_THRESHOLD == 0 && !forceInterpretation() && !isCompiled) {
 811             compileToBytecode();
 812         }
 813         if (this.vmentry != null) {
 814             // already prepared (e.g., a primitive DMH invoker form)
 815             return;
 816         }
 817         MethodType mtype = methodType();
 818         LambdaForm prep = mtype.form().cachedLambdaForm(MethodTypeForm.LF_INTERPRET);
 819         if (prep == null) {
 820             assert (isValidSignature(basicTypeSignature()));
 821             prep = LambdaForm.createBlankForType(mtype);
 822             prep.vmentry = InvokerBytecodeGenerator.generateLambdaFormInterpreterEntryPoint(mtype);
 823             prep = mtype.form().setCachedLambdaForm(MethodTypeForm.LF_INTERPRET, prep);
 824         }
 825         this.vmentry = prep.vmentry;
 826         // TO DO: Maybe add invokeGeneric, invokeWithArguments
 827     }
 828 
 829     private static @Stable PerfCounter LF_FAILED;
 830 
 831     private static PerfCounter failedCompilationCounter() {
 832         if (LF_FAILED == null) {
 833             LF_FAILED = PerfCounter.newPerfCounter("java.lang.invoke.failedLambdaFormCompilations");
 834         }
 835         return LF_FAILED;
 836     }
 837 
 838     /** Generate optimizable bytecode for this form. */
 839     void compileToBytecode() {
 840         if (forceInterpretation()) {
 841             return; // this should not be compiled
 842         }
 843         if (vmentry != null && isCompiled) {
 844             return;  // already compiled somehow
 845         }
 846 
 847         // Obtain the invoker MethodType outside of the following try block.
 848         // This ensures that an IllegalArgumentException is directly thrown if the
 849         // type would have 256 or more parameters
 850         MethodType invokerType = methodType();
 851         assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
 852         try {
 853             vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
 854             if (TRACE_INTERPRETER)
 855                 traceInterpreter("compileToBytecode", this);
 856             isCompiled = true;
 857         } catch (InvokerBytecodeGenerator.BytecodeGenerationException bge) {
 858             // bytecode generation failed - mark this LambdaForm as to be run in interpretation mode only
 859             invocationCounter = -1;
 860             failedCompilationCounter().increment();
 861             if (LOG_LF_COMPILATION_FAILURE) {
 862                 System.out.println("LambdaForm compilation failed: " + this);
 863                 bge.printStackTrace(System.out);
 864             }
 865         } catch (Error e) {
 866             // Pass through any error
 867             throw e;
 868         } catch (Exception e) {
 869             // Wrap any exception
 870             throw newInternalError(this.toString(), e);
 871         }
 872     }
 873 
 874     // The next few routines are called only from assert expressions
 875     // They verify that the built-in invokers process the correct raw data types.
 876     private static boolean argumentTypesMatch(String sig, Object[] av) {
 877         int arity = signatureArity(sig);
 878         assert(av.length == arity) : "av.length == arity: av.length=" + av.length + ", arity=" + arity;
 879         assert(av[0] instanceof MethodHandle) : "av[0] not instance of MethodHandle: " + av[0];
 880         MethodHandle mh = (MethodHandle) av[0];
 881         MethodType mt = mh.type();
 882         assert(mt.parameterCount() == arity-1);
 883         for (int i = 0; i < av.length; i++) {
 884             Class<?> pt = (i == 0 ? MethodHandle.class : mt.parameterType(i-1));
 885             assert(valueMatches(basicType(sig.charAt(i)), pt, av[i]));
 886         }
 887         return true;
 888     }
 889     private static boolean valueMatches(BasicType tc, Class<?> type, Object x) {
 890         // The following line is needed because (...)void method handles can use non-void invokers
 891         if (type == void.class)  tc = V_TYPE;   // can drop any kind of value
 892         assert tc == basicType(type) : tc + " == basicType(" + type + ")=" + basicType(type);
 893         switch (tc) {
 894         case I_TYPE: assert checkInt(type, x)   : "checkInt(" + type + "," + x +")";   break;
 895         case J_TYPE: assert x instanceof Long   : "instanceof Long: " + x;             break;
 896         case F_TYPE: assert x instanceof Float  : "instanceof Float: " + x;            break;
 897         case D_TYPE: assert x instanceof Double : "instanceof Double: " + x;           break;
 898         case L_TYPE: assert checkRef(type, x)   : "checkRef(" + type + "," + x + ")";  break;
 899         case V_TYPE: break;  // allow anything here; will be dropped
 900         default:  assert(false);
 901         }
 902         return true;
 903     }
 904     private static boolean checkInt(Class<?> type, Object x) {
 905         assert(x instanceof Integer);
 906         if (type == int.class)  return true;
 907         Wrapper w = Wrapper.forBasicType(type);
 908         assert(w.isSubwordOrInt());
 909         Object x1 = Wrapper.INT.wrap(w.wrap(x));
 910         return x.equals(x1);
 911     }
 912     private static boolean checkRef(Class<?> type, Object x) {
 913         assert(!type.isPrimitive());
 914         if (x == null)  return true;
 915         if (type.isInterface())  return true;
 916         return type.isInstance(x);
 917     }
 918 
 919     /** If the invocation count hits the threshold we spin bytecodes and call that subsequently. */
 920     private static final int COMPILE_THRESHOLD;
 921     static {
 922         COMPILE_THRESHOLD = Math.max(-1, MethodHandleStatics.COMPILE_THRESHOLD);
 923     }
 924     private int invocationCounter = 0; // a value of -1 indicates LambdaForm interpretation mode forever
 925 
 926     private boolean forceInterpretation() {
 927         return invocationCounter == -1;
 928     }
 929 
 930     @Hidden
 931     @DontInline
 932     /** Interpretively invoke this form on the given arguments. */
 933     Object interpretWithArguments(Object... argumentValues) throws Throwable {
 934         if (TRACE_INTERPRETER)
 935             return interpretWithArgumentsTracing(argumentValues);
 936         checkInvocationCounter();
 937         assert(arityCheck(argumentValues));
 938         Object[] values = Arrays.copyOf(argumentValues, names.length);
 939         for (int i = argumentValues.length; i < values.length; i++) {
 940             values[i] = interpretName(names[i], values);
 941         }
 942         Object rv = (result < 0) ? null : values[result];
 943         assert(resultCheck(argumentValues, rv));
 944         return rv;
 945     }
 946 
 947     @Hidden
 948     @DontInline
 949     /** Evaluate a single Name within this form, applying its function to its arguments. */
 950     Object interpretName(Name name, Object[] values) throws Throwable {
 951         if (TRACE_INTERPRETER)
 952             traceInterpreter("| interpretName", name.debugString(), (Object[]) null);
 953         Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
 954         for (int i = 0; i < arguments.length; i++) {
 955             Object a = arguments[i];
 956             if (a instanceof Name n) {
 957                 int i2 = n.index();
 958                 assert(names[i2] == a);
 959                 a = values[i2];
 960                 arguments[i] = a;
 961             }
 962         }
 963         return name.function.invokeWithArguments(arguments);
 964     }
 965 
 966     private void checkInvocationCounter() {
 967         if (COMPILE_THRESHOLD != 0 &&
 968             !forceInterpretation() && invocationCounter < COMPILE_THRESHOLD) {
 969             invocationCounter++;  // benign race
 970             if (invocationCounter >= COMPILE_THRESHOLD) {
 971                 // Replace vmentry with a bytecode version of this LF.
 972                 compileToBytecode();
 973             }
 974         }
 975     }
 976     Object interpretWithArgumentsTracing(Object... argumentValues) throws Throwable {
 977         traceInterpreter("[ interpretWithArguments", this, argumentValues);
 978         if (!forceInterpretation() && invocationCounter < COMPILE_THRESHOLD) {
 979             int ctr = invocationCounter++;  // benign race
 980             traceInterpreter("| invocationCounter", ctr);
 981             if (invocationCounter >= COMPILE_THRESHOLD) {
 982                 compileToBytecode();
 983             }
 984         }
 985         Object rval;
 986         try {
 987             assert(arityCheck(argumentValues));
 988             Object[] values = Arrays.copyOf(argumentValues, names.length);
 989             for (int i = argumentValues.length; i < values.length; i++) {
 990                 values[i] = interpretName(names[i], values);
 991             }
 992             rval = (result < 0) ? null : values[result];
 993         } catch (Throwable ex) {
 994             traceInterpreter("] throw =>", ex);
 995             throw ex;
 996         }
 997         traceInterpreter("] return =>", rval);
 998         return rval;
 999     }
1000 
1001     static void traceInterpreter(String event, Object obj, Object... args) {
1002         if (TRACE_INTERPRETER) {
1003             System.out.println("LFI: "+event+" "+(obj != null ? obj : "")+(args != null && args.length != 0 ? Arrays.asList(args) : ""));
1004         }
1005     }
1006     static void traceInterpreter(String event, Object obj) {
1007         traceInterpreter(event, obj, (Object[])null);
1008     }
1009     private boolean arityCheck(Object[] argumentValues) {
1010         assert(argumentValues.length == arity) : arity+"!="+Arrays.asList(argumentValues)+".length";
1011         // also check that the leading (receiver) argument is somehow bound to this LF:
1012         assert(argumentValues[0] instanceof MethodHandle) : "not MH: " + argumentValues[0];
1013         MethodHandle mh = (MethodHandle) argumentValues[0];
1014         assert(mh.internalForm() == this);
1015         // note:  argument #0 could also be an interface wrapper, in the future
1016         argumentTypesMatch(basicTypeSignature(), argumentValues);
1017         return true;
1018     }
1019     private boolean resultCheck(Object[] argumentValues, Object result) {
1020         MethodHandle mh = (MethodHandle) argumentValues[0];
1021         MethodType mt = mh.type();
1022         assert(valueMatches(returnType(), mt.returnType(), result));
1023         return true;
1024     }
1025 
1026     private boolean isEmpty() {
1027         if (result < 0)
1028             return (names.length == arity);
1029         else if (result == arity && names.length == arity + 1)
1030             return names[arity].isConstantZero();
1031         else
1032             return false;
1033     }
1034 
1035     public String toString() {
1036         return debugString(-1);
1037     }
1038 
1039     String debugString(int indentLevel) {
1040         String prefix = MethodHandle.debugPrefix(indentLevel);
1041         String lambdaName = lambdaName();
1042         StringBuilder buf = new StringBuilder(lambdaName);
1043         buf.append("=Lambda(");
1044         for (int i = 0; i < names.length; i++) {
1045             if (i == arity)  buf.append(")=>{");
1046             Name n = names[i];
1047             if (i >= arity)  buf.append("\n    ").append(prefix);
1048             buf.append(n.paramString());
1049             if (i < arity) {
1050                 if (i+1 < arity)  buf.append(",");
1051                 continue;
1052             }
1053             buf.append("=").append(n.exprString());
1054             buf.append(";");
1055         }
1056         if (arity == names.length)  buf.append(")=>{");
1057         buf.append(result < 0 ? "void" : names[result]).append("}");
1058         if (TRACE_INTERPRETER) {
1059             // Extra verbosity:
1060             buf.append(":").append(basicTypeSignature());
1061             buf.append("/").append(vmentry);
1062         }
1063         return buf.toString();
1064     }
1065 
1066     @Override
1067     public boolean equals(Object obj) {
1068         return obj instanceof LambdaForm lf && equals(lf);
1069     }
1070     public boolean equals(LambdaForm that) {
1071         if (this.result != that.result)  return false;
1072         return Arrays.equals(this.names, that.names);
1073     }
1074     public int hashCode() {
1075         return result + 31 * Arrays.hashCode(names);
1076     }
1077     LambdaFormEditor editor() {
1078         return LambdaFormEditor.lambdaFormEditor(this);
1079     }
1080 
1081     boolean contains(Name name) {
1082         int pos = name.index();
1083         if (pos >= 0) {
1084             return pos < names.length && name.equals(names[pos]);
1085         }
1086         for (int i = arity; i < names.length; i++) {
1087             if (name.equals(names[i]))
1088                 return true;
1089         }
1090         return false;
1091     }
1092 
1093     static class NamedFunction {
1094         final MemberName member;
1095         private @Stable MethodHandle resolvedHandle;
1096         private @Stable MethodType type;
1097 
1098         NamedFunction(MethodHandle resolvedHandle) {
1099             this(resolvedHandle.internalMemberName(), resolvedHandle);
1100         }
1101         NamedFunction(MemberName member, MethodHandle resolvedHandle) {
1102             this.member = member;
1103             this.resolvedHandle = resolvedHandle;
1104              // The following assert is almost always correct, but will fail for corner cases, such as PrivateInvokeTest.
1105              //assert(!isInvokeBasic(member));
1106         }
1107         NamedFunction(MethodType basicInvokerType) {
1108             assert(basicInvokerType == basicInvokerType.basicType()) : basicInvokerType;
1109             if (basicInvokerType.parameterSlotCount() < MethodType.MAX_MH_INVOKER_ARITY) {
1110                 this.resolvedHandle = basicInvokerType.invokers().basicInvoker();
1111                 this.member = resolvedHandle.internalMemberName();
1112             } else {
1113                 // necessary to pass BigArityTest
1114                 this.member = Invokers.invokeBasicMethod(basicInvokerType);
1115             }
1116             assert(isInvokeBasic(member));
1117         }
1118 
1119         private static boolean isInvokeBasic(MemberName member) {
1120             return member != null &&
1121                    member.getDeclaringClass() == MethodHandle.class &&
1122                   "invokeBasic".equals(member.getName());
1123         }
1124 
1125         // The next 2 constructors are used to break circular dependencies on MH.invokeStatic, etc.
1126         // Any LambdaForm containing such a member is not interpretable.
1127         // This is OK, since all such LFs are prepared with special primitive vmentry points.
1128         // And even without the resolvedHandle, the name can still be compiled and optimized.
1129         NamedFunction(Method method) {
1130             this(new MemberName(method));
1131         }
1132         NamedFunction(MemberName member) {
1133             this(member, null);
1134         }
1135 
1136         MethodHandle resolvedHandle() {
1137             if (resolvedHandle == null)  resolve();
1138             return resolvedHandle;
1139         }
1140 
1141         synchronized void resolve() {
1142             if (resolvedHandle == null) {
1143                 resolvedHandle = DirectMethodHandle.make(member);
1144             }
1145         }
1146 
1147         @Override
1148         public boolean equals(Object other) {
1149             if (this == other) return true;
1150             if (other == null) return false;
1151             return (other instanceof NamedFunction that)
1152                     && this.member != null
1153                     && this.member.equals(that.member);
1154         }
1155 
1156         @Override
1157         public int hashCode() {
1158             if (member != null)
1159                 return member.hashCode();
1160             return super.hashCode();
1161         }
1162 
1163         static final MethodType INVOKER_METHOD_TYPE =
1164             MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1165 
1166         private static MethodHandle computeInvoker(MethodTypeForm typeForm) {
1167             typeForm = typeForm.basicType().form();  // normalize to basic type
1168             MethodHandle mh = typeForm.cachedMethodHandle(MethodTypeForm.MH_NF_INV);
1169             if (mh != null)  return mh;
1170             MemberName invoker = InvokerBytecodeGenerator.generateNamedFunctionInvoker(typeForm);  // this could take a while
1171             mh = DirectMethodHandle.make(invoker);
1172             MethodHandle mh2 = typeForm.cachedMethodHandle(MethodTypeForm.MH_NF_INV);
1173             if (mh2 != null)  return mh2;  // benign race
1174             if (!mh.type().equals(INVOKER_METHOD_TYPE))
1175                 throw newInternalError(mh.debugString());
1176             return typeForm.setCachedMethodHandle(MethodTypeForm.MH_NF_INV, mh);
1177         }
1178 
1179         @Hidden
1180         Object invokeWithArguments(Object... arguments) throws Throwable {
1181             // If we have a cached invoker, call it right away.
1182             // NOTE: The invoker always returns a reference value.
1183             if (TRACE_INTERPRETER)  return invokeWithArgumentsTracing(arguments);
1184             return invoker().invokeBasic(resolvedHandle(), arguments);
1185         }
1186 
1187         @Hidden
1188         Object invokeWithArgumentsTracing(Object[] arguments) throws Throwable {
1189             Object rval;
1190             try {
1191                 traceInterpreter("[ call", this, arguments);
1192                 // resolvedHandle might be uninitialized, ok for tracing
1193                 if (resolvedHandle == null) {
1194                     traceInterpreter("| resolve", this);
1195                     resolvedHandle();
1196                 }
1197                 rval = invoker().invokeBasic(resolvedHandle(), arguments);
1198             } catch (Throwable ex) {
1199                 traceInterpreter("] throw =>", ex);
1200                 throw ex;
1201             }
1202             traceInterpreter("] return =>", rval);
1203             return rval;
1204         }
1205 
1206         private MethodHandle invoker() {
1207             return computeInvoker(methodType().form());
1208         }
1209 
1210         MethodType methodType() {
1211             MethodType type = this.type;
1212             if (type == null) {
1213                 this.type = type = calculateMethodType(member, resolvedHandle);
1214             }
1215             return type;
1216         }
1217 
1218         private static MethodType calculateMethodType(MemberName member, MethodHandle resolvedHandle) {
1219             if (resolvedHandle != null) {
1220                 return resolvedHandle.type();
1221             } else {
1222                 // only for certain internal LFs during bootstrapping
1223                 return member.getInvocationType();
1224             }
1225         }
1226 
1227         MemberName member() {
1228             assert(assertMemberIsConsistent());
1229             return member;
1230         }
1231 
1232         // Called only from assert.
1233         private boolean assertMemberIsConsistent() {
1234             if (resolvedHandle instanceof DirectMethodHandle) {
1235                 MemberName m = resolvedHandle.internalMemberName();
1236                 assert(m.equals(member));
1237             }
1238             return true;
1239         }
1240 
1241         Class<?> memberDeclaringClassOrNull() {
1242             return (member == null) ? null : member.getDeclaringClass();
1243         }
1244 
1245         BasicType returnType() {
1246             return basicType(methodType().returnType());
1247         }
1248 
1249         BasicType parameterType(int n) {
1250             return basicType(methodType().parameterType(n));
1251         }
1252 
1253         int arity() {
1254             return methodType().parameterCount();
1255         }
1256 
1257         public String toString() {
1258             if (member == null)  return String.valueOf(resolvedHandle);
1259             return member.getDeclaringClass().getSimpleName()+"."+member.getName();
1260         }
1261 
1262         public boolean isIdentity() {
1263             return this.equals(identity(returnType()));
1264         }
1265 
1266         public boolean isConstantZero() {
1267             return this.equals(constantZero(returnType()));
1268         }
1269 
1270         public MethodHandleImpl.Intrinsic intrinsicName() {
1271             return resolvedHandle != null
1272                 ? resolvedHandle.intrinsicName()
1273                 : MethodHandleImpl.Intrinsic.NONE;
1274         }
1275 
1276         public Object intrinsicData() {
1277             return resolvedHandle != null
1278                 ? resolvedHandle.intrinsicData()
1279                 : null;
1280         }
1281     }
1282 
1283     public static String basicTypeSignature(MethodType type) {
1284         int params = type.parameterCount();
1285         char[] sig = new char[params + 2];
1286         int sigp = 0;
1287         while (sigp < params) {
1288             sig[sigp] = basicTypeChar(type.parameterType(sigp++));
1289         }
1290         sig[sigp++] = '_';
1291         sig[sigp++] = basicTypeChar(type.returnType());
1292         assert(sigp == sig.length);
1293         return String.valueOf(sig);
1294     }
1295 
1296     /** Hack to make signatures more readable when they show up in method names.
1297      * Signature should start with a sequence of uppercase ASCII letters.
1298      * Runs of three or more are replaced by a single letter plus a decimal repeat count.
1299      * A tail of anything other than uppercase ASCII is passed through unchanged.
1300      * @param signature sequence of uppercase ASCII letters with possible repetitions
1301      * @return same sequence, with repetitions counted by decimal numerals
1302      */
1303     public static String shortenSignature(String signature) {
1304         final int NO_CHAR = -1, MIN_RUN = 3;
1305         int c0, c1 = NO_CHAR, c1reps = 0;
1306         StringBuilder buf = null;
1307         int len = signature.length();
1308         if (len < MIN_RUN)  return signature;
1309         for (int i = 0; i <= len; i++) {
1310             if (c1 != NO_CHAR && !('A' <= c1 && c1 <= 'Z')) {
1311                 // wrong kind of char; bail out here
1312                 if (buf != null) {
1313                     buf.append(signature, i - c1reps, len);
1314                 }
1315                 break;
1316             }
1317             // shift in the next char:
1318             c0 = c1; c1 = (i == len ? NO_CHAR : signature.charAt(i));
1319             if (c1 == c0) { ++c1reps; continue; }
1320             // shift in the next count:
1321             int c0reps = c1reps; c1reps = 1;
1322             // end of a  character run
1323             if (c0reps < MIN_RUN) {
1324                 if (buf != null) {
1325                     while (--c0reps >= 0)
1326                         buf.append((char)c0);
1327                 }
1328                 continue;
1329             }
1330             // found three or more in a row
1331             if (buf == null)
1332                 buf = new StringBuilder().append(signature, 0, i - c0reps);
1333             buf.append((char)c0).append(c0reps);
1334         }
1335         return (buf == null) ? signature : buf.toString();
1336     }
1337 
1338     static final class Name {
1339         final BasicType type;
1340         @Stable short index;
1341         final NamedFunction function;
1342         final Object constraint;  // additional type information, if not null
1343         @Stable final Object[] arguments;
1344 
1345         private static final Object[] EMPTY_ARGS = new Object[0];
1346 
1347         private Name(int index, BasicType type, NamedFunction function, Object[] arguments) {
1348             this.index = (short)index;
1349             this.type = type;
1350             this.function = function;
1351             this.arguments = arguments;
1352             this.constraint = null;
1353             assert(this.index == index && typesMatch(function, this.arguments));
1354         }
1355         private Name(Name that, Object constraint) {
1356             this.index = that.index;
1357             this.type = that.type;
1358             this.function = that.function;
1359             this.arguments = that.arguments;
1360             this.constraint = constraint;
1361             assert(constraint == null || isParam());  // only params have constraints
1362             assert(constraint == null || constraint instanceof ClassSpecializer.SpeciesData || constraint instanceof Class);
1363         }
1364         Name(MethodHandle function, Object... arguments) {
1365             this(new NamedFunction(function), arguments);
1366         }
1367         Name(MethodType functionType, Object... arguments) {
1368             this(new NamedFunction(functionType), arguments);
1369             assert(arguments[0] instanceof Name name && name.type == L_TYPE);
1370         }
1371         Name(MemberName function, Object... arguments) {
1372             this(new NamedFunction(function), arguments);
1373         }
1374         Name(NamedFunction function) {
1375             this(-1, function.returnType(), function, EMPTY_ARGS);
1376         }
1377         Name(NamedFunction function, Object arg) {
1378             this(-1, function.returnType(), function, new Object[] { arg });
1379         }
1380         Name(NamedFunction function, Object arg0, Object arg1) {
1381             this(-1, function.returnType(), function, new Object[] { arg0, arg1 });
1382         }
1383         Name(NamedFunction function, Object... arguments) {
1384             this(-1, function.returnType(), function, Arrays.copyOf(arguments, arguments.length, Object[].class));
1385         }
1386         /** Create a raw parameter of the given type, with an expected index. */
1387         Name(int index, BasicType type) {
1388             this(index, type, null, null);
1389         }
1390         /** Create a raw parameter of the given type. */
1391         Name(BasicType type) { this(-1, type); }
1392 
1393         BasicType type() { return type; }
1394         int index() { return index; }
1395         boolean initIndex(int i) {
1396             if (index != i) {
1397                 if (index != -1)  return false;
1398                 index = (short)i;
1399             }
1400             return true;
1401         }
1402         char typeChar() {
1403             return type.btChar;
1404         }
1405 
1406         Name newIndex(int i) {
1407             if (initIndex(i))  return this;
1408             return cloneWithIndex(i);
1409         }
1410         Name cloneWithIndex(int i) {
1411             Object[] newArguments = (arguments == null) ? null : arguments.clone();
1412             return new Name(i, type, function, newArguments).withConstraint(constraint);
1413         }
1414         Name withConstraint(Object constraint) {
1415             if (constraint == this.constraint)  return this;
1416             return new Name(this, constraint);
1417         }
1418         Name replaceName(Name oldName, Name newName) {  // FIXME: use replaceNames uniformly
1419             if (oldName == newName)  return this;
1420             @SuppressWarnings("LocalVariableHidesMemberVariable")
1421             Object[] arguments = this.arguments;
1422             if (arguments == null)  return this;
1423             boolean replaced = false;
1424             for (int j = 0; j < arguments.length; j++) {
1425                 if (arguments[j] == oldName) {
1426                     if (!replaced) {
1427                         replaced = true;
1428                         arguments = arguments.clone();
1429                     }
1430                     arguments[j] = newName;
1431                 }
1432             }
1433             if (!replaced)  return this;
1434             return new Name(function, arguments);
1435         }
1436         /** In the arguments of this Name, replace oldNames[i] pairwise by newNames[i].
1437          *  Limit such replacements to {@code start<=i<end}.  Return possibly changed self.
1438          */
1439         Name replaceNames(Name[] oldNames, Name[] newNames, int start, int end) {
1440             if (start >= end)  return this;
1441             @SuppressWarnings("LocalVariableHidesMemberVariable")
1442             Object[] arguments = this.arguments;
1443             boolean replaced = false;
1444         eachArg:
1445             for (int j = 0; j < arguments.length; j++) {
1446                 if (arguments[j] instanceof Name n) {
1447                     int check = n.index;
1448                     // harmless check to see if the thing is already in newNames:
1449                     if (check >= 0 && check < newNames.length && n == newNames[check])
1450                         continue eachArg;
1451                     // n might not have the correct index: n != oldNames[n.index].
1452                     for (int i = start; i < end; i++) {
1453                         if (n == oldNames[i]) {
1454                             if (n == newNames[i])
1455                                 continue eachArg;
1456                             if (!replaced) {
1457                                 replaced = true;
1458                                 arguments = arguments.clone();
1459                             }
1460                             arguments[j] = newNames[i];
1461                             continue eachArg;
1462                         }
1463                     }
1464                 }
1465             }
1466             if (!replaced)  return this;
1467             return new Name(function, arguments);
1468         }
1469         void internArguments() {
1470             @SuppressWarnings("LocalVariableHidesMemberVariable")
1471             Object[] arguments = this.arguments;
1472             for (int j = 0; j < arguments.length; j++) {
1473                 if (arguments[j] instanceof Name n) {
1474                     if (n.isParam() && n.index < INTERNED_ARGUMENT_LIMIT)
1475                         arguments[j] = internArgument(n);
1476                 }
1477             }
1478         }
1479         boolean isParam() {
1480             return function == null;
1481         }
1482         boolean isConstantZero() {
1483             return !isParam() && arguments.length == 0 && function.isConstantZero();
1484         }
1485 
1486         boolean refersTo(Class<?> declaringClass, String methodName) {
1487             return function != null &&
1488                     function.member() != null && function.member().refersTo(declaringClass, methodName);
1489         }
1490 
1491         /**
1492          * Check if MemberName is a call to MethodHandle.invokeBasic.
1493          */
1494         boolean isInvokeBasic() {
1495             if (function == null)
1496                 return false;
1497             if (arguments.length < 1)
1498                 return false;  // must have MH argument
1499             MemberName member = function.member();
1500             return member != null && member.refersTo(MethodHandle.class, "invokeBasic") &&
1501                     !member.isPublic() && !member.isStatic();
1502         }
1503 
1504         /**
1505          * Check if MemberName is a call to MethodHandle.linkToStatic, etc.
1506          */
1507         boolean isLinkerMethodInvoke() {
1508             if (function == null)
1509                 return false;
1510             if (arguments.length < 1)
1511                 return false;  // must have MH argument
1512             MemberName member = function.member();
1513             return member != null &&
1514                     member.getDeclaringClass() == MethodHandle.class &&
1515                     !member.isPublic() && member.isStatic() &&
1516                     member.getName().startsWith("linkTo");
1517         }
1518 
1519         public String toString() {
1520             return (isParam()?"a":"t")+(index >= 0 ? index : System.identityHashCode(this))+":"+typeChar();
1521         }
1522         public String debugString() {
1523             String s = paramString();
1524             return (function == null) ? s : s + "=" + exprString();
1525         }
1526         public String paramString() {
1527             String s = toString();
1528             Object c = constraint;
1529             if (c == null)
1530                 return s;
1531             if (c instanceof Class<?> cl)  c = cl.getSimpleName();
1532             return s + "/" + c;
1533         }
1534         public String exprString() {
1535             if (function == null)  return toString();
1536             StringBuilder buf = new StringBuilder(function.toString());
1537             buf.append("(");
1538             String cma = "";
1539             for (Object a : arguments) {
1540                 buf.append(cma); cma = ",";
1541                 if (a instanceof Name || a instanceof Integer)
1542                     buf.append(a);
1543                 else
1544                     buf.append("(").append(a).append(")");
1545             }
1546             buf.append(")");
1547             return buf.toString();
1548         }
1549 
1550         private boolean typesMatch(NamedFunction function, Object ... arguments) {
1551             if (arguments == null) {
1552                 assert(function == null);
1553                 return true;
1554             }
1555             assert(arguments.length == function.arity()) : "arity mismatch: arguments.length=" + arguments.length + " == function.arity()=" + function.arity() + " in " + debugString();
1556             for (int i = 0; i < arguments.length; i++) {
1557                 assert (typesMatch(function.parameterType(i), arguments[i])) : "types don't match: function.parameterType(" + i + ")=" + function.parameterType(i) + ", arguments[" + i + "]=" + arguments[i] + " in " + debugString();
1558             }
1559             return true;
1560         }
1561 
1562         private static boolean typesMatch(BasicType parameterType, Object object) {
1563             if (object instanceof Name name) {
1564                 return name.type == parameterType;
1565             }
1566             switch (parameterType) {
1567                 case I_TYPE:  return object instanceof Integer;
1568                 case J_TYPE:  return object instanceof Long;
1569                 case F_TYPE:  return object instanceof Float;
1570                 case D_TYPE:  return object instanceof Double;
1571             }
1572             assert(parameterType == L_TYPE);
1573             return true;
1574         }
1575 
1576         /** Return the index of the last occurrence of n in the argument array.
1577          *  Return -1 if the name is not used.
1578          */
1579         int lastUseIndex(Name n) {
1580             if (arguments == null)  return -1;
1581             for (int i = arguments.length; --i >= 0; ) {
1582                 if (arguments[i] == n)  return i;
1583             }
1584             return -1;
1585         }
1586 
1587         /** Return the number of occurrences of n in the argument array.
1588          *  Return 0 if the name is not used.
1589          */
1590         int useCount(Name n) {
1591             int count = 0;
1592             if (arguments != null) {
1593                 for (Object argument : arguments) {
1594                     if (argument == n) {
1595                         count++;
1596                     }
1597                 }
1598             }
1599             return count;
1600         }
1601 
1602         public boolean equals(Name that) {
1603             if (this == that)  return true;
1604             if (isParam())
1605                 // each parameter is a unique atom
1606                 return false;  // this != that
1607             return
1608                 //this.index == that.index &&
1609                 this.type == that.type &&
1610                 this.function.equals(that.function) &&
1611                 Arrays.equals(this.arguments, that.arguments);
1612         }
1613         @Override
1614         public boolean equals(Object x) {
1615             return x instanceof Name n && equals(n);
1616         }
1617         @Override
1618         public int hashCode() {
1619             if (isParam())
1620                 return index | (type.ordinal() << 8);
1621             return function.hashCode() ^ Arrays.hashCode(arguments);
1622         }
1623     }
1624 
1625     /** Return the index of the last name which contains n as an argument.
1626      *  Return -1 if the name is not used.  Return names.length if it is the return value.
1627      */
1628     int lastUseIndex(Name n) {
1629         int ni = n.index, nmax = names.length;
1630         assert(names[ni] == n);
1631         if (result == ni)  return nmax;  // live all the way beyond the end
1632         for (int i = nmax; --i > ni; ) {
1633             if (names[i].lastUseIndex(n) >= 0)
1634                 return i;
1635         }
1636         return -1;
1637     }
1638 
1639     /** Return the number of times n is used as an argument or return value. */
1640     int useCount(Name n) {
1641         int count = (result == n.index) ? 1 : 0;
1642         int i = Math.max(n.index + 1, arity);
1643         while (i < names.length) {
1644             count += names[i++].useCount(n);
1645         }
1646         return count;
1647     }
1648 
1649     static Name argument(int which, BasicType type) {
1650         if (which >= INTERNED_ARGUMENT_LIMIT)
1651             return new Name(which, type);
1652         return INTERNED_ARGUMENTS[type.ordinal()][which];
1653     }
1654     static Name internArgument(Name n) {
1655         assert(n.isParam()) : "not param: " + n;
1656         assert(n.index < INTERNED_ARGUMENT_LIMIT);
1657         if (n.constraint != null)  return n;
1658         return argument(n.index, n.type);
1659     }
1660     static Name[] arguments(int extra, MethodType types) {
1661         int length = types.parameterCount();
1662         Name[] names = new Name[length + extra];
1663         for (int i = 0; i < length; i++)
1664             names[i] = argument(i, basicType(types.parameterType(i)));
1665         return names;
1666     }
1667     static final int INTERNED_ARGUMENT_LIMIT = 10;
1668     private static final Name[][] INTERNED_ARGUMENTS
1669             = new Name[ARG_TYPE_LIMIT][INTERNED_ARGUMENT_LIMIT];
1670     static {
1671         for (BasicType type : BasicType.ARG_TYPES) {
1672             int ord = type.ordinal();
1673             for (int i = 0; i < INTERNED_ARGUMENTS[ord].length; i++) {
1674                 INTERNED_ARGUMENTS[ord][i] = new Name(i, type);
1675             }
1676         }
1677     }
1678 
1679     private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
1680 
1681     static LambdaForm identityForm(BasicType type) {
1682         int ord = type.ordinal();
1683         LambdaForm form = LF_identity[ord];
1684         if (form != null) {
1685             return form;
1686         }
1687         createFormsFor(type);
1688         return LF_identity[ord];
1689     }
1690 
1691     static LambdaForm zeroForm(BasicType type) {
1692         int ord = type.ordinal();
1693         LambdaForm form = LF_zero[ord];
1694         if (form != null) {
1695             return form;
1696         }
1697         createFormsFor(type);
1698         return LF_zero[ord];
1699     }
1700 
1701     static NamedFunction identity(BasicType type) {
1702         int ord = type.ordinal();
1703         NamedFunction function = NF_identity[ord];
1704         if (function != null) {
1705             return function;
1706         }
1707         createFormsFor(type);
1708         return NF_identity[ord];
1709     }
1710 
1711     static NamedFunction constantZero(BasicType type) {
1712         int ord = type.ordinal();
1713         NamedFunction function = NF_zero[ord];
1714         if (function != null) {
1715             return function;
1716         }
1717         createFormsFor(type);
1718         return NF_zero[ord];
1719     }
1720 
1721     private static final @Stable LambdaForm[] LF_identity = new LambdaForm[TYPE_LIMIT];
1722     private static final @Stable LambdaForm[] LF_zero = new LambdaForm[TYPE_LIMIT];
1723     private static final @Stable NamedFunction[] NF_identity = new NamedFunction[TYPE_LIMIT];
1724     private static final @Stable NamedFunction[] NF_zero = new NamedFunction[TYPE_LIMIT];
1725 
1726     private static final Object createFormsLock = new Object();
1727     private static void createFormsFor(BasicType type) {
1728         // Avoid racy initialization during bootstrap
1729         UNSAFE.ensureClassInitialized(BoundMethodHandle.class);
1730         synchronized (createFormsLock) {
1731             final int ord = type.ordinal();
1732             LambdaForm idForm = LF_identity[ord];
1733             if (idForm != null) {
1734                 return;
1735             }
1736             char btChar = type.basicTypeChar();
1737             boolean isVoid = (type == V_TYPE);
1738             Class<?> btClass = type.btClass;
1739             MethodType zeType = MethodType.methodType(btClass);
1740             MethodType idType = (isVoid) ? zeType : MethodType.methodType(btClass, btClass);
1741 
1742             // Look up symbolic names.  It might not be necessary to have these,
1743             // but if we need to emit direct references to bytecodes, it helps.
1744             // Zero is built from a call to an identity function with a constant zero input.
1745             MemberName idMem = new MemberName(LambdaForm.class, "identity_"+btChar, idType, REF_invokeStatic);
1746             MemberName zeMem = null;
1747             try {
1748                 idMem = IMPL_NAMES.resolveOrFail(REF_invokeStatic, idMem, null, LM_TRUSTED, NoSuchMethodException.class);
1749                 if (!isVoid) {
1750                     zeMem = new MemberName(LambdaForm.class, "zero_"+btChar, zeType, REF_invokeStatic);
1751                     zeMem = IMPL_NAMES.resolveOrFail(REF_invokeStatic, zeMem, null, LM_TRUSTED, NoSuchMethodException.class);
1752                 }
1753             } catch (IllegalAccessException|NoSuchMethodException ex) {
1754                 throw newInternalError(ex);
1755             }
1756 
1757             NamedFunction idFun;
1758             LambdaForm zeForm;
1759             NamedFunction zeFun;
1760 
1761             // Create the LFs and NamedFunctions. Precompiling LFs to byte code is needed to break circular
1762             // bootstrap dependency on this method in case we're interpreting LFs
1763             if (isVoid) {
1764                 Name[] idNames = new Name[] { argument(0, L_TYPE) };
1765                 idForm = LambdaForm.create(1, idNames, VOID_RESULT, Kind.IDENTITY);
1766                 idForm.compileToBytecode();
1767                 idFun = new NamedFunction(idMem, SimpleMethodHandle.make(idMem.getInvocationType(), idForm));
1768 
1769                 zeForm = idForm;
1770                 zeFun = idFun;
1771             } else {
1772                 Name[] idNames = new Name[] { argument(0, L_TYPE), argument(1, type) };
1773                 idForm = LambdaForm.create(2, idNames, 1, Kind.IDENTITY);
1774                 idForm.compileToBytecode();
1775                 idFun = new NamedFunction(idMem, MethodHandleImpl.makeIntrinsic(SimpleMethodHandle.make(idMem.getInvocationType(), idForm),
1776                             MethodHandleImpl.Intrinsic.IDENTITY));
1777 
1778                 Object zeValue = Wrapper.forBasicType(btChar).zero();
1779                 Name[] zeNames = new Name[] { argument(0, L_TYPE), new Name(idFun, zeValue) };
1780                 zeForm = LambdaForm.create(1, zeNames, 1, Kind.ZERO);
1781                 zeForm.compileToBytecode();
1782                 zeFun = new NamedFunction(zeMem, MethodHandleImpl.makeIntrinsic(SimpleMethodHandle.make(zeMem.getInvocationType(), zeForm),
1783                         MethodHandleImpl.Intrinsic.ZERO));
1784             }
1785 
1786             LF_zero[ord] = zeForm;
1787             NF_zero[ord] = zeFun;
1788             LF_identity[ord] = idForm;
1789             NF_identity[ord] = idFun;
1790 
1791             assert(idFun.isIdentity());
1792             assert(zeFun.isConstantZero());
1793             assert(new Name(zeFun).isConstantZero());
1794         }
1795     }
1796 
1797     // Avoid appealing to ValueConversions at bootstrap time:
1798     private static int identity_I(int x) { return x; }
1799     private static long identity_J(long x) { return x; }
1800     private static float identity_F(float x) { return x; }
1801     private static double identity_D(double x) { return x; }
1802     private static Object identity_L(Object x) { return x; }
1803     private static void identity_V() { return; }
1804     private static int zero_I() { return 0; }
1805     private static long zero_J() { return 0; }
1806     private static float zero_F() { return 0; }
1807     private static double zero_D() { return 0; }
1808     private static Object zero_L() { return null; }
1809 
1810     /**
1811      * Internal marker for byte-compiled LambdaForms.
1812      */
1813     /*non-public*/
1814     @Target(ElementType.METHOD)
1815     @Retention(RetentionPolicy.RUNTIME)
1816     @interface Compiled {
1817     }
1818 
1819     private static final HashMap<String,Integer> DEBUG_NAME_COUNTERS;
1820     private static final HashMap<LambdaForm,String> DEBUG_NAMES;
1821     static {
1822         if (debugEnabled()) {
1823             DEBUG_NAME_COUNTERS = new HashMap<>();
1824             DEBUG_NAMES = new HashMap<>();
1825         } else {
1826             DEBUG_NAME_COUNTERS = null;
1827             DEBUG_NAMES = null;
1828         }
1829     }
1830 
1831     static {
1832         // The Holder class will contain pre-generated forms resolved
1833         // using MemberName.getFactory(). However, that doesn't initialize the
1834         // class, which subtly breaks inlining etc. By forcing
1835         // initialization of the Holder class we avoid these issues.
1836         UNSAFE.ensureClassInitialized(Holder.class);
1837     }
1838 
1839     /* Placeholder class for zero and identity forms generated ahead of time */
1840     final class Holder {}
1841 
1842     // The following hack is necessary in order to suppress TRACE_INTERPRETER
1843     // during execution of the static initializes of this class.
1844     // Turning on TRACE_INTERPRETER too early will cause
1845     // stack overflows and other misbehavior during attempts to trace events
1846     // that occur during LambdaForm.<clinit>.
1847     // Therefore, do not move this line higher in this file, and do not remove.
1848     private static final boolean TRACE_INTERPRETER = MethodHandleStatics.TRACE_INTERPRETER;
1849 }