jdk/src/java.base/share/classes/java/lang/invoke/MethodTypeForm.java
author vlivanov
Wed, 10 Sep 2014 19:19:47 +0400
changeset 26467 d69abed3a07d
parent 25859 3317bb8137f4
child 26468 2d57604f9299
permissions -rw-r--r--
8050052: Small cleanups in java.lang.invoke code Reviewed-by: vlivanov, psandoz Contributed-by: john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20527
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2707
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2707
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2707
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2707
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2707
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang.invoke;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    28
import sun.invoke.util.Wrapper;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    29
import static java.lang.invoke.MethodHandleStatics.*;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    30
import static java.lang.invoke.MethodHandleNatives.Constants.*;
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20494
diff changeset
    31
 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    32
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
/**
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    34
 * Shared information for a group of method types, which differ
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    35
 * only by reference types, and therefore share a common erasure
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    36
 * and wrapping.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    37
 * <p>
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    38
 * For an empirical discussion of the structure of method types,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    39
 * see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/">
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    40
 * the thread "Avoiding Boxing" on jvm-languages</a>.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    41
 * There are approximately 2000 distinct erased method types in the JDK.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    42
 * There are a little over 10 times that number of unerased types.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    43
 * No more than half of these are likely to be loaded at once.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    44
 * @author John Rose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    45
 */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    46
final class MethodTypeForm {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    47
    final int[] argToSlotTable, slotToArgTable;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    48
    final long argCounts;               // packed slot & value counts
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    49
    final long primCounts;              // packed prim & double counts
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    50
    final MethodType erasedType;        // the canonical erasure
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    51
    final MethodType basicType;         // the canonical erasure, with primitives simplified
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    52
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    53
    // Cached adapter information:
20494
cf77ae5ff678 8001107: @Stable annotation for constant folding of lazily evaluated variables
vlivanov
parents: 14342
diff changeset
    54
    @Stable MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    55
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    56
    // Cached lambda form information, for basic types only:
20494
cf77ae5ff678 8001107: @Stable annotation for constant folding of lazily evaluated variables
vlivanov
parents: 14342
diff changeset
    57
    final @Stable LambdaForm[] lambdaForms;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    58
    // Indexes into lambdaForms:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    59
    static final int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    60
            LF_INVVIRTUAL     =  0,  // DMH invokeVirtual
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    61
            LF_INVSTATIC      =  1,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    62
            LF_INVSPECIAL     =  2,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    63
            LF_NEWINVSPECIAL  =  3,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    64
            LF_INVINTERFACE   =  4,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    65
            LF_INVSTATIC_INIT =  5,  // DMH invokeStatic with <clinit> barrier
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    66
            LF_INTERPRET      =  6,  // LF interpreter
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    67
            LF_COUNTER        =  7,  // CMH wrapper
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    68
            LF_REINVOKE       =  8,  // other wrapper
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    69
            LF_EX_LINKER      =  9,  // invokeExact_MT (for invokehandle)
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    70
            LF_EX_INVOKER     = 10,  // MHs.invokeExact
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    71
            LF_GEN_LINKER     = 11,  // generic invoke_MT (for invokehandle)
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    72
            LF_GEN_INVOKER    = 12,  // generic MHs.invoke
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    73
            LF_CS_LINKER      = 13,  // linkToCallSite_CS
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20494
diff changeset
    74
            LF_MH_LINKER      = 14,  // linkToCallSite_MH
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    75
            LF_GWC            = 15,  // guardWithCatch (catchException)
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
    76
            LF_LIMIT          = 16;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    77
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    78
    /** Return the type corresponding uniquely (1-1) to this MT-form.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    79
     *  It might have any primitive returns or arguments, but will have no references except Object.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    80
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    81
    public MethodType erasedType() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    82
        return erasedType;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    83
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
    84
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    85
    /** Return the basic type derived from the erased type of this MT-form.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    86
     *  A basic type is erased (all references Object) and also has all primitive
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    87
     *  types (except int, long, float, double, void) normalized to int.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    88
     *  Such basic types correspond to low-level JVM calling sequences.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    89
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    90
    public MethodType basicType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    91
        return basicType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    92
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
    93
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    94
    private boolean assertIsBasicType() {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    95
        // primitives must be flattened also
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    96
        assert(erasedType == basicType)
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    97
                : "erasedType: " + erasedType + " != basicType: " + basicType;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    98
        return true;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    99
    }
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   100
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   101
    public LambdaForm cachedLambdaForm(int which) {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   102
        assert(assertIsBasicType());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   103
        return lambdaForms[which];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   104
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   105
24696
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 23038
diff changeset
   106
    synchronized public LambdaForm setCachedLambdaForm(int which, LambdaForm form) {
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 23038
diff changeset
   107
        // Simulate a CAS, to avoid racy duplication of results.
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 23038
diff changeset
   108
        LambdaForm prev = lambdaForms[which];
00479fa7c27f 8005873: JRuby test_respond_to.rb asserts with: MT-unsafe modification of inline cache
thartmann
parents: 23038
diff changeset
   109
        if (prev != null) return prev;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   110
        return lambdaForms[which] = form;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   111
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   112
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   113
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   114
     * Build an MTF for a given type, which must have all references erased to Object.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   115
     * This MTF will stand for that type and all un-erased variations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   116
     * Eagerly compute some basic properties of the type, common to all variations.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   117
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   118
    protected MethodTypeForm(MethodType erasedType) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   119
        this.erasedType = erasedType;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   120
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   121
        Class<?>[] ptypes = erasedType.ptypes();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   122
        int ptypeCount = ptypes.length;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   123
        int pslotCount = ptypeCount;            // temp. estimate
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   124
        int rtypeCount = 1;                     // temp. estimate
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   125
        int rslotCount = 1;                     // temp. estimate
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   126
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   127
        int[] argToSlotTab = null, slotToArgTab = null;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   128
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   129
        // Walk the argument types, looking for primitives.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   130
        int pac = 0, lac = 0, prc = 0, lrc = 0;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   131
        Class<?>[] epts = ptypes;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   132
        Class<?>[] bpts = epts;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   133
        for (int i = 0; i < epts.length; i++) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   134
            Class<?> pt = epts[i];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   135
            if (pt != Object.class) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   136
                ++pac;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   137
                Wrapper w = Wrapper.forPrimitiveType(pt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   138
                if (w.isDoubleWord())  ++lac;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   139
                if (w.isSubwordOrInt() && pt != int.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   140
                    if (bpts == epts)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   141
                        bpts = bpts.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   142
                    bpts[i] = int.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   143
                }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   144
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   145
        }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   146
        pslotCount += lac;                  // #slots = #args + #longs
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   147
        Class<?> rt = erasedType.returnType();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   148
        Class<?> bt = rt;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   149
        if (rt != Object.class) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   150
            ++prc;          // even void.class counts as a prim here
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   151
            Wrapper w = Wrapper.forPrimitiveType(rt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   152
            if (w.isDoubleWord())  ++lrc;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   153
            if (w.isSubwordOrInt() && rt != int.class)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   154
                bt = int.class;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   155
            // adjust #slots, #args
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   156
            if (rt == void.class)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   157
                rtypeCount = rslotCount = 0;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   158
            else
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   159
                rslotCount += lrc;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   160
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   161
        if (epts == bpts && bt == rt) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   162
            this.basicType = erasedType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   163
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   164
            this.basicType = MethodType.makeImpl(bt, bpts, true);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   165
            // fill in rest of data from the basic type:
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   166
            MethodTypeForm that = this.basicType.form();
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   167
            assert(this != that);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   168
            this.primCounts = that.primCounts;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   169
            this.argCounts = that.argCounts;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   170
            this.argToSlotTable = that.argToSlotTable;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   171
            this.slotToArgTable = that.slotToArgTable;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   172
            this.lambdaForms = null;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   173
            return;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   174
        }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   175
        if (lac != 0) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   176
            int slot = ptypeCount + lac;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   177
            slotToArgTab = new int[slot+1];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   178
            argToSlotTab = new int[1+ptypeCount];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   179
            argToSlotTab[0] = slot;  // argument "-1" is past end of slots
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   180
            for (int i = 0; i < epts.length; i++) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   181
                Class<?> pt = epts[i];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   182
                Wrapper w = Wrapper.forBasicType(pt);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   183
                if (w.isDoubleWord())  --slot;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   184
                --slot;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   185
                slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   186
                argToSlotTab[1+i]  = slot;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   187
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   188
            assert(slot == 0);  // filled the table
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   189
        } else if (pac != 0) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   190
            // have primitives but no long primitives; share slot counts with generic
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   191
            assert(ptypeCount == pslotCount);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   192
            MethodTypeForm that = MethodType.genericMethodType(ptypeCount).form();
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   193
            assert(this != that);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   194
            slotToArgTab = that.slotToArgTable;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   195
            argToSlotTab = that.argToSlotTable;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   196
        } else {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   197
            int slot = ptypeCount; // first arg is deepest in stack
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   198
            slotToArgTab = new int[slot+1];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   199
            argToSlotTab = new int[1+ptypeCount];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   200
            argToSlotTab[0] = slot;  // argument "-1" is past end of slots
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   201
            for (int i = 0; i < ptypeCount; i++) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   202
                --slot;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   203
                slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   204
                argToSlotTab[1+i]  = slot;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   205
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   206
        }
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   207
        this.primCounts = pack(lrc, prc, lac, pac);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   208
        this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   209
        this.argToSlotTable = argToSlotTab;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   210
        this.slotToArgTable = slotToArgTab;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   211
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   212
        if (pslotCount >= 256)  throw newIllegalArgumentException("too many arguments");
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   213
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   214
        // Initialize caches, but only for basic types
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   215
        assert(basicType == erasedType);
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   216
        this.lambdaForms = new LambdaForm[LF_LIMIT];
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   217
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   218
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   219
    private static long pack(int a, int b, int c, int d) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   220
        assert(((a|b|c|d) & ~0xFFFF) == 0);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   221
        long hw = ((a << 16) | b), lw = ((c << 16) | d);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   222
        return (hw << 32) | lw;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   223
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   224
    private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   225
        assert(word <= 3);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   226
        return (char)(packed >> ((3-word) * 16));
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   227
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   228
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   229
    public int parameterCount() {                      // # outgoing values
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   230
        return unpack(argCounts, 3);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   231
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   232
    public int parameterSlotCount() {                  // # outgoing interpreter slots
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   233
        return unpack(argCounts, 2);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   234
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   235
    public int returnCount() {                         // = 0 (V), or 1
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   236
        return unpack(argCounts, 1);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   237
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   238
    public int returnSlotCount() {                     // = 0 (V), 2 (J/D), or 1
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   239
        return unpack(argCounts, 0);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   240
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   241
    public int primitiveParameterCount() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   242
        return unpack(primCounts, 3);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   243
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   244
    public int longPrimitiveParameterCount() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   245
        return unpack(primCounts, 2);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   246
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   247
    public int primitiveReturnCount() {                // = 0 (obj), or 1
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   248
        return unpack(primCounts, 1);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   249
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   250
    public int longPrimitiveReturnCount() {            // = 1 (J/D), or 0
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   251
        return unpack(primCounts, 0);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   252
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   253
    public boolean hasPrimitives() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   254
        return primCounts != 0;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   255
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   256
    public boolean hasNonVoidPrimitives() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   257
        if (primCounts == 0)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   258
        if (primitiveParameterCount() != 0)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   259
        return (primitiveReturnCount() != 0 && returnCount() != 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 10419
diff changeset
   260
    }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   261
    public boolean hasLongPrimitives() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   262
        return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   263
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   264
    public int parameterToArgSlot(int i) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   265
        return argToSlotTable[1+i];
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   266
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   267
    public int argSlotToParameter(int argSlot) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   268
        // Note:  Empty slots are represented by zero in this table.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   269
        // Valid arguments slots contain incremented entries, so as to be non-zero.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   270
        // We return -1 the caller to mean an empty slot.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   271
        return slotToArgTable[argSlot] - 1;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   272
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   273
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   274
    static MethodTypeForm findForm(MethodType mt) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   275
        MethodType erased = canonicalize(mt, ERASE, ERASE);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   276
        if (erased == null) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   277
            // It is already erased.  Make a new MethodTypeForm.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   278
            return new MethodTypeForm(mt);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   279
        } else {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   280
            // Share the MethodTypeForm with the erased version.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   281
            return erased.form();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   282
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   283
    }
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   284
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   285
    /** Codes for {@link #canonicalize(java.lang.Class, int)}.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   286
     * ERASE means change every reference to {@code Object}.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   287
     * WRAP means convert primitives (including {@code void} to their
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   288
     * corresponding wrapper types.  UNWRAP means the reverse of WRAP.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   289
     * INTS means convert all non-void primitive types to int or long,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   290
     * according to size.  LONGS means convert all non-void primitives
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   291
     * to long, regardless of size.  RAW_RETURN means convert a type
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   292
     * (assumed to be a return type) to int if it is smaller than an int,
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   293
     * or if it is void.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   294
     */
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   295
    public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   296
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   297
    /** Canonicalize the types in the given method type.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   298
     * If any types change, intern the new type, and return it.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   299
     * Otherwise return null.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   300
     */
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   301
    public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   302
        Class<?>[] ptypes = mt.ptypes();
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   303
        Class<?>[] ptc = MethodTypeForm.canonicalizeAll(ptypes, howArgs);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   304
        Class<?> rtype = mt.returnType();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   305
        Class<?> rtc = MethodTypeForm.canonicalize(rtype, howRet);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   306
        if (ptc == null && rtc == null) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   307
            // It is already canonical.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   308
            return null;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   309
        }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   310
        // Find the erased version of the method type:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   311
        if (rtc == null)  rtc = rtype;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   312
        if (ptc == null)  ptc = ptypes;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   313
        return MethodType.makeImpl(rtc, ptc, true);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   314
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   315
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   316
    /** Canonicalize the given return or param type.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   317
     *  Return null if the type is already canonicalized.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   318
     */
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   319
    static Class<?> canonicalize(Class<?> t, int how) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   320
        Class<?> ct;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   321
        if (t == Object.class) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   322
            // no change, ever
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   323
        } else if (!t.isPrimitive()) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   324
            switch (how) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   325
                case UNWRAP:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   326
                    ct = Wrapper.asPrimitiveType(t);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   327
                    if (ct != t)  return ct;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   328
                    break;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   329
                case RAW_RETURN:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   330
                case ERASE:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   331
                    return Object.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   332
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   333
        } else if (t == void.class) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   334
            // no change, usually
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   335
            switch (how) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   336
                case RAW_RETURN:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   337
                    return int.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   338
                case WRAP:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   339
                    return Void.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   340
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   341
        } else {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   342
            // non-void primitive
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   343
            switch (how) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   344
                case WRAP:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   345
                    return Wrapper.asWrapperType(t);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   346
                case INTS:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   347
                    if (t == int.class || t == long.class)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   348
                        return null;  // no change
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   349
                    if (t == double.class)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   350
                        return long.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   351
                    return int.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   352
                case LONGS:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   353
                    if (t == long.class)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   354
                        return null;  // no change
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   355
                    return long.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   356
                case RAW_RETURN:
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   357
                    if (t == int.class || t == long.class ||
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   358
                        t == float.class || t == double.class)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   359
                        return null;  // no change
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   360
                    // everything else returns as an int
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   361
                    return int.class;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   362
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   363
        }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   364
        // no change; return null to signify
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   365
        return null;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   366
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   367
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   368
    /** Canonicalize each param type in the given array.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   369
     *  Return null if all types are already canonicalized.
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   370
     */
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   371
    static Class<?>[] canonicalizeAll(Class<?>[] ts, int how) {
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   372
        Class<?>[] cs = null;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   373
        for (int imax = ts.length, i = 0; i < imax; i++) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   374
            Class<?> c = canonicalize(ts[i], how);
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   375
            if (c == void.class)
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   376
                c = null;  // a Void parameter was unwrapped to void; ignore
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   377
            if (c != null) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   378
                if (cs == null)
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   379
                    cs = ts.clone();
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   380
                cs[i] = c;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   381
            }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   382
        }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   383
        return cs;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   384
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   385
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   386
    @Override
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   387
    public String toString() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   388
        return "Form"+erasedType;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   389
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 5506
diff changeset
   390
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   391
}