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