jdk/src/java.base/share/classes/java/lang/invoke/BoundMethodHandle.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24572 jdk/src/share/classes/java/lang/invoke/BoundMethodHandle.java@5c9e5961d21c
child 26467 d69abed3a07d
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@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: 22297
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: 4537
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: 4537
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: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
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
14595
69a527af467a 8001885: JSR 292 classes should use jdk.internal.org.objectweb.asm
twisti
parents: 14089
diff changeset
    28
import static jdk.internal.org.objectweb.asm.Opcodes.*;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    29
import static java.lang.invoke.LambdaForm.*;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    30
import static java.lang.invoke.LambdaForm.BasicType.*;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    31
import static java.lang.invoke.MethodHandleStatics.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    32
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    33
import java.lang.invoke.LambdaForm.NamedFunction;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    34
import java.lang.invoke.MethodHandles.Lookup;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    35
import java.lang.reflect.Field;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    36
import java.util.Arrays;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    37
import java.util.HashMap;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    38
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    39
import sun.invoke.util.ValueConversions;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    40
import sun.invoke.util.Wrapper;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    41
14595
69a527af467a 8001885: JSR 292 classes should use jdk.internal.org.objectweb.asm
twisti
parents: 14089
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassWriter;
69a527af467a 8001885: JSR 292 classes should use jdk.internal.org.objectweb.asm
twisti
parents: 14089
diff changeset
    43
import jdk.internal.org.objectweb.asm.MethodVisitor;
69a527af467a 8001885: JSR 292 classes should use jdk.internal.org.objectweb.asm
twisti
parents: 14089
diff changeset
    44
import jdk.internal.org.objectweb.asm.Type;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    45
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    46
/**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
 * The flavor of method handle which emulates an invoke instruction
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
 * on a predetermined argument.  The JVM dispatches to the correct method
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    49
 * when the handle is created, not when it is invoked.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    50
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    51
 * All bound arguments are encapsulated in dedicated species.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    52
 */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    53
/* non-public */ abstract class BoundMethodHandle extends MethodHandle {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    54
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    55
    /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    56
        super(type, form);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    57
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    58
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    59
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    60
    // BMH API and internals
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    61
    //
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    62
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    63
    static MethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    64
        // for some type signatures, there exist pre-defined concrete BMH classes
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    65
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    66
            switch (xtype) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    67
            case L_TYPE:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    68
                if (true)  return bindSingle(type, form, x);  // Use known fast path.
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    69
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(L_TYPE).constructor[0].invokeBasic(type, form, x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    70
            case I_TYPE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    71
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    72
            case J_TYPE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    73
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor[0].invokeBasic(type, form, (long) x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    74
            case F_TYPE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    75
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor[0].invokeBasic(type, form, (float) x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    76
            case D_TYPE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    77
                return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor[0].invokeBasic(type, form, (double) x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    78
            default : throw newInternalError("unexpected xtype: " + xtype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    79
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    80
        } catch (Throwable t) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
    81
            throw newInternalError(t);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    82
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    85
    static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    86
            return new Species_L(type, form, x);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    87
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
    88
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    89
    MethodHandle cloneExtend(MethodType type, LambdaForm form, BasicType xtype, Object x) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    90
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    91
            switch (xtype) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    92
            case L_TYPE: return copyWithExtendL(type, form, x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    93
            case I_TYPE: return copyWithExtendI(type, form, ValueConversions.widenSubword(x));
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    94
            case J_TYPE: return copyWithExtendJ(type, form, (long) x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    95
            case F_TYPE: return copyWithExtendF(type, form, (float) x);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
    96
            case D_TYPE: return copyWithExtendD(type, form, (double) x);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    97
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
    98
        } catch (Throwable t) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
    99
            throw newInternalError(t);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   100
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   101
        throw newInternalError("unexpected type: " + xtype);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   103
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   104
    @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   105
    MethodHandle bindArgument(int pos, BasicType basicType, Object value) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   106
        MethodType type = type().dropParameterTypes(pos, pos+1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   107
        LambdaForm form = internalForm().bind(1+pos, speciesData());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   108
        return cloneExtend(type, form, basicType, value);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   110
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   111
    @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   112
    MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   113
        LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos + drops));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   114
        try {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   115
             return copyWith(srcType, form);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   116
         } catch (Throwable t) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   117
             throw newInternalError(t);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   118
         }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   119
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   120
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   121
    @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   122
    MethodHandle permuteArguments(MethodType newType, int[] reorder) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   123
        try {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   124
             return copyWith(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList())));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   125
         } catch (Throwable t) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   126
             throw newInternalError(t);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   127
         }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   128
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   129
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   130
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   131
     * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   132
     * static field containing this value, and they must accordingly implement this method.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   133
     */
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   134
    /*non-public*/ abstract SpeciesData speciesData();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   135
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   136
    /**
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   137
     * Return the number of fields in this BMH.  Equivalent to speciesData().fieldCount().
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   138
     */
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   139
    /*non-public*/ abstract int fieldCount();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   140
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   141
    @Override
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13424
diff changeset
   142
    final Object internalProperties() {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13424
diff changeset
   143
        return "/BMH="+internalValues();
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13424
diff changeset
   144
    }
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13424
diff changeset
   145
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13424
diff changeset
   146
    @Override
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   147
    final Object internalValues() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   148
        Object[] boundValues = new Object[speciesData().fieldCount()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   149
        for (int i = 0; i < boundValues.length; ++i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   150
            boundValues[i] = arg(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   151
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   152
        return Arrays.asList(boundValues);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   153
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   154
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   155
    /*non-public*/ final Object arg(int i) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   156
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   157
            switch (speciesData().fieldType(i)) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   158
            case L_TYPE: return          speciesData().getters[i].invokeBasic(this);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   159
            case I_TYPE: return (int)    speciesData().getters[i].invokeBasic(this);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   160
            case J_TYPE: return (long)   speciesData().getters[i].invokeBasic(this);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   161
            case F_TYPE: return (float)  speciesData().getters[i].invokeBasic(this);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   162
            case D_TYPE: return (double) speciesData().getters[i].invokeBasic(this);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   163
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   164
        } catch (Throwable ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   165
            throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   166
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   167
        throw new InternalError("unexpected type: " + speciesData().typeChars+"."+i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   168
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   169
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   170
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   171
    // cloning API
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   172
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   173
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   174
    /*non-public*/ abstract BoundMethodHandle copyWith(MethodType mt, LambdaForm lf);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   175
    /*non-public*/ abstract BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   176
    /*non-public*/ abstract BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int    narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   177
    /*non-public*/ abstract BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long   narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   178
    /*non-public*/ abstract BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float  narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   179
    /*non-public*/ abstract BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   180
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   181
    // The following is a grossly irregular hack:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   182
    @Override MethodHandle reinvokerTarget() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   183
        try {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   184
            return (MethodHandle) arg(0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   185
        } catch (Throwable ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   186
            throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   187
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   188
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   189
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   190
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   191
    // concrete BMH classes required to close bootstrap loops
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   192
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   193
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   194
    private  // make it private to force users to access the enclosing class first
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   195
    static final class Species_L extends BoundMethodHandle {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   196
        final Object argL0;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   197
        private Species_L(MethodType mt, LambdaForm lf, Object argL0) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   198
            super(mt, lf);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   199
            this.argL0 = argL0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   200
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   201
        // The following is a grossly irregular hack:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   202
        @Override MethodHandle reinvokerTarget() { return (MethodHandle) argL0; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   203
        @Override
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   204
        /*non-public*/ SpeciesData speciesData() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   205
            return SPECIES_DATA;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   206
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   207
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   208
        /*non-public*/ int fieldCount() {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   209
            return 1;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   210
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   211
        /*non-public*/ static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   212
        /*non-public*/ static BoundMethodHandle make(MethodType mt, LambdaForm lf, Object argL0) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   213
            return new Species_L(mt, lf, argL0);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   214
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   215
        @Override
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   216
        /*non-public*/ final BoundMethodHandle copyWith(MethodType mt, LambdaForm lf) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   217
            return new Species_L(mt, lf, argL0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   218
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   219
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   220
        /*non-public*/ final BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   221
            try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   222
                return (BoundMethodHandle) SPECIES_DATA.extendWith(L_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   223
            } catch (Throwable ex) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   224
                throw uncaughtException(ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   225
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   226
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   227
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   228
        /*non-public*/ final BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   229
            try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   230
                return (BoundMethodHandle) SPECIES_DATA.extendWith(I_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   231
            } catch (Throwable ex) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   232
                throw uncaughtException(ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   233
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   234
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   235
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   236
        /*non-public*/ final BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   237
            try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   238
                return (BoundMethodHandle) SPECIES_DATA.extendWith(J_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   239
            } catch (Throwable ex) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   240
                throw uncaughtException(ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   241
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   242
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   243
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   244
        /*non-public*/ final BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   245
            try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   246
                return (BoundMethodHandle) SPECIES_DATA.extendWith(F_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   247
            } catch (Throwable ex) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   248
                throw uncaughtException(ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   249
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   250
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   251
        @Override
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   252
        /*non-public*/ final BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   253
            try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   254
                return (BoundMethodHandle) SPECIES_DATA.extendWith(D_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   255
            } catch (Throwable ex) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   256
                throw uncaughtException(ex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   257
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   258
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   259
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   260
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   261
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   262
    // BMH species meta-data
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   263
    //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   264
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   265
    /**
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   266
     * Meta-data wrapper for concrete BMH types.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   267
     * Each BMH type corresponds to a given sequence of basic field types (LIJFD).
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   268
     * The fields are immutable; their values are fully specified at object construction.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   269
     * Each BMH type supplies an array of getter functions which may be used in lambda forms.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   270
     * A BMH is constructed by cloning a shorter BMH and adding one or more new field values.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   271
     * As a degenerate and common case, the "shorter BMH" can be missing, and contributes zero prior fields.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   272
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   273
    static class SpeciesData {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   274
        final String                             typeChars;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   275
        final BasicType[]                        typeCodes;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   276
        final Class<? extends BoundMethodHandle> clazz;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   277
        // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   278
        // Therefore, we need a non-final link in the chain.  Use array elements.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   279
        final MethodHandle[]                     constructor;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   280
        final MethodHandle[]                     getters;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   281
        final NamedFunction[]                    nominalGetters;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   282
        final SpeciesData[]                      extensions;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   283
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   284
        /*non-public*/ int fieldCount() {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   285
            return typeCodes.length;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   286
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   287
        /*non-public*/ BasicType fieldType(int i) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   288
            return typeCodes[i];
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   289
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   290
        /*non-public*/ char fieldTypeChar(int i) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   291
            return typeChars.charAt(i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   292
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   293
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   294
        public String toString() {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   295
            return "SpeciesData["+(isPlaceholder() ? "<placeholder>" : clazz.getSimpleName())+":"+typeChars+"]";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   296
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   297
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   298
        /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   299
         * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   300
         * represents a MH bound to a generic invoker, which in turn forwards to the corresponding
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   301
         * getter.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   302
         */
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 16906
diff changeset
   303
        NamedFunction getterFunction(int i) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   304
            return nominalGetters[i];
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 16906
diff changeset
   305
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 16906
diff changeset
   306
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   307
        static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   308
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   309
        private SpeciesData(String types, Class<? extends BoundMethodHandle> clazz) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   310
            this.typeChars = types;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   311
            this.typeCodes = basicTypes(types);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   312
            this.clazz = clazz;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   313
            if (!INIT_DONE) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   314
                this.constructor = new MethodHandle[1];  // only one ctor
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   315
                this.getters = new MethodHandle[types.length()];
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   316
                this.nominalGetters = new NamedFunction[types.length()];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   317
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   318
                this.constructor = Factory.makeCtors(clazz, types, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   319
                this.getters = Factory.makeGetters(clazz, types, null);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   320
                this.nominalGetters = Factory.makeNominalGetters(types, null, this.getters);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   321
            }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   322
            this.extensions = new SpeciesData[ARG_TYPE_LIMIT];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   323
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   324
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   325
        private void initForBootstrap() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   326
            assert(!INIT_DONE);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   327
            if (constructor[0] == null) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   328
                String types = typeChars;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   329
                Factory.makeCtors(clazz, types, this.constructor);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   330
                Factory.makeGetters(clazz, types, this.getters);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   331
                Factory.makeNominalGetters(types, this.nominalGetters, this.getters);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   332
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   333
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   334
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   335
        private SpeciesData(String typeChars) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   336
            // Placeholder only.
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   337
            this.typeChars = typeChars;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   338
            this.typeCodes = basicTypes(typeChars);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   339
            this.clazz = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   340
            this.constructor = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   341
            this.getters = null;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   342
            this.nominalGetters = null;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   343
            this.extensions = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   344
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   345
        private boolean isPlaceholder() { return clazz == null; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   346
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   347
        private static final HashMap<String, SpeciesData> CACHE = new HashMap<>();
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 16906
diff changeset
   348
        static { CACHE.put("", EMPTY); }  // make bootstrap predictable
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   349
        private static final boolean INIT_DONE;  // set after <clinit> finishes...
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   350
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   351
        SpeciesData extendWith(byte type) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   352
            return extendWith(BasicType.basicType(type));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   353
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   354
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   355
        SpeciesData extendWith(BasicType type) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   356
            int ord = type.ordinal();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   357
            SpeciesData d = extensions[ord];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   358
            if (d != null)  return d;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   359
            extensions[ord] = d = get(typeChars+type.basicTypeChar());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   360
            return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   361
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   362
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   363
        private static SpeciesData get(String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   364
            // Acquire cache lock for query.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   365
            SpeciesData d = lookupCache(types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   366
            if (!d.isPlaceholder())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   367
                return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   368
            synchronized (d) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   369
                // Use synch. on the placeholder to prevent multiple instantiation of one species.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   370
                // Creating this class forces a recursive call to getForClass.
13424
5de7e57cdc02 7188911: nightly failures after JSR 292 lazy method handle update (round 2)
twisti
parents: 13423
diff changeset
   371
                if (lookupCache(types).isPlaceholder())
5de7e57cdc02 7188911: nightly failures after JSR 292 lazy method handle update (round 2)
twisti
parents: 13423
diff changeset
   372
                    Factory.generateConcreteBMHClass(types);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   373
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   374
            // Reacquire cache lock.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   375
            d = lookupCache(types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   376
            // Class loading must have upgraded the cache.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   377
            assert(d != null && !d.isPlaceholder());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   378
            return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   379
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   380
        static SpeciesData getForClass(String types, Class<? extends BoundMethodHandle> clazz) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   381
            // clazz is a new class which is initializing its SPECIES_DATA field
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   382
            return updateCache(types, new SpeciesData(types, clazz));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   383
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   384
        private static synchronized SpeciesData lookupCache(String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   385
            SpeciesData d = CACHE.get(types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   386
            if (d != null)  return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   387
            d = new SpeciesData(types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   388
            assert(d.isPlaceholder());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   389
            CACHE.put(types, d);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   390
            return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   391
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   392
        private static synchronized SpeciesData updateCache(String types, SpeciesData d) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   393
            SpeciesData d2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   394
            assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   395
            assert(!d.isPlaceholder());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   396
            CACHE.put(types, d);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   397
            return d;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   398
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   399
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   400
        static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   401
            // pre-fill the BMH speciesdata cache with BMH's inner classes
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   402
            final Class<BoundMethodHandle> rootCls = BoundMethodHandle.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   403
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   404
                for (Class<?> c : rootCls.getDeclaredClasses()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   405
                    if (rootCls.isAssignableFrom(c)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   406
                        final Class<? extends BoundMethodHandle> cbmh = c.asSubclass(BoundMethodHandle.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   407
                        SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   408
                        assert(d != null) : cbmh.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   409
                        assert(d.clazz == cbmh);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   410
                        assert(d == lookupCache(d.typeChars));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   411
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   412
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   413
            } catch (Throwable e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   414
                throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   415
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   416
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   417
            for (SpeciesData d : CACHE.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   418
                d.initForBootstrap();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   419
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   420
            // Note:  Do not simplify this, because INIT_DONE must not be
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   421
            // a compile-time constant during bootstrapping.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   422
            INIT_DONE = Boolean.TRUE;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   423
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   424
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   425
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   426
    static SpeciesData getSpeciesData(String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   427
        return SpeciesData.get(types);
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   428
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   429
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   430
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   431
     * Generation of concrete BMH classes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   432
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   433
     * A concrete BMH species is fit for binding a number of values adhering to a
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   434
     * given type pattern. Reference types are erased.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   435
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   436
     * BMH species are cached by type pattern.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   437
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   438
     * A BMH species has a number of fields with the concrete (possibly erased) types of
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   439
     * bound values. Setters are provided as an API in BMH. Getters are exposed as MHs,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   440
     * which can be included as names in lambda forms.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   441
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   442
    static class Factory {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   443
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   444
        static final String JLO_SIG  = "Ljava/lang/Object;";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   445
        static final String JLS_SIG  = "Ljava/lang/String;";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   446
        static final String JLC_SIG  = "Ljava/lang/Class;";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   447
        static final String MH       = "java/lang/invoke/MethodHandle";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   448
        static final String MH_SIG   = "L"+MH+";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   449
        static final String BMH      = "java/lang/invoke/BoundMethodHandle";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   450
        static final String BMH_SIG  = "L"+BMH+";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   451
        static final String SPECIES_DATA     = "java/lang/invoke/BoundMethodHandle$SpeciesData";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   452
        static final String SPECIES_DATA_SIG = "L"+SPECIES_DATA+";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   453
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   454
        static final String SPECIES_PREFIX_NAME = "Species_";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   455
        static final String SPECIES_PREFIX_PATH = BMH + "$" + SPECIES_PREFIX_NAME;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   456
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   457
        static final String BMHSPECIES_DATA_EWI_SIG = "(B)" + SPECIES_DATA_SIG;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   458
        static final String BMHSPECIES_DATA_GFC_SIG = "(" + JLS_SIG + JLC_SIG + ")" + SPECIES_DATA_SIG;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   459
        static final String MYSPECIES_DATA_SIG = "()" + SPECIES_DATA_SIG;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   460
        static final String VOID_SIG   = "()V";
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   461
        static final String INT_SIG    = "()I";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   462
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   463
        static final String SIG_INCIPIT = "(Ljava/lang/invoke/MethodType;Ljava/lang/invoke/LambdaForm;";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   464
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   465
        static final String[] E_THROWABLE = new String[] { "java/lang/Throwable" };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   466
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   467
        /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   468
         * Generate a concrete subclass of BMH for a given combination of bound types.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   469
         *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   470
         * A concrete BMH species adheres to the following schema:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   471
         *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   472
         * <pre>
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   473
         * class Species_[[types]] extends BoundMethodHandle {
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   474
         *     [[fields]]
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   475
         *     final SpeciesData speciesData() { return SpeciesData.get("[[types]]"); }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   476
         * }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   477
         * </pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   478
         *
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   479
         * The {@code [[types]]} signature is precisely the string that is passed to this
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   480
         * method.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   481
         *
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   482
         * The {@code [[fields]]} section consists of one field definition per character in
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   483
         * the type signature, adhering to the naming schema described in the definition of
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20527
diff changeset
   484
         * {@link #makeFieldName}.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   485
         *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   486
         * For example, a concrete BMH species for two reference and one integral bound values
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   487
         * would have the following shape:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   488
         *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   489
         * <pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   490
         * class BoundMethodHandle { ... private static
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   491
         * final class Species_LLI extends BoundMethodHandle {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   492
         *     final Object argL0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   493
         *     final Object argL1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   494
         *     final int argI2;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   495
         *     private Species_LLI(MethodType mt, LambdaForm lf, Object argL0, Object argL1, int argI2) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   496
         *         super(mt, lf);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   497
         *         this.argL0 = argL0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   498
         *         this.argL1 = argL1;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   499
         *         this.argI2 = argI2;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   500
         *     }
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   501
         *     final SpeciesData speciesData() { return SPECIES_DATA; }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   502
         *     final int fieldCount() { return 3; }
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   503
         *     static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LLI", Species_LLI.class);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   504
         *     static BoundMethodHandle make(MethodType mt, LambdaForm lf, Object argL0, Object argL1, int argI2) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   505
         *         return new Species_LLI(mt, lf, argL0, argL1, argI2);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   506
         *     }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   507
         *     final BoundMethodHandle copyWith(MethodType mt, LambdaForm lf) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   508
         *         return new Species_LLI(mt, lf, argL0, argL1, argI2);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   509
         *     }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   510
         *     final BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   511
         *         return SPECIES_DATA.extendWith(L_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   512
         *     }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   513
         *     final BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   514
         *         return SPECIES_DATA.extendWith(I_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   515
         *     }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   516
         *     final BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   517
         *         return SPECIES_DATA.extendWith(J_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   518
         *     }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   519
         *     final BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   520
         *         return SPECIES_DATA.extendWith(F_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   521
         *     }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   522
         *     public final BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   523
         *         return SPECIES_DATA.extendWith(D_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   524
         *     }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   525
         * }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   526
         * </pre>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   527
         *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   528
         * @param types the type signature, wherein reference types are erased to 'L'
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   529
         * @return the generated concrete BMH class
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   530
         */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   531
        static Class<? extends BoundMethodHandle> generateConcreteBMHClass(String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   532
            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   533
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   534
            String shortTypes = LambdaForm.shortenSignature(types);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   535
            final String className  = SPECIES_PREFIX_PATH + shortTypes;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   536
            final String sourceFile = SPECIES_PREFIX_NAME + shortTypes;
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   537
            final int NOT_ACC_PUBLIC = 0;  // not ACC_PUBLIC
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   538
            cw.visit(V1_6, NOT_ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, BMH, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   539
            cw.visitSource(sourceFile, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   540
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   541
            // emit static types and SPECIES_DATA fields
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   542
            cw.visitField(NOT_ACC_PUBLIC + ACC_STATIC, "SPECIES_DATA", SPECIES_DATA_SIG, null, null).visitEnd();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   543
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   544
            // emit bound argument fields
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   545
            for (int i = 0; i < types.length(); ++i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   546
                final char t = types.charAt(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   547
                final String fieldName = makeFieldName(types, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   548
                final String fieldDesc = t == 'L' ? JLO_SIG : String.valueOf(t);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   549
                cw.visitField(ACC_FINAL, fieldName, fieldDesc, null, null).visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   550
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   551
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   552
            MethodVisitor mv;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   553
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   554
            // emit constructor
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   555
            mv = cw.visitMethod(ACC_PRIVATE, "<init>", makeSignature(types, true), null, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   556
            mv.visitCode();
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   557
            mv.visitVarInsn(ALOAD, 0); // this
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   558
            mv.visitVarInsn(ALOAD, 1); // type
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   559
            mv.visitVarInsn(ALOAD, 2); // form
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   560
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 20535
diff changeset
   561
            mv.visitMethodInsn(INVOKESPECIAL, BMH, "<init>", makeSignature("", true), false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   562
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   563
            for (int i = 0, j = 0; i < types.length(); ++i, ++j) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   564
                // i counts the arguments, j counts corresponding argument slots
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   565
                char t = types.charAt(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   566
                mv.visitVarInsn(ALOAD, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   567
                mv.visitVarInsn(typeLoadOp(t), j + 3); // parameters start at 3
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   568
                mv.visitFieldInsn(PUTFIELD, className, makeFieldName(types, i), typeSig(t));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   569
                if (t == 'J' || t == 'D') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   570
                    ++j; // adjust argument register access
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   571
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   572
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   573
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   574
            mv.visitInsn(RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   575
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   576
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   577
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   578
            // emit implementation of reinvokerTarget()
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   579
            mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_FINAL, "reinvokerTarget", "()" + MH_SIG, null, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   580
            mv.visitCode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   581
            mv.visitVarInsn(ALOAD, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   582
            mv.visitFieldInsn(GETFIELD, className, "argL0", JLO_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   583
            mv.visitTypeInsn(CHECKCAST, MH);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   584
            mv.visitInsn(ARETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   585
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   586
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   587
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   588
            // emit implementation of speciesData()
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   589
            mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_FINAL, "speciesData", MYSPECIES_DATA_SIG, null, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   590
            mv.visitCode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   591
            mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   592
            mv.visitInsn(ARETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   593
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   594
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   595
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   596
            // emit implementation of fieldCount()
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   597
            mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_FINAL, "fieldCount", INT_SIG, null, null);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   598
            mv.visitCode();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   599
            int fc = types.length();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   600
            if (fc <= (ICONST_5 - ICONST_0)) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   601
                mv.visitInsn(ICONST_0 + fc);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   602
            } else {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   603
                mv.visitIntInsn(SIPUSH, fc);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   604
            }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   605
            mv.visitInsn(IRETURN);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   606
            mv.visitMaxs(0, 0);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   607
            mv.visitEnd();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   608
            // emit make()  ...factory method wrapping constructor
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   609
            mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_STATIC, "make", makeSignature(types, false), null, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   610
            mv.visitCode();
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   611
            // make instance
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   612
            mv.visitTypeInsn(NEW, className);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   613
            mv.visitInsn(DUP);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   614
            // load mt, lf
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   615
            mv.visitVarInsn(ALOAD, 0);  // type
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   616
            mv.visitVarInsn(ALOAD, 1);  // form
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   617
            // load factory method arguments
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   618
            for (int i = 0, j = 0; i < types.length(); ++i, ++j) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   619
                // i counts the arguments, j counts corresponding argument slots
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   620
                char t = types.charAt(i);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   621
                mv.visitVarInsn(typeLoadOp(t), j + 2); // parameters start at 3
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   622
                if (t == 'J' || t == 'D') {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   623
                    ++j; // adjust argument register access
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   624
                }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   625
            }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   626
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   627
            // finally, invoke the constructor and return
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   628
            mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", makeSignature(types, true), false);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   629
            mv.visitInsn(ARETURN);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   630
            mv.visitMaxs(0, 0);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   631
            mv.visitEnd();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   632
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   633
            // emit copyWith()
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   634
            mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_FINAL, "copyWith", makeSignature("", false), null, null);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   635
            mv.visitCode();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   636
            // make instance
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   637
            mv.visitTypeInsn(NEW, className);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   638
            mv.visitInsn(DUP);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   639
            // load mt, lf
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   640
            mv.visitVarInsn(ALOAD, 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   641
            mv.visitVarInsn(ALOAD, 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   642
            // put fields on the stack
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   643
            emitPushFields(types, className, mv);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   644
            // finally, invoke the constructor and return
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   645
            mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", makeSignature(types, true), false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   646
            mv.visitInsn(ARETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   647
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   648
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   649
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   650
            // for each type, emit copyWithExtendT()
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   651
            for (BasicType type : BasicType.ARG_TYPES) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   652
                int ord = type.ordinal();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   653
                char btChar = type.basicTypeChar();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   654
                mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_FINAL, "copyWithExtend" + btChar, makeSignature(String.valueOf(btChar), false), null, E_THROWABLE);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   655
                mv.visitCode();
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   656
                // return SPECIES_DATA.extendWith(t).constructor[0].invokeBasic(mt, lf, argL0, ..., narg)
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   657
                // obtain constructor
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   658
                mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   659
                int iconstInsn = ICONST_0 + ord;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   660
                assert(iconstInsn <= ICONST_5);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   661
                mv.visitInsn(iconstInsn);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   662
                mv.visitMethodInsn(INVOKEVIRTUAL, SPECIES_DATA, "extendWith", BMHSPECIES_DATA_EWI_SIG, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   663
                mv.visitFieldInsn(GETFIELD, SPECIES_DATA, "constructor", "[" + MH_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   664
                mv.visitInsn(ICONST_0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   665
                mv.visitInsn(AALOAD);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   666
                // load mt, lf
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   667
                mv.visitVarInsn(ALOAD, 1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   668
                mv.visitVarInsn(ALOAD, 2);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   669
                // put fields on the stack
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   670
                emitPushFields(types, className, mv);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   671
                // put narg on stack
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   672
                mv.visitVarInsn(typeLoadOp(btChar), 3);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   673
                // finally, invoke the constructor and return
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   674
                mv.visitMethodInsn(INVOKEVIRTUAL, MH, "invokeBasic", makeSignature(types + btChar, false), false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   675
                mv.visitInsn(ARETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   676
                mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   677
                mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   678
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   679
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   680
            // emit class initializer
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23038
diff changeset
   681
            mv = cw.visitMethod(NOT_ACC_PUBLIC | ACC_STATIC, "<clinit>", VOID_SIG, null, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   682
            mv.visitCode();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   683
            mv.visitLdcInsn(types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   684
            mv.visitLdcInsn(Type.getObjectType(className));
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 20535
diff changeset
   685
            mv.visitMethodInsn(INVOKESTATIC, SPECIES_DATA, "getForClass", BMHSPECIES_DATA_GFC_SIG, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   686
            mv.visitFieldInsn(PUTSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   687
            mv.visitInsn(RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   688
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   689
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   690
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   691
            cw.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   692
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   693
            // load class
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   694
            final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   695
            InvokerBytecodeGenerator.maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   696
            Class<? extends BoundMethodHandle> bmhClass =
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   697
                //UNSAFE.defineAnonymousClass(BoundMethodHandle.class, classFile, null).asSubclass(BoundMethodHandle.class);
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14595
diff changeset
   698
                UNSAFE.defineClass(className, classFile, 0, classFile.length,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14595
diff changeset
   699
                                   BoundMethodHandle.class.getClassLoader(), null)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14595
diff changeset
   700
                    .asSubclass(BoundMethodHandle.class);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   701
            UNSAFE.ensureClassInitialized(bmhClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   702
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   703
            return bmhClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   704
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   705
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   706
        private static int typeLoadOp(char t) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   707
            switch (t) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   708
            case 'L': return ALOAD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   709
            case 'I': return ILOAD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   710
            case 'J': return LLOAD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   711
            case 'F': return FLOAD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   712
            case 'D': return DLOAD;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   713
            default : throw newInternalError("unrecognized type " + t);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   714
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   715
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   716
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   717
        private static void emitPushFields(String types, String className, MethodVisitor mv) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   718
            for (int i = 0; i < types.length(); ++i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   719
                char tc = types.charAt(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   720
                mv.visitVarInsn(ALOAD, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   721
                mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   722
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   723
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   724
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   725
        static String typeSig(char t) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   726
            return t == 'L' ? JLO_SIG : String.valueOf(t);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   727
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   728
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   729
        //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   730
        // Getter MH generation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   731
        //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   732
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   733
        private static MethodHandle makeGetter(Class<?> cbmhClass, String types, int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   734
            String fieldName = makeFieldName(types, index);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   735
            Class<?> fieldType = Wrapper.forBasicType(types.charAt(index)).primitiveType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   736
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   737
                return LOOKUP.findGetter(cbmhClass, fieldName, fieldType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   738
            } catch (NoSuchFieldException | IllegalAccessException e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   739
                throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   740
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   741
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   742
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   743
        static MethodHandle[] makeGetters(Class<?> cbmhClass, String types, MethodHandle[] mhs) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   744
            if (mhs == null)  mhs = new MethodHandle[types.length()];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   745
            for (int i = 0; i < mhs.length; ++i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   746
                mhs[i] = makeGetter(cbmhClass, types, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   747
                assert(mhs[i].internalMemberName().getDeclaringClass() == cbmhClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   748
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   749
            return mhs;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   750
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   751
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   752
        static MethodHandle[] makeCtors(Class<? extends BoundMethodHandle> cbmh, String types, MethodHandle mhs[]) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   753
            if (mhs == null)  mhs = new MethodHandle[1];
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   754
            if (types.equals(""))  return mhs;  // hack for empty BMH species
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   755
            mhs[0] = makeCbmhCtor(cbmh, types);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   756
            return mhs;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   757
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   758
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   759
        static NamedFunction[] makeNominalGetters(String types, NamedFunction[] nfs, MethodHandle[] getters) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   760
            if (nfs == null)  nfs = new NamedFunction[types.length()];
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   761
            for (int i = 0; i < nfs.length; ++i) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   762
                nfs[i] = new NamedFunction(getters[i]);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   763
            }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   764
            return nfs;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   765
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   766
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   767
        //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   768
        // Auxiliary methods.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   769
        //
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   770
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   771
        static SpeciesData speciesDataFromConcreteBMHClass(Class<? extends BoundMethodHandle> cbmh) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   772
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   773
                Field F_SPECIES_DATA = cbmh.getDeclaredField("SPECIES_DATA");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   774
                return (SpeciesData) F_SPECIES_DATA.get(null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   775
            } catch (ReflectiveOperationException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   776
                throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   777
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   778
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   779
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   780
        /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   781
         * Field names in concrete BMHs adhere to this pattern:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   782
         * arg + type + index
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   783
         * where type is a single character (L, I, J, F, D).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   784
         */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   785
        private static String makeFieldName(String types, int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   786
            assert index >= 0 && index < types.length();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   787
            return "arg" + types.charAt(index) + index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   788
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   789
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   790
        private static String makeSignature(String types, boolean ctor) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   791
            StringBuilder buf = new StringBuilder(SIG_INCIPIT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   792
            for (char c : types.toCharArray()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   793
                buf.append(typeSig(c));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   794
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   795
            return buf.append(')').append(ctor ? "V" : BMH_SIG).toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   796
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   797
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   798
        static MethodHandle makeCbmhCtor(Class<? extends BoundMethodHandle> cbmh, String types) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   799
            try {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   800
                return LOOKUP.findStatic(cbmh, "make", MethodType.fromMethodDescriptorString(makeSignature(types, false), null));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   801
            } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | TypeNotPresentException e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   802
                throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   803
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   804
        }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   805
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7554
diff changeset
   806
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   807
    private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   808
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   809
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   810
     * All subclasses must provide such a value describing their type signature.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   811
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 9752
diff changeset
   812
    static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY;
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   813
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   814
    private static final SpeciesData[] SPECIES_DATA_CACHE = new SpeciesData[5];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   815
    private static SpeciesData checkCache(int size, String types) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   816
        int idx = size - 1;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   817
        SpeciesData data = SPECIES_DATA_CACHE[idx];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   818
        if (data != null)  return data;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   819
        SPECIES_DATA_CACHE[idx] = data = getSpeciesData(types);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   820
        return data;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   821
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   822
    static SpeciesData speciesData_L()     { return checkCache(1, "L"); }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   823
    static SpeciesData speciesData_LL()    { return checkCache(2, "LL"); }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   824
    static SpeciesData speciesData_LLL()   { return checkCache(3, "LLL"); }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   825
    static SpeciesData speciesData_LLLL()  { return checkCache(4, "LLLL"); }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 23010
diff changeset
   826
    static SpeciesData speciesData_LLLLL() { return checkCache(5, "LLLLL"); }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   827
}