jdk/src/share/classes/java/lang/invoke/LambdaForm.java
author twisti
Tue, 24 Jul 2012 10:47:44 -0700
changeset 13423 17843fff200d
child 13610 28122b96858e
permissions -rw-r--r--
7023639: JSR 292 method handle invocation needs a fast path for compiled code 6984705: JSR 292 method handle creation should not go through JNI Summary: remove assembly code for JDK 7 chained method handles Reviewed-by: jrose, twisti, mhaupt, forax Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     1
/*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     4
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    10
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    15
 * accompanied this code).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    16
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    20
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    23
 * questions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    24
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    25
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    26
package java.lang.invoke;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    27
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    28
import java.lang.annotation.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    29
import java.lang.reflect.Method;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    30
import java.util.Map;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    31
import java.util.List;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    32
import java.util.Arrays;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    33
import java.util.ArrayList;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    34
import java.util.HashMap;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    35
import java.util.concurrent.ConcurrentHashMap;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    36
import sun.invoke.util.Wrapper;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    37
import static java.lang.invoke.MethodHandleStatics.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    38
import static java.lang.invoke.MethodHandleNatives.Constants.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    39
import java.lang.reflect.Field;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    40
import java.util.Objects;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    41
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    42
/**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    43
 * The symbolic, non-executable form of a method handle's invocation semantics.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    44
 * It consists of a series of names.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    45
 * The first N (N=arity) names are parameters,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    46
 * while any remaining names are temporary values.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    47
 * Each temporary specifies the application of a function to some arguments.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    48
 * The functions are method handles, while the arguments are mixes of
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    49
 * constant values and local names.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    50
 * The result of the lambda is defined as one of the names, often the last one.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    51
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    52
 * Here is an approximate grammar:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    53
 * <pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    54
 * LambdaForm = "(" ArgName* ")=>{" TempName* Result "}"
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    55
 * ArgName = "a" N ":" T
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    56
 * TempName = "t" N ":" T "=" Function "(" Argument* ");"
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    57
 * Function = ConstantValue
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    58
 * Argument = NameRef | ConstantValue
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    59
 * Result = NameRef | "void"
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    60
 * NameRef = "a" N | "t" N
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    61
 * N = (any whole number)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    62
 * T = "L" | "I" | "J" | "F" | "D" | "V"
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    63
 * </pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    64
 * Names are numbered consecutively from left to right starting at zero.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    65
 * (The letters are merely a taste of syntax sugar.)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    66
 * Thus, the first temporary (if any) is always numbered N (where N=arity).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    67
 * Every occurrence of a name reference in an argument list must refer to
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    68
 * a name previously defined within the same lambda.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    69
 * A lambda has a void result if and only if its result index is -1.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    70
 * If a temporary has the type "V", it cannot be the subject of a NameRef,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    71
 * even though possesses a number.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    72
 * Note that all reference types are erased to "L", which stands for {@code Object).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    73
 * All subword types (boolean, byte, short, char) are erased to "I" which is {@code int}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    74
 * The other types stand for the usual primitive types.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    75
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    76
 * Function invocation closely follows the static rules of the Java verifier.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    77
 * Arguments and return values must exactly match when their "Name" types are
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    78
 * considered.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    79
 * Conversions are allowed only if they do not change the erased type.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    80
 * <ul>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    81
 * <li>L = Object: casts are used freely to convert into and out of reference types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    82
 * <li>I = int: subword types are forcibly narrowed when passed as arguments (see {@code explicitCastArguments})
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    83
 * <li>J = long: no implicit conversions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    84
 * <li>F = float: no implicit conversions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    85
 * <li>D = double: no implicit conversions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    86
 * <li>V = void: a function result may be void if and only if its Name is of type "V"
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    87
 * </ul>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    88
 * Although implicit conversions are not allowed, explicit ones can easily be
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    89
 * encoded by using temporary expressions which call type-transformed identity functions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    90
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    91
 * Examples:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    92
 * <pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    93
 * (a0:J)=>{ a0 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    94
 *     == identity(long)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    95
 * (a0:I)=>{ t1:V = System.out#println(a0); void }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    96
 *     == System.out#println(int)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    97
 * (a0:L)=>{ t1:V = System.out#println(a0); a0 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    98
 *     == identity, with printing side-effect
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    99
 * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   100
 *                 t3:L = BoundMethodHandle#target(a0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   101
 *                 t4:L = MethodHandle#invoke(t3, t2, a1); t4 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   102
 *     == general invoker for unary insertArgument combination
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   103
 * (a0:L, a1:L)=>{ t2:L = FilterMethodHandle#filter(a0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   104
 *                 t3:L = MethodHandle#invoke(t2, a1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   105
 *                 t4:L = FilterMethodHandle#target(a0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   106
 *                 t5:L = MethodHandle#invoke(t4, t3); t5 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   107
 *     == general invoker for unary filterArgument combination
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   108
 * (a0:L, a1:L)=>{ ...(same as previous example)...
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   109
 *                 t5:L = MethodHandle#invoke(t4, t3, a1); t5 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   110
 *     == general invoker for unary/unary foldArgument combination
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   111
 * (a0:L, a1:I)=>{ t2:I = identity(long).asType((int)->long)(a1); t2 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   112
 *     == invoker for identity method handle which performs i2l
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   113
 * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   114
 *                 t3:L = Class#cast(t2,a1); t3 }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   115
 *     == invoker for identity method handle which performs cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   116
 * </pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   117
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   118
 * @author John Rose, JSR 292 EG
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   119
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   120
class LambdaForm {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   121
    final int arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   122
    final int result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   123
    final Name[] names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   124
    final String debugName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   125
    MemberName vmentry;   // low-level behavior, or null if not yet prepared
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   126
    private boolean isCompiled;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   127
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   128
    // Caches for common structural transforms:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   129
    LambdaForm[] bindCache;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   130
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   131
    public static final int VOID_RESULT = -1, LAST_RESULT = -2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   132
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   133
    LambdaForm(String debugName,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   134
               int arity, Name[] names, int result) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   135
        assert(namesOK(arity, names));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   136
        this.arity = arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   137
        this.result = fixResult(result, names);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   138
        this.names = names.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   139
        this.debugName = debugName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   140
        normalize();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   141
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   142
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   143
    LambdaForm(String debugName,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   144
               int arity, Name[] names) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   145
        this(debugName,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   146
             arity, names, LAST_RESULT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   147
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   148
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   149
    LambdaForm(String debugName,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   150
               Name[] formals, Name[] temps, Name result) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   151
        this(debugName,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   152
             formals.length, buildNames(formals, temps, result), LAST_RESULT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   153
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   154
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   155
    private static Name[] buildNames(Name[] formals, Name[] temps, Name result) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   156
        int arity = formals.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   157
        int length = arity + temps.length + (result == null ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   158
        Name[] names = Arrays.copyOf(formals, length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   159
        System.arraycopy(temps, 0, names, arity, temps.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   160
        if (result != null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   161
            names[length - 1] = result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   162
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   163
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   164
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   165
    private LambdaForm(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   166
        // Make a blank lambda form, which returns a constant zero or null.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   167
        // It is used as a template for managing the invocation of similar forms that are non-empty.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   168
        // Called only from getPreparedForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   169
        assert(isValidSignature(sig));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   170
        this.arity = signatureArity(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   171
        this.result = (signatureReturn(sig) == 'V' ? -1 : arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   172
        this.names = buildEmptyNames(arity, sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   173
        this.debugName = "LF.zero";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   174
        assert(nameRefsAreLegal());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   175
        assert(isEmpty());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   176
        assert(sig.equals(basicTypeSignature()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   177
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   178
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   179
    private static Name[] buildEmptyNames(int arity, String basicTypeSignature) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   180
        assert(isValidSignature(basicTypeSignature));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   181
        int resultPos = arity + 1;  // skip '_'
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   182
        if (arity < 0 || basicTypeSignature.length() != resultPos+1)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   183
            throw new IllegalArgumentException("bad arity for "+basicTypeSignature);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   184
        int numRes = (basicTypeSignature.charAt(resultPos) == 'V' ? 0 : 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   185
        Name[] names = arguments(numRes, basicTypeSignature.substring(0, arity));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   186
        for (int i = 0; i < numRes; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   187
            names[arity + i] = constantZero(arity + i, basicTypeSignature.charAt(resultPos + i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   188
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   189
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   190
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   191
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   192
    private static int fixResult(int result, Name[] names) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   193
        if (result >= 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   194
            if (names[result].type == 'V')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   195
                return -1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   196
        } else if (result == LAST_RESULT) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   197
            return names.length - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   198
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   199
        return result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   200
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   201
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   202
    private static boolean namesOK(int arity, Name[] names) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   203
        for (int i = 0; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   204
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   205
            assert(n != null) : "n is null";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   206
            if (i < arity)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   207
                assert( n.isParam()) : n + " is not param at " + i;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   208
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   209
                assert(!n.isParam()) : n + " is param at " + i;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   210
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   211
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   212
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   213
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   214
    /** Renumber and/or replace params so that they are interned and canonically numbered. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   215
    private void normalize() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   216
        Name[] oldNames = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   217
        int changesStart = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   218
        for (int i = 0; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   219
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   220
            if (!n.initIndex(i)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   221
                if (oldNames == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   222
                    oldNames = names.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   223
                    changesStart = i;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   224
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   225
                names[i] = n.cloneWithIndex(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   226
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   227
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   228
        if (oldNames != null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   229
            int startFixing = arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   230
            if (startFixing <= changesStart)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   231
                startFixing = changesStart+1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   232
            for (int i = startFixing; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   233
                Name fixed = names[i].replaceNames(oldNames, names, changesStart, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   234
                names[i] = fixed.newIndex(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   235
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   236
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   237
        assert(nameRefsAreLegal());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   238
        int maxInterned = Math.min(arity, INTERNED_ARGUMENT_LIMIT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   239
        boolean needIntern = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   240
        for (int i = 0; i < maxInterned; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   241
            Name n = names[i], n2 = internArgument(n);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   242
            if (n != n2) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   243
                names[i] = n2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   244
                needIntern = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   245
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   246
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   247
        if (needIntern) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   248
            for (int i = arity; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   249
                names[i].internArguments();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   250
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   251
            assert(nameRefsAreLegal());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   252
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   253
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   254
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   255
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   256
     * Check that all embedded Name references are localizable to this lambda,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   257
     * and are properly ordered after their corresponding definitions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   258
     * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   259
     * Note that a Name can be local to multiple lambdas, as long as
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   260
     * it possesses the same index in each use site.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   261
     * This allows Name references to be freely reused to construct
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   262
     * fresh lambdas, without confusion.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   263
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   264
    private boolean nameRefsAreLegal() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   265
        assert(arity >= 0 && arity <= names.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   266
        assert(result >= -1 && result < names.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   267
        // Do all names possess an index consistent with their local definition order?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   268
        for (int i = 0; i < arity; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   269
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   270
            assert(n.index() == i) : Arrays.asList(n.index(), i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   271
            assert(n.isParam());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   272
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   273
        // Also, do all local name references
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   274
        for (int i = arity; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   275
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   276
            assert(n.index() == i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   277
            for (Object arg : n.arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   278
                if (arg instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   279
                    Name n2 = (Name) arg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   280
                    int i2 = n2.index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   281
                    assert(0 <= i2 && i2 < names.length) : n.debugString() + ": 0 <= i2 && i2 < names.length: 0 <= " + i2 + " < " + names.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   282
                    assert(names[i2] == n2) : Arrays.asList("-1-", i, "-2-", n.debugString(), "-3-", i2, "-4-", n2.debugString(), "-5-", names[i2].debugString(), "-6-", this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   283
                    assert(i2 < i);  // ref must come after def!
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   284
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   285
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   286
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   287
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   288
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   289
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   290
    /** Invoke this form on the given arguments. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   291
    // final Object invoke(Object... args) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   292
    //     // NYI: fit this into the fast path?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   293
    //     return interpretWithArguments(args);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   294
    // }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   295
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   296
    /** Report the return type. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   297
    char returnType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   298
        if (result < 0)  return 'V';
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   299
        Name n = names[result];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   300
        return n.type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   301
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   302
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   303
    /** Report the N-th argument type. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   304
    char parameterType(int n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   305
        assert(n < arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   306
        return names[n].type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   307
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   308
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   309
    /** Report the arity. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   310
    int arity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   311
        return arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   312
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   313
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   314
    /** Return the method type corresponding to my basic type signature. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   315
    MethodType methodType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   316
        return signatureType(basicTypeSignature());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   317
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   318
    /** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   319
    final String basicTypeSignature() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   320
        StringBuilder buf = new StringBuilder(arity() + 3);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   321
        for (int i = 0, a = arity(); i < a; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   322
            buf.append(parameterType(i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   323
        return buf.append('_').append(returnType()).toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   324
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   325
    static int signatureArity(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   326
        assert(isValidSignature(sig));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   327
        return sig.indexOf('_');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   328
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   329
    static char signatureReturn(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   330
        return sig.charAt(signatureArity(sig)+1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   331
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   332
    static boolean isValidSignature(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   333
        int arity = sig.indexOf('_');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   334
        if (arity < 0)  return false;  // must be of the form *_*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   335
        int siglen = sig.length();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   336
        if (siglen != arity + 2)  return false;  // *_X
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   337
        for (int i = 0; i < siglen; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   338
            if (i == arity)  continue;  // skip '_'
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   339
            char c = sig.charAt(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   340
            if (c == 'V')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   341
                return (i == siglen - 1 && arity == siglen - 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   342
            if (ALL_TYPES.indexOf(c) < 0)  return false; // must be [LIJFD]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   343
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   344
        return true;  // [LIJFD]*_[LIJFDV]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   345
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   346
    static Class<?> typeClass(char t) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   347
        switch (t) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   348
        case 'I': return int.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   349
        case 'J': return long.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   350
        case 'F': return float.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   351
        case 'D': return double.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   352
        case 'L': return Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   353
        case 'V': return void.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   354
        default: assert false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   355
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   356
        return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   357
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   358
    static MethodType signatureType(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   359
        Class<?>[] ptypes = new Class<?>[signatureArity(sig)];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   360
        for (int i = 0; i < ptypes.length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   361
            ptypes[i] = typeClass(sig.charAt(i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   362
        Class<?> rtype = typeClass(signatureReturn(sig));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   363
        return MethodType.methodType(rtype, ptypes);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   364
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   365
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   366
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   367
     * Code generation issues:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   368
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   369
     * Compiled LFs should be reusable in general.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   370
     * The biggest issue is how to decide when to pull a name into
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   371
     * the bytecode, versus loading a reified form from the MH data.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   372
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   373
     * For example, an asType wrapper may require execution of a cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   374
     * after a call to a MH.  The target type of the cast can be placed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   375
     * as a constant in the LF itself.  This will force the cast type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   376
     * to be compiled into the bytecodes and native code for the MH.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   377
     * Or, the target type of the cast can be erased in the LF, and
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   378
     * loaded from the MH data.  (Later on, if the MH as a whole is
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   379
     * inlined, the data will flow into the inlined instance of the LF,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   380
     * as a constant, and the end result will be an optimal cast.)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   381
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   382
     * This erasure of cast types can be done with any use of
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   383
     * reference types.  It can also be done with whole method
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   384
     * handles.  Erasing a method handle might leave behind
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   385
     * LF code that executes correctly for any MH of a given
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   386
     * type, and load the required MH from the enclosing MH's data.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   387
     * Or, the erasure might even erase the expected MT.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   388
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   389
     * Also, for direct MHs, the MemberName of the target
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   390
     * could be erased, and loaded from the containing direct MH.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   391
     * As a simple case, a LF for all int-valued non-static
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   392
     * field getters would perform a cast on its input argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   393
     * (to non-constant base type derived from the MemberName)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   394
     * and load an integer value from the input object
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   395
     * (at a non-constant offset also derived from the MemberName).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   396
     * Such MN-erased LFs would be inlinable back to optimized
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   397
     * code, whenever a constant enclosing DMH is available
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   398
     * to supply a constant MN from its data.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   399
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   400
     * The main problem here is to keep LFs reasonably generic,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   401
     * while ensuring that hot spots will inline good instances.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   402
     * "Reasonably generic" means that we don't end up with
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   403
     * repeated versions of bytecode or machine code that do
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   404
     * not differ in their optimized form.  Repeated versions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   405
     * of machine would have the undesirable overheads of
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   406
     * (a) redundant compilation work and (b) extra I$ pressure.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   407
     * To control repeated versions, we need to be ready to
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   408
     * erase details from LFs and move them into MH data,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   409
     * whevener those details are not relevant to significant
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   410
     * optimization.  "Significant" means optimization of
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   411
     * code that is actually hot.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   412
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   413
     * Achieving this may require dynamic splitting of MHs, by replacing
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   414
     * a generic LF with a more specialized one, on the same MH,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   415
     * if (a) the MH is frequently executed and (b) the MH cannot
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   416
     * be inlined into a containing caller, such as an invokedynamic.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   417
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   418
     * Compiled LFs that are no longer used should be GC-able.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   419
     * If they contain non-BCP references, they should be properly
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   420
     * interlinked with the class loader(s) that their embedded types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   421
     * depend on.  This probably means that reusable compiled LFs
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   422
     * will be tabulated (indexed) on relevant class loaders,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   423
     * or else that the tables that cache them will have weak links.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   424
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   425
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   426
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   427
     * Make this LF directly executable, as part of a MethodHandle.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   428
     * Invariant:  Every MH which is invoked must prepare its LF
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   429
     * before invocation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   430
     * (In principle, the JVM could do this very lazily,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   431
     * as a sort of pre-invocation linkage step.)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   432
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   433
    public void prepare() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   434
        if (COMPILE_THRESHOLD == 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   435
            compileToBytecode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   436
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   437
        if (this.vmentry != null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   438
            // already prepared (e.g., a primitive DMH invoker form)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   439
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   440
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   441
        LambdaForm prep = getPreparedForm(basicTypeSignature());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   442
        this.vmentry = prep.vmentry;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   443
        // TO DO: Maybe add invokeGeneric, invokeWithArguments
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   444
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   445
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   446
    /** Generate optimizable bytecode for this form. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   447
    MemberName compileToBytecode() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   448
        MethodType invokerType = methodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   449
        assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   450
        if (vmentry != null && isCompiled) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   451
            return vmentry;  // already compiled somehow
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   452
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   453
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   454
            vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   455
            if (TRACE_INTERPRETER)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   456
                traceInterpreter("compileToBytecode", this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   457
            isCompiled = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   458
            return vmentry;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   459
        } catch (Error | Exception ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   460
            throw new InternalError(this.toString(), ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   461
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   462
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   463
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   464
    private static final ConcurrentHashMap<String,LambdaForm> PREPARED_FORMS;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   465
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   466
        int   capacity   = 512;    // expect many distinct signatures over time
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   467
        float loadFactor = 0.75f;  // normal default
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   468
        int   writers    = 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   469
        PREPARED_FORMS = new ConcurrentHashMap<>(capacity, loadFactor, writers);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   470
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   471
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   472
    private static Map<String,LambdaForm> computeInitialPreparedForms() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   473
        // Find all predefined invokers and associate them with canonical empty lambda forms.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   474
        HashMap<String,LambdaForm> forms = new HashMap<>();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   475
        for (MemberName m : MemberName.getFactory().getMethods(LambdaForm.class, false, null, null, null)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   476
            if (!m.isStatic() || !m.isPackage())  continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   477
            MethodType mt = m.getMethodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   478
            if (mt.parameterCount() > 0 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   479
                mt.parameterType(0) == MethodHandle.class &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   480
                m.getName().startsWith("interpret_")) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   481
                String sig = basicTypeSignature(mt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   482
                assert(m.getName().equals("interpret" + sig.substring(sig.indexOf('_'))));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   483
                LambdaForm form = new LambdaForm(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   484
                form.vmentry = m;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   485
                mt.form().setCachedLambdaForm(MethodTypeForm.LF_COUNTER, form);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   486
                // FIXME: get rid of PREPARED_FORMS; use MethodTypeForm cache only
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   487
                forms.put(sig, form);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   488
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   489
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   490
        //System.out.println("computeInitialPreparedForms => "+forms);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   491
        return forms;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   492
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   493
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   494
    // Set this false to disable use of the interpret_L methods defined in this file.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   495
    private static final boolean USE_PREDEFINED_INTERPRET_METHODS = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   496
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   497
    // The following are predefined exact invokers.  The system must build
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   498
    // a separate invoker for each distinct signature.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   499
    static Object interpret_L(MethodHandle mh) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   500
        Object[] av = {mh};
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   501
        String sig = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   502
        assert(argumentTypesMatch(sig = "L_L", av));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   503
        Object res = mh.form.interpretWithArguments(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   504
        assert(returnTypesMatch(sig, av, res));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   505
        return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   506
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   507
    static Object interpret_L(MethodHandle mh, Object x1) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   508
        Object[] av = {mh, x1};
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   509
        String sig = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   510
        assert(argumentTypesMatch(sig = "LL_L", av));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   511
        Object res = mh.form.interpretWithArguments(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   512
        assert(returnTypesMatch(sig, av, res));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   513
        return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   514
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   515
    static Object interpret_L(MethodHandle mh, Object x1, Object x2) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   516
        Object[] av = {mh, x1, x2};
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   517
        String sig = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   518
        assert(argumentTypesMatch(sig = "LLL_L", av));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   519
        Object res = mh.form.interpretWithArguments(av);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   520
        assert(returnTypesMatch(sig, av, res));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   521
        return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   522
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   523
    private static LambdaForm getPreparedForm(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   524
        MethodType mtype = signatureType(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   525
        //LambdaForm prep = PREPARED_FORMS.get(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   526
        LambdaForm prep =  mtype.form().cachedLambdaForm(MethodTypeForm.LF_INTERPRET);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   527
        if (prep != null)  return prep;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   528
        assert(isValidSignature(sig));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   529
        prep = new LambdaForm(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   530
        prep.vmentry = InvokerBytecodeGenerator.generateLambdaFormInterpreterEntryPoint(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   531
        //LambdaForm prep2 = PREPARED_FORMS.putIfAbsent(sig.intern(), prep);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   532
        return mtype.form().setCachedLambdaForm(MethodTypeForm.LF_INTERPRET, prep);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   533
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   534
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   535
    // The next few routines are called only from assert expressions
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   536
    // They verify that the built-in invokers process the correct raw data types.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   537
    private static boolean argumentTypesMatch(String sig, Object[] av) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   538
        int arity = signatureArity(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   539
        assert(av.length == arity) : "av.length == arity: av.length=" + av.length + ", arity=" + arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   540
        assert(av[0] instanceof MethodHandle) : "av[0] not instace of MethodHandle: " + av[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   541
        MethodHandle mh = (MethodHandle) av[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   542
        MethodType mt = mh.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   543
        assert(mt.parameterCount() == arity-1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   544
        for (int i = 0; i < av.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   545
            Class<?> pt = (i == 0 ? MethodHandle.class : mt.parameterType(i-1));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   546
            assert(valueMatches(sig.charAt(i), pt, av[i]));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   547
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   548
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   549
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   550
    private static boolean valueMatches(char tc, Class<?> type, Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   551
        // The following line is needed because (...)void method handles can use non-void invokers
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   552
        if (type == void.class)  tc = 'V';   // can drop any kind of value
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   553
        assert tc == basicType(type) : tc + " == basicType(" + type + ")=" + basicType(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   554
        switch (tc) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   555
        case 'I': assert checkInt(type, x)   : "checkInt(" + type + "," + x +")";   break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   556
        case 'J': assert x instanceof Long   : "instanceof Long: " + x;             break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   557
        case 'F': assert x instanceof Float  : "instanceof Float: " + x;            break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   558
        case 'D': assert x instanceof Double : "instanceof Double: " + x;           break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   559
        case 'L': assert checkRef(type, x)   : "checkRef(" + type + "," + x + ")";  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   560
        case 'V': break;  // allow anything here; will be dropped
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   561
        default:  assert(false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   562
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   563
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   564
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   565
    private static boolean returnTypesMatch(String sig, Object[] av, Object res) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   566
        MethodHandle mh = (MethodHandle) av[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   567
        return valueMatches(signatureReturn(sig), mh.type().returnType(), res);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   568
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   569
    private static boolean checkInt(Class<?> type, Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   570
        assert(x instanceof Integer);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   571
        if (type == int.class)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   572
        Wrapper w = Wrapper.forBasicType(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   573
        assert(w.isSubwordOrInt());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   574
        Object x1 = Wrapper.INT.wrap(w.wrap(x));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   575
        return x.equals(x1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   576
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   577
    private static boolean checkRef(Class<?> type, Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   578
        assert(!type.isPrimitive());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   579
        if (x == null)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   580
        if (type.isInterface())  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   581
        return type.isInstance(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   582
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   583
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   584
    /** If the invocation count hits the threshold we spin bytecodes and call that subsequently. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   585
    private static final int COMPILE_THRESHOLD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   586
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   587
        if (MethodHandleStatics.COMPILE_THRESHOLD != null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   588
            COMPILE_THRESHOLD = MethodHandleStatics.COMPILE_THRESHOLD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   589
        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   590
            COMPILE_THRESHOLD = 30;  // default value
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   591
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   592
    private int invocationCounter = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   593
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   594
    @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   595
    /** Interpretively invoke this form on the given arguments. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   596
    Object interpretWithArguments(Object... argumentValues) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   597
        if (TRACE_INTERPRETER)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   598
            return interpretWithArgumentsTracing(argumentValues);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   599
        if (COMPILE_THRESHOLD != 0 &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   600
            invocationCounter < COMPILE_THRESHOLD) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   601
            invocationCounter++;  // benign race
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   602
            if (invocationCounter >= COMPILE_THRESHOLD) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   603
                // Replace vmentry with a bytecode version of this LF.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   604
                compileToBytecode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   605
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   606
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   607
        assert(arityCheck(argumentValues));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   608
        Object[] values = Arrays.copyOf(argumentValues, names.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   609
        for (int i = argumentValues.length; i < values.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   610
            values[i] = interpretName(names[i], values);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   611
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   612
        return (result < 0) ? null : values[result];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   613
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   614
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   615
    @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   616
    /** Evaluate a single Name within this form, applying its function to its arguments. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   617
    Object interpretName(Name name, Object[] values) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   618
        if (TRACE_INTERPRETER)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   619
            traceInterpreter("| interpretName", name.debugString(), (Object[]) null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   620
        Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   621
        for (int i = 0; i < arguments.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   622
            Object a = arguments[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   623
            if (a instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   624
                int i2 = ((Name)a).index();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   625
                assert(names[i2] == a);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   626
                a = values[i2];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   627
                arguments[i] = a;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   628
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   629
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   630
        return name.function.invokeWithArguments(arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   631
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   632
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   633
    Object interpretWithArgumentsTracing(Object... argumentValues) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   634
        traceInterpreter("[ interpretWithArguments", this, argumentValues);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   635
        if (invocationCounter < COMPILE_THRESHOLD) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   636
            int ctr = invocationCounter++;  // benign race
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   637
            traceInterpreter("| invocationCounter", ctr);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   638
            if (invocationCounter >= COMPILE_THRESHOLD) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   639
                compileToBytecode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   640
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   641
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   642
        Object rval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   643
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   644
            assert(arityCheck(argumentValues));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   645
            Object[] values = Arrays.copyOf(argumentValues, names.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   646
            for (int i = argumentValues.length; i < values.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   647
                values[i] = interpretName(names[i], values);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   648
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   649
            rval = (result < 0) ? null : values[result];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   650
        } catch (Throwable ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   651
            traceInterpreter("] throw =>", ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   652
            throw ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   653
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   654
        traceInterpreter("] return =>", rval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   655
        return rval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   656
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   657
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   658
    //** This transform is applied (statically) to every name.function. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   659
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   660
    private static MethodHandle eraseSubwordTypes(MethodHandle mh) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   661
        MethodType mt = mh.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   662
        if (mt.hasPrimitives()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   663
            mt = mt.changeReturnType(eraseSubwordType(mt.returnType()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   664
            for (int i = 0; i < mt.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   665
                mt = mt.changeParameterType(i, eraseSubwordType(mt.parameterType(i)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   666
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   667
            mh = MethodHandles.explicitCastArguments(mh, mt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   668
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   669
        return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   670
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   671
    private static Class<?> eraseSubwordType(Class<?> type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   672
        if (!type.isPrimitive())  return type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   673
        if (type == int.class)  return type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   674
        Wrapper w = Wrapper.forPrimitiveType(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   675
        if (w.isSubwordOrInt())  return int.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   676
        return type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   677
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   678
    */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   679
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   680
    static void traceInterpreter(String event, Object obj, Object... args) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   681
        if (!TRACE_INTERPRETER)  return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   682
        System.out.println("LFI: "+event+" "+(obj != null ? obj : "")+(args != null && args.length != 0 ? Arrays.asList(args) : ""));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   683
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   684
    static void traceInterpreter(String event, Object obj) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   685
        traceInterpreter(event, obj, (Object[])null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   686
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   687
    private boolean arityCheck(Object[] argumentValues) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   688
        assert(argumentValues.length == arity) : arity+"!="+Arrays.asList(argumentValues)+".length";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   689
        // also check that the leading (receiver) argument is somehow bound to this LF:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   690
        assert(argumentValues[0] instanceof MethodHandle) : "not MH: " + argumentValues[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   691
        assert(((MethodHandle)argumentValues[0]).internalForm() == this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   692
        // note:  argument #0 could also be an interface wrapper, in the future
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   693
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   694
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   695
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   696
    private boolean isEmpty() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   697
        if (result < 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   698
            return (names.length == arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   699
        else if (result == arity && names.length == arity + 1)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   700
            return names[arity].isConstantZero();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   701
        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   702
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   703
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   704
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   705
    public String toString() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   706
        StringBuilder buf = new StringBuilder("Lambda(");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   707
        for (int i = 0; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   708
            if (i == arity)  buf.append(")=>{");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   709
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   710
            if (i >= arity)  buf.append("\n    ");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   711
            buf.append(n);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   712
            if (i < arity) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   713
                if (i+1 < arity)  buf.append(",");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   714
                continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   715
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   716
            buf.append("=").append(n.exprString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   717
            buf.append(";");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   718
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   719
        buf.append(result < 0 ? "void" : names[result]).append("}");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   720
        if (TRACE_INTERPRETER) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   721
            // Extra verbosity:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   722
            buf.append(":").append(basicTypeSignature());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   723
            buf.append("/").append(vmentry);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   724
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   725
        return buf.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   726
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   727
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   728
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   729
     * Apply immediate binding for a Name in this form indicated by its position relative to the form.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   730
     * The first parameter to a LambdaForm, a0:L, always represents the form's method handle, so 0 is not
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   731
     * accepted as valid.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   732
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   733
    LambdaForm bindImmediate(int pos, char basicType, Object value) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   734
        // must be an argument, and the types must match
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   735
        assert pos > 0 && pos < arity && names[pos].type == basicType && Name.typesMatch(basicType, value);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   736
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   737
        int arity2 = arity - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   738
        Name[] names2 = new Name[names.length - 1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   739
        for (int r = 0, w = 0; r < names.length; ++r, ++w) { // (r)ead from names, (w)rite to names2
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   740
            Name n = names[r];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   741
            if (n.isParam()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   742
                if (n.index == pos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   743
                    // do not copy over the argument that is to be replaced with a literal,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   744
                    // but adjust the write index
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   745
                    --w;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   746
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   747
                    names2[w] = new Name(w, n.type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   748
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   749
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   750
                Object[] arguments2 = new Object[n.arguments.length];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   751
                for (int i = 0; i < n.arguments.length; ++i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   752
                    Object arg = n.arguments[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   753
                    if (arg instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   754
                        int ni = ((Name) arg).index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   755
                        if (ni == pos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   756
                            arguments2[i] = value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   757
                        } else if (ni < pos) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   758
                            // replacement position not yet passed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   759
                            arguments2[i] = names2[ni];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   760
                        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   761
                            // replacement position passed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   762
                            arguments2[i] = names2[ni - 1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   763
                        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   764
                    } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   765
                        arguments2[i] = arg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   766
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   767
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   768
                names2[w] = new Name(n.function, arguments2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   769
                names2[w].initIndex(w);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   770
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   771
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   772
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   773
        int result2 = result == -1 ? -1 : result - 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   774
        return new LambdaForm(debugName, arity2, names2, result2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   775
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   776
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   777
    LambdaForm bind(int namePos, BoundMethodHandle.SpeciesData oldData) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   778
        Name name = names[namePos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   779
        BoundMethodHandle.SpeciesData newData = oldData.extendWithType(name.type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   780
        return bind(name, newData.getterName(names[0], oldData.fieldCount()), oldData, newData);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   781
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   782
    LambdaForm bind(Name name, Name binding,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   783
                    BoundMethodHandle.SpeciesData oldData,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   784
                    BoundMethodHandle.SpeciesData newData) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   785
        int pos = name.index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   786
        assert(name.isParam());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   787
        assert(!binding.isParam());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   788
        assert(name.type == binding.type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   789
        assert(0 <= pos && pos < arity && names[pos] == name);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   790
        assert(binding.function.memberDeclaringClassOrNull() == newData.clazz);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   791
        assert(oldData.getters.length == newData.getters.length-1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   792
        if (bindCache != null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   793
            LambdaForm form = bindCache[pos];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   794
            if (form != null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   795
                assert(form.contains(binding)) : "form << " + form + " >> does not contain binding << " + binding + " >>";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   796
                return form;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   797
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   798
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   799
            bindCache = new LambdaForm[arity];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   800
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   801
        assert(nameRefsAreLegal());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   802
        int arity2 = arity-1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   803
        Name[] names2 = names.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   804
        names2[pos] = binding;  // we might move this in a moment
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   805
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   806
        // The newly created LF will run with a different BMH.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   807
        // Switch over any pre-existing BMH field references to the new BMH class.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   808
        int firstOldRef = -1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   809
        for (int i = 0; i < names2.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   810
            Name n = names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   811
            if (n.function != null &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   812
                n.function.memberDeclaringClassOrNull() == oldData.clazz) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   813
                MethodHandle oldGetter = n.function.resolvedHandle;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   814
                MethodHandle newGetter = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   815
                for (int j = 0; j < oldData.getters.length; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   816
                    if (oldGetter == oldData.getters[j])
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   817
                        newGetter =  newData.getters[j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   818
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   819
                if (newGetter != null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   820
                    if (firstOldRef < 0)  firstOldRef = i;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   821
                    Name n2 = new Name(newGetter, n.arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   822
                    names2[i] = n2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   823
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   824
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   825
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   826
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   827
        // Walk over the new list of names once, in forward order.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   828
        // Replace references to 'name' with 'binding'.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   829
        // Replace data structure references to the old BMH species with the new.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   830
        // This might cause a ripple effect, but it will settle in one pass.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   831
        assert(firstOldRef < 0 || firstOldRef > pos);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   832
        for (int i = pos+1; i < names2.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   833
            if (i <= arity2)  continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   834
            names2[i] = names2[i].replaceNames(names, names2, pos, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   835
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   836
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   837
        //  (a0, a1, name=a2, a3, a4)  =>  (a0, a1, a3, a4, binding)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   838
        int insPos = pos;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   839
        for (; insPos+1 < names2.length; insPos++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   840
            Name n = names2[insPos+1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   841
            if (n.isSiblingBindingBefore(binding)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   842
                names2[insPos] = n;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   843
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   844
                break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   845
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   846
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   847
        names2[insPos] = binding;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   848
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   849
        // Since we moved some stuff, maybe update the result reference:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   850
        int result2 = result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   851
        if (result2 == pos)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   852
            result2 = insPos;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   853
        else if (result2 > pos && result2 <= insPos)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   854
            result2 -= 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   855
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   856
        return bindCache[pos] = new LambdaForm(debugName, arity2, names2, result2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   857
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   858
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   859
    boolean contains(Name name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   860
        int pos = name.index();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   861
        if (pos >= 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   862
            return pos < names.length && name.equals(names[pos]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   863
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   864
        for (int i = arity; i < names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   865
            if (name.equals(names[i]))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   866
                return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   867
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   868
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   869
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   870
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   871
    LambdaForm addArguments(int pos, char... types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   872
        assert(pos <= arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   873
        int length = names.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   874
        int inTypes = types.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   875
        Name[] names2 = Arrays.copyOf(names, length + inTypes);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   876
        int arity2 = arity + inTypes;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   877
        int result2 = result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   878
        if (result2 >= arity)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   879
            result2 += inTypes;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   880
        // names array has MH in slot 0; skip it.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   881
        int argpos = pos + 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   882
        // Note:  The LF constructor will rename names2[argpos...].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   883
        // Make space for new arguments (shift temporaries).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   884
        System.arraycopy(names, argpos, names2, argpos + inTypes, length - argpos);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   885
        for (int i = 0; i < inTypes; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   886
            names2[argpos + i] = new Name(types[i]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   887
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   888
        return new LambdaForm(debugName, arity2, names2, result2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   889
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   890
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   891
    LambdaForm addArguments(int pos, List<Class<?>> types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   892
        char[] basicTypes = new char[types.size()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   893
        for (int i = 0; i < basicTypes.length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   894
            basicTypes[i] = basicType(types.get(i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   895
        return addArguments(pos, basicTypes);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   896
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   897
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   898
    LambdaForm permuteArguments(int skip, int[] reorder, char[] types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   899
        // Note:  When inArg = reorder[outArg], outArg is fed by a copy of inArg.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   900
        // The types are the types of the new (incoming) arguments.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   901
        int length = names.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   902
        int inTypes = types.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   903
        int outArgs = reorder.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   904
        assert(skip+outArgs == arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   905
        assert(permutedTypesMatch(reorder, types, names, skip));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   906
        int pos = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   907
        // skip trivial first part of reordering:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   908
        while (pos < outArgs && reorder[pos] == pos)  pos += 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   909
        Name[] names2 = new Name[length - outArgs + inTypes];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   910
        System.arraycopy(names, 0, names2, 0, skip+pos);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   911
        // copy the body:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   912
        int bodyLength = length - arity;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   913
        System.arraycopy(names, skip+outArgs, names2, skip+inTypes, bodyLength);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   914
        int arity2 = names2.length - bodyLength;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   915
        int result2 = result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   916
        if (result2 >= 0) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   917
            if (result2 < skip+outArgs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   918
                // return the corresponding inArg
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   919
                result2 = reorder[result2-skip];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   920
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   921
                result2 = result2 - outArgs + inTypes;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   922
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   923
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   924
        // rework names in the body:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   925
        for (int j = pos; j < outArgs; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   926
            Name n = names[skip+j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   927
            int i = reorder[j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   928
            // replace names[skip+j] by names2[skip+i]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   929
            Name n2 = names2[skip+i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   930
            if (n2 == null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   931
                names2[skip+i] = n2 = new Name(types[i]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   932
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   933
                assert(n2.type == types[i]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   934
            for (int k = arity2; k < names2.length; k++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   935
                names2[k] = names2[k].replaceName(n, n2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   936
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   937
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   938
        // some names are unused, but must be filled in
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   939
        for (int i = skip+pos; i < arity2; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   940
            if (names2[i] == null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   941
                names2[i] = argument(i, types[i - skip]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   942
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   943
        for (int j = arity; j < names.length; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   944
            int i = j - arity + arity2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   945
            // replace names2[i] by names[j]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   946
            Name n = names[j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   947
            Name n2 = names2[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   948
            if (n != n2) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   949
                for (int k = i+1; k < names2.length; k++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   950
                    names2[k] = names2[k].replaceName(n, n2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   951
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   952
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   953
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   954
        return new LambdaForm(debugName, arity2, names2, result2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   955
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   956
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   957
    static boolean permutedTypesMatch(int[] reorder, char[] types, Name[] names, int skip) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   958
        int inTypes = types.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   959
        int outArgs = reorder.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   960
        for (int i = 0; i < outArgs; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   961
            assert(names[skip+i].isParam());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   962
            assert(names[skip+i].type == types[reorder[i]]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   963
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   964
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   965
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   966
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   967
    static class NamedFunction {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   968
        final MemberName member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   969
        MethodHandle resolvedHandle;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   970
        MethodHandle invoker;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   971
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   972
        NamedFunction(MethodHandle resolvedHandle) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   973
            this(resolvedHandle.internalMemberName(), resolvedHandle);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   974
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   975
        NamedFunction(MemberName member, MethodHandle resolvedHandle) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   976
            this.member = member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   977
            //resolvedHandle = eraseSubwordTypes(resolvedHandle);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   978
            this.resolvedHandle = resolvedHandle;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   979
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   980
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   981
        // The next 3 constructors are used to break circular dependencies on MH.invokeStatic, etc.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   982
        // Any LambdaForm containing such a member is not interpretable.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   983
        // This is OK, since all such LFs are prepared with special primitive vmentry points.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   984
        // And even without the resolvedHandle, the name can still be compiled and optimized.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   985
        NamedFunction(Method method) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   986
            this(new MemberName(method));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   987
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   988
        NamedFunction(Field field) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   989
            this(new MemberName(field));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   990
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   991
        NamedFunction(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   992
            this.member = member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   993
            this.resolvedHandle = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   994
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   995
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   996
        MethodHandle resolvedHandle() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   997
            if (resolvedHandle == null)  resolve();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   998
            return resolvedHandle;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   999
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1000
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1001
        void resolve() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1002
            resolvedHandle = DirectMethodHandle.make(member);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1003
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1004
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1005
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1006
        public boolean equals(Object other) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1007
            if (this == other) return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1008
            if (other == null) return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1009
            if (!(other instanceof NamedFunction)) return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1010
            NamedFunction that = (NamedFunction) other;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1011
            return this.member != null && this.member.equals(that.member);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1012
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1013
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1014
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1015
        public int hashCode() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1016
            if (member != null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1017
                return member.hashCode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1018
            return super.hashCode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1019
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1020
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1021
        // Put the predefined NamedFunction invokers into the table.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1022
        static void initializeInvokers() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1023
            for (MemberName m : MemberName.getFactory().getMethods(NamedFunction.class, false, null, null, null)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1024
                if (!m.isStatic() || !m.isPackage())  continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1025
                MethodType type = m.getMethodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1026
                if (type.equals(INVOKER_METHOD_TYPE) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1027
                    m.getName().startsWith("invoke_")) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1028
                    String sig = m.getName().substring("invoke_".length());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1029
                    int arity = LambdaForm.signatureArity(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1030
                    MethodType srcType = MethodType.genericMethodType(arity);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1031
                    if (LambdaForm.signatureReturn(sig) == 'V')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1032
                        srcType = srcType.changeReturnType(void.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1033
                    MethodTypeForm typeForm = srcType.form();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1034
                    typeForm.namedFunctionInvoker = DirectMethodHandle.make(m);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1035
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1036
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1037
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1038
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1039
        // The following are predefined NamedFunction invokers.  The system must build
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1040
        // a separate invoker for each distinct signature.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1041
        /** void return type invokers. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1042
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1043
        static Object invoke__V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1044
            assert(a.length == 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1045
            mh.invokeBasic();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1046
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1047
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1048
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1049
        static Object invoke_L_V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1050
            assert(a.length == 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1051
            mh.invokeBasic(a[0]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1052
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1053
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1054
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1055
        static Object invoke_LL_V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1056
            assert(a.length == 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1057
            mh.invokeBasic(a[0], a[1]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1058
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1059
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1060
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1061
        static Object invoke_LLL_V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1062
            assert(a.length == 3);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1063
            mh.invokeBasic(a[0], a[1], a[2]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1064
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1065
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1066
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1067
        static Object invoke_LLLL_V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1068
            assert(a.length == 4);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1069
            mh.invokeBasic(a[0], a[1], a[2], a[3]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1070
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1071
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1072
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1073
        static Object invoke_LLLLL_V(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1074
            assert(a.length == 5);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1075
            mh.invokeBasic(a[0], a[1], a[2], a[3], a[4]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1076
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1077
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1078
        /** Object return type invokers. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1079
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1080
        static Object invoke__L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1081
            assert(a.length == 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1082
            return mh.invokeBasic();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1083
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1084
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1085
        static Object invoke_L_L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1086
            assert(a.length == 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1087
            return mh.invokeBasic(a[0]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1088
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1089
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1090
        static Object invoke_LL_L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1091
            assert(a.length == 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1092
            return mh.invokeBasic(a[0], a[1]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1093
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1094
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1095
        static Object invoke_LLL_L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1096
            assert(a.length == 3);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1097
            return mh.invokeBasic(a[0], a[1], a[2]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1098
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1099
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1100
        static Object invoke_LLLL_L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1101
            assert(a.length == 4);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1102
            return mh.invokeBasic(a[0], a[1], a[2], a[3]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1103
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1104
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1105
        static Object invoke_LLLLL_L(MethodHandle mh, Object[] a) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1106
            assert(a.length == 5);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1107
            return mh.invokeBasic(a[0], a[1], a[2], a[3], a[4]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1108
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1109
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1110
        static final MethodType INVOKER_METHOD_TYPE =
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1111
            MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1112
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1113
        private static MethodHandle computeInvoker(MethodTypeForm typeForm) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1114
            MethodHandle mh = typeForm.namedFunctionInvoker;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1115
            if (mh != null)  return mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1116
            MemberName invoker = InvokerBytecodeGenerator.generateNamedFunctionInvoker(typeForm);  // this could take a while
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1117
            mh = DirectMethodHandle.make(invoker);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1118
            MethodHandle mh2 = typeForm.namedFunctionInvoker;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1119
            if (mh2 != null)  return mh2;  // benign race
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1120
            if (!mh.type().equals(INVOKER_METHOD_TYPE))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1121
                throw new InternalError(mh.debugString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1122
            return typeForm.namedFunctionInvoker = mh;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1123
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1124
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1125
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1126
        Object invokeWithArguments(Object... arguments) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1127
            // If we have a cached invoker, call it right away.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1128
            // NOTE: The invoker always returns a reference value.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1129
            if (TRACE_INTERPRETER)  return invokeWithArgumentsTracing(arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1130
            assert(checkArgumentTypes(arguments, methodType()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1131
            return invoker().invokeBasic(resolvedHandle(), arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1132
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1133
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1134
        @Hidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1135
        Object invokeWithArgumentsTracing(Object[] arguments) throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1136
            Object rval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1137
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1138
                traceInterpreter("[ call", this, arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1139
                if (invoker == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1140
                    traceInterpreter("| getInvoker", this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1141
                    invoker();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1142
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1143
                if (resolvedHandle == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1144
                    traceInterpreter("| resolve", this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1145
                    resolvedHandle();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1146
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1147
                assert(checkArgumentTypes(arguments, methodType()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1148
                rval = invoker().invokeBasic(resolvedHandle(), arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1149
            } catch (Throwable ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1150
                traceInterpreter("] throw =>", ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1151
                throw ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1152
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1153
            traceInterpreter("] return =>", rval);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1154
            return rval;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1155
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1156
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1157
        private MethodHandle invoker() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1158
            if (invoker != null)  return invoker;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1159
            // Get an invoker and cache it.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1160
            return invoker = computeInvoker(methodType().form());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1161
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1162
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1163
        private static boolean checkArgumentTypes(Object[] arguments, MethodType methodType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1164
            if (true)  return true;  // FIXME
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1165
            MethodType dstType = methodType.form().erasedType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1166
            MethodType srcType = dstType.basicType().wrap();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1167
            Class<?>[] ptypes = new Class<?>[arguments.length];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1168
            for (int i = 0; i < arguments.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1169
                Object arg = arguments[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1170
                Class<?> ptype = arg == null ? Object.class : arg.getClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1171
                // If the dest. type is a primitive we keep the
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1172
                // argument type.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1173
                ptypes[i] = dstType.parameterType(i).isPrimitive() ? ptype : Object.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1174
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1175
            MethodType argType = MethodType.methodType(srcType.returnType(), ptypes).wrap();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1176
            assert(argType.isConvertibleTo(srcType)) : "wrong argument types: cannot convert " + argType + " to " + srcType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1177
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1178
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1179
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1180
        String basicTypeSignature() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1181
            //return LambdaForm.basicTypeSignature(resolvedHandle.type());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1182
            return LambdaForm.basicTypeSignature(methodType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1183
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1184
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1185
        MethodType methodType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1186
            if (resolvedHandle != null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1187
                return resolvedHandle.type();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1188
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1189
                // only for certain internal LFs during bootstrapping
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1190
                return member.getInvocationType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1191
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1192
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1193
        MemberName member() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1194
            assert(assertMemberIsConsistent());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1195
            return member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1196
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1197
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1198
        // Called only from assert.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1199
        private boolean assertMemberIsConsistent() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1200
            if (resolvedHandle instanceof DirectMethodHandle) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1201
                MemberName m = resolvedHandle.internalMemberName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1202
                assert(m.equals(member));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1203
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1204
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1205
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1206
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1207
        Class<?> memberDeclaringClassOrNull() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1208
            return (member == null) ? null : member.getDeclaringClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1209
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1210
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1211
        char returnType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1212
            return basicType(methodType().returnType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1213
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1214
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1215
        char parameterType(int n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1216
            return basicType(methodType().parameterType(n));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1217
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1218
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1219
        int arity() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1220
            //int siglen = member.getMethodType().parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1221
            //if (!member.isStatic())  siglen += 1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1222
            //return siglen;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1223
            return methodType().parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1224
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1225
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1226
        public String toString() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1227
            if (member == null)  return resolvedHandle.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1228
            return member.getDeclaringClass().getSimpleName()+"."+member.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1229
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1230
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1231
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1232
    void resolve() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1233
        for (Name n : names) n.resolve();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1234
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1235
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1236
    public static char basicType(Class<?> type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1237
        char c = Wrapper.basicTypeChar(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1238
        if ("ZBSC".indexOf(c) >= 0)  c = 'I';
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1239
        assert("LIJFDV".indexOf(c) >= 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1240
        return c;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1241
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1242
    public static char[] basicTypes(List<Class<?>> types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1243
        char[] btypes = new char[types.size()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1244
        for (int i = 0; i < btypes.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1245
            btypes[i] = basicType(types.get(i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1246
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1247
        return btypes;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1248
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1249
    public static String basicTypeSignature(MethodType type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1250
        char[] sig = new char[type.parameterCount() + 2];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1251
        int sigp = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1252
        for (Class<?> pt : type.parameterList()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1253
            sig[sigp++] = basicType(pt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1254
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1255
        sig[sigp++] = '_';
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1256
        sig[sigp++] = basicType(type.returnType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1257
        assert(sigp == sig.length);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1258
        return String.valueOf(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1259
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1260
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1261
    static final class Name {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1262
        final char type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1263
        private short index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1264
        final NamedFunction function;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1265
        final Object[] arguments;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1266
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1267
        private Name(int index, char type, NamedFunction function, Object[] arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1268
            this.index = (short)index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1269
            this.type = type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1270
            this.function = function;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1271
            this.arguments = arguments;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1272
            assert(this.index == index);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1273
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1274
        Name(MethodHandle function, Object... arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1275
            this(new NamedFunction(function), arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1276
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1277
        Name(MemberName function, Object... arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1278
            this(new NamedFunction(function), arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1279
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1280
        Name(NamedFunction function, Object... arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1281
            this(-1, function.returnType(), function, arguments = arguments.clone());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1282
            assert(arguments.length == function.arity()) : "arity mismatch: arguments.length=" + arguments.length + " == function.arity()=" + function.arity() + " in " + debugString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1283
            for (int i = 0; i < arguments.length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1284
                assert(typesMatch(function.parameterType(i), arguments[i])) : "types don't match: function.parameterType(" + i + ")=" + function.parameterType(i) + ", arguments[" + i + "]=" + arguments[i] + " in " + debugString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1285
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1286
        Name(int index, char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1287
            this(index, type, null, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1288
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1289
        Name(char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1290
            this(-1, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1291
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1292
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1293
        char type() { return type; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1294
        int index() { return index; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1295
        boolean initIndex(int i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1296
            if (index != i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1297
                if (index != -1)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1298
                index = (short)i;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1299
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1300
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1301
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1302
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1303
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1304
        void resolve() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1305
            if (function != null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1306
                function.resolve();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1307
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1308
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1309
        Name newIndex(int i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1310
            if (initIndex(i))  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1311
            return cloneWithIndex(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1312
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1313
        Name cloneWithIndex(int i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1314
            Object[] newArguments = (arguments == null) ? null : arguments.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1315
            return new Name(i, type, function, newArguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1316
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1317
        Name replaceName(Name oldName, Name newName) {  // FIXME: use replaceNames uniformly
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1318
            if (oldName == newName)  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1319
            @SuppressWarnings("LocalVariableHidesMemberVariable")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1320
            Object[] arguments = this.arguments;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1321
            if (arguments == null)  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1322
            boolean replaced = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1323
            for (int j = 0; j < arguments.length; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1324
                if (arguments[j] == oldName) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1325
                    if (!replaced) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1326
                        replaced = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1327
                        arguments = arguments.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1328
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1329
                    arguments[j] = newName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1330
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1331
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1332
            if (!replaced)  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1333
            return new Name(function, arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1334
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1335
        Name replaceNames(Name[] oldNames, Name[] newNames, int start, int end) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1336
            @SuppressWarnings("LocalVariableHidesMemberVariable")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1337
            Object[] arguments = this.arguments;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1338
            boolean replaced = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1339
        eachArg:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1340
            for (int j = 0; j < arguments.length; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1341
                if (arguments[j] instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1342
                    Name n = (Name) arguments[j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1343
                    int check = n.index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1344
                    // harmless check to see if the thing is already in newNames:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1345
                    if (check >= 0 && check < newNames.length && n == newNames[check])
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1346
                        continue eachArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1347
                    // n might not have the correct index: n != oldNames[n.index].
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1348
                    for (int i = start; i < end; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1349
                        if (n == oldNames[i]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1350
                            if (n == newNames[i])
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1351
                                continue eachArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1352
                            if (!replaced) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1353
                                replaced = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1354
                                arguments = arguments.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1355
                            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1356
                            arguments[j] = newNames[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1357
                            continue eachArg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1358
                        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1359
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1360
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1361
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1362
            if (!replaced)  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1363
            return new Name(function, arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1364
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1365
        void internArguments() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1366
            @SuppressWarnings("LocalVariableHidesMemberVariable")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1367
            Object[] arguments = this.arguments;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1368
            for (int j = 0; j < arguments.length; j++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1369
                if (arguments[j] instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1370
                    Name n = (Name) arguments[j];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1371
                    if (n.isParam() && n.index < INTERNED_ARGUMENT_LIMIT)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1372
                        arguments[j] = internArgument(n);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1373
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1374
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1375
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1376
        boolean isParam() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1377
            return function == null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1378
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1379
        boolean isConstantZero() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1380
            return !isParam() && arguments.length == 0 && function.equals(constantZero(0, type).function);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1381
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1382
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1383
        public String toString() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1384
            return (isParam()?"a":"t")+(index >= 0 ? index : System.identityHashCode(this))+":"+type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1385
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1386
        public String debugString() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1387
            String s = toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1388
            return (function == null) ? s : s + "=" + exprString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1389
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1390
        public String exprString() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1391
            if (function == null)  return "null";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1392
            StringBuilder buf = new StringBuilder(function.toString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1393
            buf.append("(");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1394
            String cma = "";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1395
            for (Object a : arguments) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1396
                buf.append(cma); cma = ",";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1397
                if (a instanceof Name || a instanceof Integer)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1398
                    buf.append(a);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1399
                else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1400
                    buf.append("(").append(a).append(")");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1401
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1402
            buf.append(")");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1403
            return buf.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1404
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1405
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1406
        private static boolean typesMatch(char parameterType, Object object) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1407
            if (object instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1408
                return ((Name)object).type == parameterType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1409
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1410
            switch (parameterType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1411
                case 'I':  return object instanceof Integer;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1412
                case 'J':  return object instanceof Long;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1413
                case 'F':  return object instanceof Float;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1414
                case 'D':  return object instanceof Double;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1415
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1416
            assert(parameterType == 'L');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1417
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1418
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1419
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1420
        /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1421
         * Does this Name precede the given binding node in some canonical order?
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1422
         * This predicate is used to order data bindings (via insertion sort)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1423
         * with some stability.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1424
         * @param binding
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1425
         * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1426
         */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1427
        boolean isSiblingBindingBefore(Name binding) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1428
            assert(!binding.isParam());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1429
            if (isParam())  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1430
            if (function.equals(binding.function) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1431
                arguments.length == binding.arguments.length) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1432
                boolean sawInt = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1433
                for (int i = 0; i < arguments.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1434
                    Object a1 = arguments[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1435
                    Object a2 = binding.arguments[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1436
                    if (!a1.equals(a2)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1437
                        if (a1 instanceof Integer && a2 instanceof Integer) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1438
                            if (sawInt)  continue;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1439
                            sawInt = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1440
                            if ((int)a1 < (int)a2)  continue;  // still might be true
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1441
                        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1442
                        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1443
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1444
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1445
                return sawInt;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1446
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1447
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1448
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1449
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1450
        public boolean equals(Name that) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1451
            if (this == that)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1452
            if (isParam())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1453
                // each parameter is a unique atom
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1454
                return false;  // this != that
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1455
            return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1456
                //this.index == that.index &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1457
                this.type == that.type &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1458
                this.function.equals(that.function) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1459
                Arrays.equals(this.arguments, that.arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1460
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1461
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1462
        public boolean equals(Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1463
            return x instanceof Name && equals((Name)x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1464
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1465
        @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1466
        public int hashCode() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1467
            if (isParam())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1468
                return index | (type << 8);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1469
            return function.hashCode() ^ Arrays.hashCode(arguments);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1470
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1471
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1472
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1473
    static Name argument(int which, char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1474
        int tn = ALL_TYPES.indexOf(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1475
        if (tn < 0 || which >= INTERNED_ARGUMENT_LIMIT)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1476
            return new Name(which, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1477
        return INTERNED_ARGUMENTS[tn][which];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1478
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1479
    static Name internArgument(Name n) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1480
        assert(n.isParam()) : "not param: " + n;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1481
        assert(n.index < INTERNED_ARGUMENT_LIMIT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1482
        return argument(n.index, n.type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1483
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1484
    static Name[] arguments(int extra, String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1485
        int length = types.length();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1486
        Name[] names = new Name[length + extra];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1487
        for (int i = 0; i < length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1488
            names[i] = argument(i, types.charAt(i));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1489
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1490
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1491
    static Name[] arguments(int extra, char... types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1492
        int length = types.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1493
        Name[] names = new Name[length + extra];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1494
        for (int i = 0; i < length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1495
            names[i] = argument(i, types[i]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1496
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1497
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1498
    static Name[] arguments(int extra, List<Class<?>> types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1499
        int length = types.size();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1500
        Name[] names = new Name[length + extra];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1501
        for (int i = 0; i < length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1502
            names[i] = argument(i, basicType(types.get(i)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1503
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1504
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1505
    static Name[] arguments(int extra, Class<?>... types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1506
        int length = types.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1507
        Name[] names = new Name[length + extra];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1508
        for (int i = 0; i < length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1509
            names[i] = argument(i, basicType(types[i]));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1510
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1511
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1512
    static Name[] arguments(int extra, MethodType types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1513
        int length = types.parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1514
        Name[] names = new Name[length + extra];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1515
        for (int i = 0; i < length; i++)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1516
            names[i] = argument(i, basicType(types.parameterType(i)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1517
        return names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1518
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1519
    static final String ALL_TYPES = "LIJFD";  // omit V, not an argument type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1520
    static final int INTERNED_ARGUMENT_LIMIT = 10;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1521
    private static final Name[][] INTERNED_ARGUMENTS
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1522
            = new Name[ALL_TYPES.length()][INTERNED_ARGUMENT_LIMIT];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1523
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1524
        for (int tn = 0; tn < ALL_TYPES.length(); tn++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1525
            for (int i = 0; i < INTERNED_ARGUMENTS[tn].length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1526
                char type = ALL_TYPES.charAt(tn);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1527
                INTERNED_ARGUMENTS[tn][i] = new Name(i, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1528
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1529
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1530
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1531
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1532
    private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1533
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1534
    static Name constantZero(int which, char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1535
        return CONSTANT_ZERO[ALL_TYPES.indexOf(type)].newIndex(which);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1536
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1537
    private static final Name[] CONSTANT_ZERO
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1538
            = new Name[ALL_TYPES.length()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1539
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1540
        for (int tn = 0; tn < ALL_TYPES.length(); tn++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1541
            char bt = ALL_TYPES.charAt(tn);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1542
            Wrapper wrap = Wrapper.forBasicType(bt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1543
            MemberName zmem = new MemberName(LambdaForm.class, "zero"+bt, MethodType.methodType(wrap.primitiveType()), REF_invokeStatic);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1544
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1545
                zmem = IMPL_NAMES.resolveOrFail(REF_invokeStatic, zmem, null, NoSuchMethodException.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1546
            } catch (IllegalAccessException|NoSuchMethodException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1547
                throw new InternalError(ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1548
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1549
            NamedFunction zcon = new NamedFunction(zmem);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1550
            Name n = new Name(zcon).newIndex(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1551
            assert(n.type == ALL_TYPES.charAt(tn));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1552
            CONSTANT_ZERO[tn] = n;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1553
            assert(n.isConstantZero());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1554
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1555
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1556
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1557
    // Avoid appealing to ValueConversions at bootstrap time:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1558
    private static int zeroI() { return 0; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1559
    private static long zeroJ() { return 0; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1560
    private static float zeroF() { return 0; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1561
    private static double zeroD() { return 0; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1562
    private static Object zeroL() { return null; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1563
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1564
    // Put this last, so that previous static inits can run before.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1565
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1566
        if (USE_PREDEFINED_INTERPRET_METHODS)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1567
            PREPARED_FORMS.putAll(computeInitialPreparedForms());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1568
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1569
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1570
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1571
     * Internal marker for byte-compiled LambdaForms.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1572
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1573
    /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1574
    @Target(ElementType.METHOD)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1575
    @Retention(RetentionPolicy.RUNTIME)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1576
    @interface Compiled {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1577
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1578
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1579
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1580
     * Internal marker for LambdaForm interpreter frames.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1581
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1582
    /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1583
    @Target(ElementType.METHOD)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1584
    @Retention(RetentionPolicy.RUNTIME)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1585
    @interface Hidden {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1586
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1587
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1588
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1589
/*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1590
    // Smoke-test for the invokers used in this file.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1591
    static void testMethodHandleLinkers() throws Throwable {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1592
        MemberName.Factory lookup = MemberName.getFactory();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1593
        MemberName asList_MN = new MemberName(Arrays.class, "asList",
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1594
                                              MethodType.methodType(List.class, Object[].class),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1595
                                              REF_invokeStatic);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1596
        //MethodHandleNatives.resolve(asList_MN, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1597
        asList_MN = lookup.resolveOrFail(asList_MN, REF_invokeStatic, null, NoSuchMethodException.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1598
        System.out.println("about to call "+asList_MN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1599
        Object[] abc = { "a", "bc" };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1600
        List<?> lst = (List<?>) MethodHandle.linkToStatic(abc, asList_MN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1601
        System.out.println("lst="+lst);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1602
        MemberName toString_MN = new MemberName(Object.class.getMethod("toString"));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1603
        String s1 = (String) MethodHandle.linkToVirtual(lst, toString_MN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1604
        toString_MN = new MemberName(Object.class.getMethod("toString"), true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1605
        String s2 = (String) MethodHandle.linkToSpecial(lst, toString_MN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1606
        System.out.println("[s1,s2,lst]="+Arrays.asList(s1, s2, lst.toString()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1607
        MemberName toArray_MN = new MemberName(List.class.getMethod("toArray"));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1608
        Object[] arr = (Object[]) MethodHandle.linkToInterface(lst, toArray_MN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1609
        System.out.println("toArray="+Arrays.toString(arr));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1610
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1611
    static { try { testMethodHandleLinkers(); } catch (Throwable ex) { throw new RuntimeException(ex); } }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1612
    // Requires these definitions in MethodHandle:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1613
    static final native Object linkToStatic(Object x1, MemberName mn) throws Throwable;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1614
    static final native Object linkToVirtual(Object x1, MemberName mn) throws Throwable;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1615
    static final native Object linkToSpecial(Object x1, MemberName mn) throws Throwable;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1616
    static final native Object linkToInterface(Object x1, MemberName mn) throws Throwable;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1617
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1618
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1619
    static { NamedFunction.initializeInvokers(); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1620
}