jdk/src/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
author twisti
Thu, 09 Aug 2012 15:03:28 -0700
changeset 13425 a8d96a0eda69
parent 13423 17843fff200d
child 13610 28122b96858e
permissions -rw-r--r--
7190416: JSR 292: typo in InvokerBytecodeGenerator.getConstantPoolSize Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     1
/*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     4
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    10
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    15
 * accompanied this code).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    16
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    20
 *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    23
 * questions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    24
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    25
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    26
package java.lang.invoke;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    27
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    28
import sun.invoke.util.VerifyAccess;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    29
import java.lang.invoke.LambdaForm.Name;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    30
import java.lang.invoke.MethodHandles.Lookup;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    31
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    32
import sun.invoke.util.Wrapper;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    33
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    34
import java.io.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    35
import java.util.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    36
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    37
import com.sun.xml.internal.ws.org.objectweb.asm.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    38
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    39
import java.lang.reflect.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    40
import static java.lang.invoke.MethodHandleStatics.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    41
import static java.lang.invoke.MethodHandleNatives.Constants.*;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    42
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    43
import sun.invoke.util.ValueConversions;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    44
import sun.invoke.util.VerifyType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    45
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    46
/**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    47
 * Code generation backend for LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    48
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    49
 * @author John Rose, JSR 292 EG
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    50
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    51
class InvokerBytecodeGenerator {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    52
    /** Define class names for convenience. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    53
    private static final String MH      = "java/lang/invoke/MethodHandle";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    54
    private static final String BMH     = "java/lang/invoke/BoundMethodHandle";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    55
    private static final String LF      = "java/lang/invoke/LambdaForm";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    56
    private static final String LFN     = "java/lang/invoke/LambdaForm$Name";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    57
    private static final String CLS     = "java/lang/Class";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    58
    private static final String OBJ     = "java/lang/Object";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    59
    private static final String OBJARY  = "[Ljava/lang/Object;";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    60
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    61
    private static final String LF_SIG  = "L" + LF + ";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    62
    private static final String LFN_SIG = "L" + LFN + ";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    63
    private static final String LL_SIG  = "(L" + OBJ + ";)L" + OBJ + ";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    64
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    65
    /** Name of its super class*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    66
    private static final String superName = LF;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    67
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    68
    /** Name of new class */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    69
    private final String className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    70
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    71
    /** Name of the source file (for stack trace printing). */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    72
    private final String sourceFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    73
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    74
    private final LambdaForm lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    75
    private final String     invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    76
    private final MethodType invokerType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    77
    private final int[] localsMap;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    78
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    79
    /** ASM bytecode generation. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    80
    private ClassWriter cw;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    81
    private MethodVisitor mv;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    82
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    83
    private static final MemberName.Factory MEMBERNAME_FACTORY = MemberName.getFactory();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    84
    private static final Class<?> HOST_CLASS = LambdaForm.class;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    85
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    86
    private InvokerBytecodeGenerator(LambdaForm lambdaForm, int localsMapSize,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    87
                                     String className, String invokerName, MethodType invokerType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    88
        if (invokerName.contains(".")) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    89
            int p = invokerName.indexOf(".");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    90
            className = invokerName.substring(0, p);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    91
            invokerName = invokerName.substring(p+1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    92
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    93
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    94
            className = makeDumpableClassName(className);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    95
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    96
        this.className  = superName + "$" + className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    97
        this.sourceFile = "LambdaForm$" + className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    98
        this.lambdaForm = lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    99
        this.invokerName = invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   100
        this.invokerType = invokerType;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   101
        this.localsMap = new int[localsMapSize];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   102
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   103
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   104
    private InvokerBytecodeGenerator(String className, String invokerName, MethodType invokerType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   105
        this(null, invokerType.parameterCount(),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   106
             className, invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   107
        // Create an array to map name indexes to locals indexes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   108
        for (int i = 0; i < localsMap.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   109
            localsMap[i] = invokerType.parameterSlotCount() - invokerType.parameterSlotDepth(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   110
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   111
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   112
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   113
    private InvokerBytecodeGenerator(String className, LambdaForm form, MethodType invokerType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   114
        this(form, form.names.length,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   115
             className, form.debugName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   116
        // Create an array to map name indexes to locals indexes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   117
        Name[] names = form.names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   118
        for (int i = 0, index = 0; i < localsMap.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   119
            localsMap[i] = index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   120
            index += Wrapper.forBasicType(names[i].type).stackSlots();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   121
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   122
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   123
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   124
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   125
    /** instance counters for dumped classes */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   126
    private final static HashMap<String,Integer> DUMP_CLASS_FILES_COUNTERS;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   127
    /** debugging flag for saving generated class files */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   128
    private final static File DUMP_CLASS_FILES_DIR;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   129
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   130
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   131
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   132
            DUMP_CLASS_FILES_COUNTERS = new HashMap<>();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   133
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   134
                File dumpDir = new File("DUMP_CLASS_FILES");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   135
                if (!dumpDir.exists()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   136
                    dumpDir.mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   137
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   138
                DUMP_CLASS_FILES_DIR = dumpDir;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   139
                System.out.println("Dumping class files to "+DUMP_CLASS_FILES_DIR+"/...");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   140
            } catch (Exception e) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   141
                throw new InternalError(e);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   142
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   143
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   144
            DUMP_CLASS_FILES_COUNTERS = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   145
            DUMP_CLASS_FILES_DIR = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   146
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   147
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   148
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   149
    static void maybeDump(final String className, final byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   150
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   151
            System.out.println("dump: " + className);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   152
            java.security.AccessController.doPrivileged(
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   153
            new java.security.PrivilegedAction<Void>() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   154
                public Void run() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   155
                    try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   156
                        String dumpName = className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   157
                        //dumpName = dumpName.replace('/', '-');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   158
                        File dumpFile = new File(DUMP_CLASS_FILES_DIR, dumpName+".class");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   159
                        dumpFile.getParentFile().mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   160
                        FileOutputStream file = new FileOutputStream(dumpFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   161
                        file.write(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   162
                        file.close();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   163
                        return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   164
                    } catch (IOException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   165
                        throw new InternalError(ex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   166
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   167
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   168
            });
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   169
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   170
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   171
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   172
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   173
    private static String makeDumpableClassName(String className) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   174
        Integer ctr;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   175
        synchronized (DUMP_CLASS_FILES_COUNTERS) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   176
            ctr = DUMP_CLASS_FILES_COUNTERS.get(className);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   177
            if (ctr == null)  ctr = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   178
            DUMP_CLASS_FILES_COUNTERS.put(className, ctr+1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   179
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   180
        String sfx = ctr.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   181
        while (sfx.length() < 3)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   182
            sfx = "0"+sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   183
        className += sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   184
        return className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   185
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   186
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   187
    class CpPatch {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   188
        int index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   189
        Object value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   190
        CpPatch(int index, Object value) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   191
            this.index = index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   192
            this.value = value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   193
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   194
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   195
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   196
    Map<Object, CpPatch> cpPatches = new HashMap<>();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   197
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   198
    int cph = 0;  // for counting constant placeholders
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   199
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   200
    String constantPlaceholder(Object arg) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   201
        String cpPlaceholder = "CONSTANT_PLACEHOLDER_" + cph++;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   202
        if (DUMP_CLASS_FILES) cpPlaceholder += " <<" + arg.toString() + ">>";  // debugging aid
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   203
        if (cpPatches.containsKey(cpPlaceholder)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   204
            throw new InternalError("observed CP placeholder twice: " + cpPlaceholder);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   205
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   206
        // insert placeholder in CP and remember the patch
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   207
        int index = cw.newConst((Object) cpPlaceholder);  // TODO check if aready in the constant pool
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   208
        cpPatches.put(cpPlaceholder, new CpPatch(index, arg));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   209
        return cpPlaceholder;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   210
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   211
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   212
    Object[] cpPatches(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   213
        int size = getConstantPoolSize(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   214
        Object[] res = new Object[size];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   215
        for (CpPatch p : cpPatches.values()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   216
                res[p.index] = p.value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   217
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   218
        return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   219
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   220
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   221
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   222
     * Extract the number of constant pool entries from a given class file.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   223
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   224
     * @param classFile the bytes of the class file in question.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   225
     * @return the number of entries in the constant pool.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   226
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   227
    private static int getConstantPoolSize(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   228
        // The first few bytes:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   229
        // u4 magic;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   230
        // u2 minor_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   231
        // u2 major_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   232
        // u2 constant_pool_count;
13425
a8d96a0eda69 7190416: JSR 292: typo in InvokerBytecodeGenerator.getConstantPoolSize
twisti
parents: 13423
diff changeset
   233
        return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   234
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   235
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   236
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   237
     * Extract the MemberName of a newly-defined method.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   238
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   239
     * @param classFile
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   240
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   241
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   242
    private MemberName loadMethod(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   243
        Class<?> invokerClass = loadAndInitializeInvokerClass(classFile, cpPatches(classFile));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   244
        return resolveInvokerMember(invokerClass, invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   245
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   246
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   247
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   248
     * Define a given class as anonymous class in the runtime system.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   249
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   250
     * @param classBytes
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   251
     * @param patches
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   252
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   253
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   254
    private static Class<?> loadAndInitializeInvokerClass(byte[] classBytes, Object[] patches) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   255
        Class<?> invokerClass = UNSAFE.defineAnonymousClass(HOST_CLASS, classBytes, patches);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   256
        UNSAFE.ensureClassInitialized(invokerClass);  // Make sure the class is initialized; VM might complain.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   257
        return invokerClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   258
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   259
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   260
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   261
     * TODO
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   262
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   263
     * @param invokerClass
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   264
     * @param name
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   265
     * @param type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   266
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   267
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   268
    private static MemberName resolveInvokerMember(Class<?> invokerClass, String name, MethodType type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   269
        MemberName member = new MemberName(invokerClass, name, type, REF_invokeStatic);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   270
        //System.out.println("resolveInvokerMember => "+member);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   271
        //for (Method m : invokerClass.getDeclaredMethods())  System.out.println("  "+m);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   272
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   273
            member = MEMBERNAME_FACTORY.resolveOrFail(REF_invokeStatic, member, HOST_CLASS, ReflectiveOperationException.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   274
        } catch (ReflectiveOperationException e) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   275
            throw new InternalError(e);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   276
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   277
        //System.out.println("resolveInvokerMember => "+member);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   278
        return member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   279
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   280
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   281
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   282
     * Set up class file generation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   283
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   284
    private void classFilePrologue() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   285
        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:
diff changeset
   286
        cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   287
        cw.visitSource(sourceFile, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   288
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   289
        String invokerDesc = invokerType.toMethodDescriptorString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   290
        mv = cw.visitMethod(Opcodes.ACC_STATIC, invokerName, invokerDesc, null, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   291
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   292
        // Force inlining of this invoker method.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   293
        mv.visitAnnotation("Ljava/lang/invoke/ForceInline;", true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   294
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   295
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   296
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   297
     * Tear down class file generation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   298
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   299
    private void classFileEpilogue() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   300
        mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   301
        mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   302
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   303
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   304
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   305
     * Low-level emit helpers.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   306
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   307
    private void emitConst(Object con) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   308
        if (con == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   309
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   310
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   311
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   312
        if (con instanceof Integer) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   313
            emitIconstInsn((int) con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   314
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   315
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   316
        if (con instanceof Long) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   317
            long x = (long) con;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   318
            if (x == (short) x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   319
                emitIconstInsn((int) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   320
                mv.visitInsn(Opcodes.I2L);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   321
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   322
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   323
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   324
        if (con instanceof Float) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   325
            float x = (float) con;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   326
            if (x == (short) x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   327
                emitIconstInsn((int) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   328
                mv.visitInsn(Opcodes.I2F);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   329
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   330
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   331
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   332
        if (con instanceof Double) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   333
            double x = (double) con;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   334
            if (x == (short) x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   335
                emitIconstInsn((int) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   336
                mv.visitInsn(Opcodes.I2D);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   337
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   338
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   339
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   340
        if (con instanceof Boolean) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   341
            emitIconstInsn((boolean) con ? 1 : 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   342
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   343
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   344
        // fall through:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   345
        mv.visitLdcInsn(con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   346
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   347
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   348
    private void emitIconstInsn(int i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   349
        int opcode;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   350
        switch (i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   351
        case 0:  opcode = Opcodes.ICONST_0;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   352
        case 1:  opcode = Opcodes.ICONST_1;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   353
        case 2:  opcode = Opcodes.ICONST_2;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   354
        case 3:  opcode = Opcodes.ICONST_3;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   355
        case 4:  opcode = Opcodes.ICONST_4;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   356
        case 5:  opcode = Opcodes.ICONST_5;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   357
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   358
            if (i == (byte) i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   359
                mv.visitIntInsn(Opcodes.BIPUSH, i & 0xFF);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   360
            } else if (i == (short) i) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   361
                mv.visitIntInsn(Opcodes.SIPUSH, (char) i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   362
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   363
                mv.visitLdcInsn(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   364
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   365
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   366
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   367
        mv.visitInsn(opcode);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   368
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   369
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   370
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   371
     * NOTE: These load/store methods use the localsMap to find the correct index!
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   372
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   373
    private void emitLoadInsn(char type, int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   374
        int opcode;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   375
        switch (type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   376
        case 'I':  opcode = Opcodes.ILOAD;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   377
        case 'J':  opcode = Opcodes.LLOAD;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   378
        case 'F':  opcode = Opcodes.FLOAD;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   379
        case 'D':  opcode = Opcodes.DLOAD;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   380
        case 'L':  opcode = Opcodes.ALOAD;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   381
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   382
            throw new InternalError("unknown type: " + type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   383
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   384
        mv.visitVarInsn(opcode, localsMap[index]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   385
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   386
    private void emitAloadInsn(int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   387
        emitLoadInsn('L', index);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   388
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   389
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   390
    private void emitStoreInsn(char type, int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   391
        int opcode;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   392
        switch (type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   393
        case 'I':  opcode = Opcodes.ISTORE;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   394
        case 'J':  opcode = Opcodes.LSTORE;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   395
        case 'F':  opcode = Opcodes.FSTORE;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   396
        case 'D':  opcode = Opcodes.DSTORE;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   397
        case 'L':  opcode = Opcodes.ASTORE;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   398
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   399
            throw new InternalError("unknown type: " + type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   400
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   401
        mv.visitVarInsn(opcode, localsMap[index]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   402
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   403
    private void emitAstoreInsn(int index) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   404
        emitStoreInsn('L', index);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   405
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   406
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   407
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   408
     * Emit a boxing call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   409
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   410
     * @param type primitive type class to box.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   411
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   412
    private void emitBoxing(Class<?> type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   413
        Wrapper wrapper = Wrapper.forPrimitiveType(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   414
        String owner = "java/lang/" + wrapper.wrapperType().getSimpleName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   415
        String name  = "valueOf";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   416
        String desc  = "(" + wrapper.basicTypeChar() + ")L" + owner + ";";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   417
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, desc);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   418
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   419
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   420
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   421
     * Emit an unboxing call (plus preceding checkcast).
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   422
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   423
     * @param type wrapper type class to unbox.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   424
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   425
    private void emitUnboxing(Class<?> type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   426
        Wrapper wrapper = Wrapper.forWrapperType(type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   427
        String owner = "java/lang/" + wrapper.wrapperType().getSimpleName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   428
        String name  = wrapper.primitiveSimpleName() + "Value";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   429
        String desc  = "()" + wrapper.basicTypeChar();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   430
        mv.visitTypeInsn(Opcodes.CHECKCAST, owner);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   431
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   432
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   433
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   434
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   435
     * Emit an implicit conversion.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   436
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   437
     * @param ptype type of value present on stack
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   438
     * @param pclass type of value required on stack
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   439
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   440
    private void emitImplicitConversion(char ptype, Class<?> pclass) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   441
        switch (ptype) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   442
        case 'L':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   443
            if (VerifyType.isNullConversion(Object.class, pclass))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   444
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   445
            if (isStaticallyNameable(pclass)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   446
                mv.visitTypeInsn(Opcodes.CHECKCAST, getInternalName(pclass));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   447
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   448
                mv.visitLdcInsn(constantPlaceholder(pclass));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   449
                mv.visitTypeInsn(Opcodes.CHECKCAST, CLS);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   450
                mv.visitInsn(Opcodes.SWAP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   451
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CLS, "cast", LL_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   452
                if (pclass.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   453
                    mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   454
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   455
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   456
        case 'I':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   457
            if (!VerifyType.isNullConversion(int.class, pclass))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   458
                emitPrimCast(ptype, Wrapper.basicTypeChar(pclass));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   459
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   460
        case 'J':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   461
            assert(pclass == long.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   462
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   463
        case 'F':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   464
            assert(pclass == float.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   465
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   466
        case 'D':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   467
            assert(pclass == double.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   468
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   469
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   470
        throw new InternalError("bad implicit conversion: tc="+ptype+": "+pclass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   471
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   472
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   473
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   474
     * Emits an actual return instruction conforming to the given return type.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   475
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   476
    private void emitReturnInsn(Class<?> type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   477
        int opcode;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   478
        switch (Wrapper.basicTypeChar(type)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   479
        case 'I':  opcode = Opcodes.IRETURN;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   480
        case 'J':  opcode = Opcodes.LRETURN;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   481
        case 'F':  opcode = Opcodes.FRETURN;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   482
        case 'D':  opcode = Opcodes.DRETURN;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   483
        case 'L':  opcode = Opcodes.ARETURN;  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   484
        case 'V':  opcode = Opcodes.RETURN;   break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   485
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   486
            throw new InternalError("unknown return type: " + type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   487
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   488
        mv.visitInsn(opcode);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   489
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   490
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   491
    private static String getInternalName(Class<?> c) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   492
        assert(VerifyAccess.isTypeVisible(c, Object.class));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   493
        return c.getName().replace('.', '/');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   494
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   495
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   496
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   497
     * Generate customized bytecode for a given LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   498
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   499
     * @param form
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   500
     * @param invokerType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   501
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   502
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   503
    static MemberName generateCustomizedCode(LambdaForm form, MethodType invokerType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   504
        InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("MH", form, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   505
        return g.loadMethod(g.generateCustomizedCodeBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   506
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   507
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   508
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   509
     * Generate an invoker method for the passed {@link LambdaForm}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   510
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   511
    private byte[] generateCustomizedCodeBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   512
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   513
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   514
        // Suppress this method in backtraces displayed to the user.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   515
        mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   516
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   517
        // Mark this method as a compiled LambdaForm
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   518
        mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Compiled;", true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   519
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   520
        // iterate over the form's names, generating bytecode instructions for each
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   521
        // start iterating at the first name following the arguments
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   522
        for (int i = lambdaForm.arity; i < lambdaForm.names.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   523
            Name name = lambdaForm.names[i];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   524
            MemberName member = name.function.member();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   525
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   526
            if (isSelectAlternative(member)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   527
                // selectAlternative idiom
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   528
                // FIXME: make sure this idiom is really present!
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   529
                emitSelectAlternative(name, lambdaForm.names[i + 1]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   530
                i++;  // skip MH.invokeBasic of the selectAlternative result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   531
            } else if (isStaticallyInvocable(member)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   532
                emitStaticInvoke(member, name);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   533
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   534
                emitInvoke(name);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   535
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   536
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   537
            // store the result from evaluating to the target name in a local if required
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   538
            // (if this is the last value, i.e., the one that is going to be returned,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   539
            // avoid store/load/return and just return)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   540
            if (i == lambdaForm.names.length - 1 && i == lambdaForm.result) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   541
                // return value - do nothing
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   542
            } else if (name.type != 'V') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   543
                // non-void: actually assign
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   544
                emitStoreInsn(name.type, name.index());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   545
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   546
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   547
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   548
        // return statement
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   549
        emitReturn();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   550
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   551
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   552
        bogusMethod(lambdaForm);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   553
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   554
        final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   555
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   556
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   557
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   558
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   559
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   560
     * Emit an invoke for the given name.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   561
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   562
     * @param name
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   563
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   564
    void emitInvoke(Name name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   565
        if (true) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   566
            // push receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   567
            MethodHandle target = name.function.resolvedHandle;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   568
            assert(target != null) : name.exprString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   569
            mv.visitLdcInsn(constantPlaceholder(target));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   570
            mv.visitTypeInsn(Opcodes.CHECKCAST, MH);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   571
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   572
            // load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   573
            emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   574
            mv.visitTypeInsn(Opcodes.CHECKCAST, MH);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   575
            mv.visitFieldInsn(Opcodes.GETFIELD, MH, "form", LF_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   576
            mv.visitFieldInsn(Opcodes.GETFIELD, LF, "names", LFN_SIG);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   577
            // TODO more to come
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   578
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   579
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   580
        // push arguments
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   581
        for (int i = 0; i < name.arguments.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   582
            emitPushArgument(name, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   583
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   584
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   585
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   586
        MethodType type = name.function.methodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   587
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   588
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   589
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   590
    static private Class<?>[] STATICALLY_INVOCABLE_PACKAGES = {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   591
        // Sample classes from each package we are willing to bind to statically:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   592
        java.lang.Object.class,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   593
        java.util.Arrays.class,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   594
        sun.misc.Unsafe.class
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   595
        //MethodHandle.class already covered
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   596
    };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   597
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   598
    static boolean isStaticallyInvocable(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   599
        if (member == null)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   600
        if (member.isConstructor())  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   601
        Class<?> cls = member.getDeclaringClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   602
        if (cls.isArray() || cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   603
            return false;  // FIXME
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   604
        if (cls.isAnonymousClass() || cls.isLocalClass())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   605
            return false;  // inner class of some sort
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   606
        if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   607
            return false;  // not on BCP
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   608
        if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   609
            return true;   // in java.lang.invoke package
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   610
        if (member.isPublic() && isStaticallyNameable(cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   611
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   612
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   613
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   614
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   615
    static boolean isStaticallyNameable(Class<?> cls) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   616
        while (cls.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   617
            cls = cls.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   618
        if (cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   619
            return true;  // int[].class, for example
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   620
        if (cls.getClassLoader() != Object.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   621
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   622
        if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   623
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   624
        if (!Modifier.isPublic(cls.getModifiers()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   625
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   626
        for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   627
            if (VerifyAccess.isSamePackage(pkgcls, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   628
                return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   629
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   630
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   631
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   632
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   633
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   634
     * Emit an invoke for the given name, using the MemberName directly.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   635
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   636
     * @param name
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   637
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   638
    void emitStaticInvoke(MemberName member, Name name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   639
        assert(member.equals(name.function.member()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   640
        String cname = getInternalName(member.getDeclaringClass());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   641
        String mname = member.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   642
        String mtype;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   643
        byte refKind = member.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   644
        if (refKind == REF_invokeSpecial) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   645
            // in order to pass the verifier, we need to convert this to invokevirtual in all cases
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   646
            assert(member.canBeStaticallyBound()) : member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   647
            refKind = REF_invokeVirtual;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   648
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   649
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   650
        // push arguments
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   651
        for (int i = 0; i < name.arguments.length; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   652
            emitPushArgument(name, i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   653
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   654
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   655
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   656
        if (member.isMethod()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   657
            mtype = member.getMethodType().toMethodDescriptorString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   658
            mv.visitMethodInsn(refKindOpcode(refKind), cname, mname, mtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   659
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   660
            mtype = MethodType.toFieldDescriptorString(member.getFieldType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   661
            mv.visitFieldInsn(refKindOpcode(refKind), cname, mname, mtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   662
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   663
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   664
    int refKindOpcode(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   665
        switch (refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   666
        case REF_invokeVirtual:      return Opcodes.INVOKEVIRTUAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   667
        case REF_invokeStatic:       return Opcodes.INVOKESTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   668
        case REF_invokeSpecial:      return Opcodes.INVOKESPECIAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   669
        case REF_invokeInterface:    return Opcodes.INVOKEINTERFACE;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   670
        case REF_getField:           return Opcodes.GETFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   671
        case REF_putField:           return Opcodes.PUTFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   672
        case REF_getStatic:          return Opcodes.GETSTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   673
        case REF_putStatic:          return Opcodes.PUTSTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   674
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   675
        throw new InternalError("refKind="+refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   676
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   677
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   678
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   679
     * Check if MemberName is a call to MethodHandleImpl.selectAlternative.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   680
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   681
     * @param member
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   682
     * @return true if member is a call to MethodHandleImpl.selectAlternative
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   683
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   684
    private boolean isSelectAlternative(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   685
        return member != null &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   686
               member.getDeclaringClass() == MethodHandleImpl.class &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   687
               member.getName().equals("selectAlternative");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   688
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   689
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   690
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   691
     * Emit bytecode for the selectAlternative idiom.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   692
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   693
     * The pattern looks like (Cf. MethodHandleImpl.makeGuardWithTest):
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   694
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   695
     *   Lambda(a0:L,a1:I)=>{
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   696
     *     t2:I=foo.test(a1:I);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   697
     *     t3:L=MethodHandleImpl.selectAlternative(t2:I,(MethodHandle(int)int),(MethodHandle(int)int));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   698
     *     t4:I=MethodHandle.invokeBasic(t3:L,a1:I);t4:I}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   699
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   700
     * @param selectAlternativeName
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   701
     * @param invokeBasicName
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   702
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   703
    private void emitSelectAlternative(Name selectAlternativeName, Name invokeBasicName) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   704
        MethodType type = selectAlternativeName.function.methodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   705
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   706
        Name receiver = (Name) invokeBasicName.arguments[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   707
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   708
        Label L_fallback = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   709
        Label L_done     = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   710
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   711
        // load test result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   712
        emitPushArgument(selectAlternativeName, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   713
        mv.visitInsn(Opcodes.ICONST_1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   714
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   715
        // if_icmpne L_fallback
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   716
        mv.visitJumpInsn(Opcodes.IF_ICMPNE, L_fallback);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   717
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   718
        // invoke selectAlternativeName.arguments[1]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   719
        MethodHandle target = (MethodHandle) selectAlternativeName.arguments[1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   720
        emitPushArgument(selectAlternativeName, 1);  // get 2nd argument of selectAlternative
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   721
        emitAstoreInsn(receiver.index());  // store the MH in the receiver slot
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   722
        emitInvoke(invokeBasicName);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   723
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   724
        // goto L_done
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   725
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   726
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   727
        // L_fallback:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   728
        mv.visitLabel(L_fallback);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   729
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   730
        // invoke selectAlternativeName.arguments[2]
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   731
        MethodHandle fallback = (MethodHandle) selectAlternativeName.arguments[2];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   732
        emitPushArgument(selectAlternativeName, 2);  // get 3rd argument of selectAlternative
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   733
        emitAstoreInsn(receiver.index());  // store the MH in the receiver slot
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   734
        emitInvoke(invokeBasicName);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   735
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   736
        // L_done:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   737
        mv.visitLabel(L_done);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   738
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   739
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   740
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   741
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   742
     * @param name
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   743
     * @param paramIndex
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   744
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   745
    private void emitPushArgument(Name name, int paramIndex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   746
        Object arg = name.arguments[paramIndex];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   747
        char ptype = name.function.parameterType(paramIndex);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   748
        MethodType mtype = name.function.methodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   749
        if (arg instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   750
            Name n = (Name) arg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   751
            emitLoadInsn(n.type, n.index());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   752
            emitImplicitConversion(n.type, mtype.parameterType(paramIndex));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   753
        } else if ((arg == null || arg instanceof String) && ptype == 'L') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   754
            emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   755
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   756
            if (Wrapper.isWrapperType(arg.getClass()) && ptype != 'L') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   757
                emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   758
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   759
                mv.visitLdcInsn(constantPlaceholder(arg));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   760
                emitImplicitConversion('L', mtype.parameterType(paramIndex));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   761
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   762
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   763
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   764
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   765
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   766
     * Emits a return statement from a LF invoker. If required, the result type is cast to the correct return type.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   767
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   768
    private void emitReturn() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   769
        // return statement
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   770
        if (lambdaForm.result == -1) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   771
            // void
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   772
            mv.visitInsn(Opcodes.RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   773
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   774
            LambdaForm.Name rn = lambdaForm.names[lambdaForm.result];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   775
            char rtype = Wrapper.basicTypeChar(invokerType.returnType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   776
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   777
            // put return value on the stack if it is not already there
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   778
            if (lambdaForm.result != lambdaForm.names.length - 1) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   779
                emitLoadInsn(rn.type, lambdaForm.result);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   780
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   781
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   782
            // potentially generate cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   783
            // rtype is the return type of the invoker - generated code must conform to this
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   784
            // rn.type is the type of the result Name in the LF
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   785
            if (rtype != rn.type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   786
                // need cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   787
                if (rtype == 'L') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   788
                    // possibly cast the primitive to the correct type for boxing
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   789
                    char boxedType = Wrapper.forWrapperType(invokerType.returnType()).basicTypeChar();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   790
                    if (boxedType != rn.type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   791
                        emitPrimCast(rn.type, boxedType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   792
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   793
                    // cast primitive to reference ("boxing")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   794
                    emitBoxing(invokerType.returnType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   795
                } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   796
                    // to-primitive cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   797
                    if (rn.type != 'L') {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   798
                        // prim-to-prim cast
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   799
                        emitPrimCast(rn.type, rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   800
                    } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   801
                        // ref-to-prim cast ("unboxing")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   802
                        throw new InternalError("no ref-to-prim (unboxing) casts supported right now");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   803
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   804
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   805
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   806
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   807
            // generate actual return statement
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   808
            emitReturnInsn(invokerType.returnType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   809
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   810
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   811
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   812
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   813
     * Emit a type conversion bytecode casting from "from" to "to".
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   814
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   815
    private void emitPrimCast(char from, char to) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   816
        // Here's how.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   817
        // -   indicates forbidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   818
        // <-> indicates implicit
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   819
        //      to ----> boolean  byte     short    char     int      long     float    double
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   820
        // from boolean    <->        -        -        -        -        -        -        -
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   821
        //      byte        -       <->       i2s      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   822
        //      short       -       i2b       <->      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   823
        //      char        -       i2b       i2s      <->      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   824
        //      int         -       i2b       i2s      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   825
        //      long        -     l2i,i2b   l2i,i2s  l2i,i2c    l2i      <->      l2f      l2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   826
        //      float       -     f2i,i2b   f2i,i2s  f2i,i2c    f2i      f2l      <->      f2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   827
        //      double      -     d2i,i2b   d2i,i2s  d2i,i2c    d2i      d2l      d2f      <->
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   828
        if (from == to) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   829
            // no cast required, should be dead code anyway
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   830
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   831
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   832
        Wrapper wfrom = Wrapper.forBasicType(from);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   833
        Wrapper wto   = Wrapper.forBasicType(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   834
        if (wfrom.isSubwordOrInt()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   835
            // cast from {byte,short,char,int} to anything
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   836
            emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   837
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   838
            // cast from {long,float,double} to anything
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   839
            if (wto.isSubwordOrInt()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   840
                // cast to {byte,short,char,int}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   841
                emitX2I(from);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   842
                if (wto.bitWidth() < 32) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   843
                    // targets other than int require another conversion
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   844
                    emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   845
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   846
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   847
                // cast to {long,float,double} - this is verbose
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   848
                boolean error = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   849
                switch (from) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   850
                case 'J':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   851
                         if (to == 'F') { mv.visitInsn(Opcodes.L2F); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   852
                    else if (to == 'D') { mv.visitInsn(Opcodes.L2D); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   853
                    else error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   854
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   855
                case 'F':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   856
                         if (to == 'J') { mv.visitInsn(Opcodes.F2L); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   857
                    else if (to == 'D') { mv.visitInsn(Opcodes.F2D); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   858
                    else error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   859
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   860
                case 'D':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   861
                         if (to == 'J') { mv.visitInsn(Opcodes.D2L); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   862
                    else if (to == 'F') { mv.visitInsn(Opcodes.D2F); }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   863
                    else error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   864
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   865
                default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   866
                    error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   867
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   868
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   869
                if (error) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   870
                    throw new IllegalStateException("unhandled prim cast: " + from + "2" + to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   871
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   872
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   873
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   874
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   875
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   876
    private void emitI2X(char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   877
        switch (type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   878
        case 'B':  mv.visitInsn(Opcodes.I2B);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   879
        case 'S':  mv.visitInsn(Opcodes.I2S);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   880
        case 'C':  mv.visitInsn(Opcodes.I2C);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   881
        case 'I':  /* naught */                break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   882
        case 'J':  mv.visitInsn(Opcodes.I2L);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   883
        case 'F':  mv.visitInsn(Opcodes.I2F);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   884
        case 'D':  mv.visitInsn(Opcodes.I2D);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   885
        case 'Z':
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   886
            // For compatibility with ValueConversions and explicitCastArguments:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   887
            mv.visitInsn(Opcodes.ICONST_1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   888
            mv.visitInsn(Opcodes.IAND);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   889
            break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   890
        default:   throw new InternalError("unknown type: " + type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   891
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   892
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   893
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   894
    private void emitX2I(char type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   895
        switch (type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   896
        case 'J':  mv.visitInsn(Opcodes.L2I);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   897
        case 'F':  mv.visitInsn(Opcodes.F2I);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   898
        case 'D':  mv.visitInsn(Opcodes.D2I);  break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   899
        default:   throw new InternalError("unknown type: " + type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   900
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   901
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   902
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   903
    private static String basicTypeCharSignature(String prefix, MethodType type) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   904
        StringBuilder buf = new StringBuilder(prefix);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   905
        for (Class<?> ptype : type.parameterList())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   906
            buf.append(Wrapper.forBasicType(ptype).basicTypeChar());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   907
        buf.append('_').append(Wrapper.forBasicType(type.returnType()).basicTypeChar());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   908
        return buf.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   909
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   910
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   911
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   912
     * Generate bytecode for a LambdaForm.vmentry which calls interpretWithArguments.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   913
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   914
     * @param sig
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   915
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   916
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   917
    static MemberName generateLambdaFormInterpreterEntryPoint(String sig) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   918
        assert(LambdaForm.isValidSignature(sig));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   919
        //System.out.println("generateExactInvoker "+sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   920
        // compute method type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   921
        // first parameter and return type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   922
        char tret = LambdaForm.signatureReturn(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   923
        MethodType type = MethodType.methodType(LambdaForm.typeClass(tret), MethodHandle.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   924
        // other parameter types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   925
        int arity = LambdaForm.signatureArity(sig);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   926
        for (int i = 1; i < arity; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   927
            type = type.appendParameterTypes(LambdaForm.typeClass(sig.charAt(i)));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   928
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   929
        InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("LFI", "interpret_"+tret, type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   930
        return g.loadMethod(g.generateLambdaFormInterpreterEntryPointBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   931
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   932
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   933
    private byte[] generateLambdaFormInterpreterEntryPointBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   934
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   935
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   936
        // Suppress this method in backtraces displayed to the user.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   937
        mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   938
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   939
        // create parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   940
        emitIconstInsn(invokerType.parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   941
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   942
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   943
        // fill parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   944
        for (int i = 0; i < invokerType.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   945
            Class<?> ptype = invokerType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   946
            mv.visitInsn(Opcodes.DUP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   947
            emitIconstInsn(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   948
            emitLoadInsn(Wrapper.basicTypeChar(ptype), i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   949
            // box if primitive type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   950
            if (ptype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   951
                emitBoxing(ptype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   952
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   953
            mv.visitInsn(Opcodes.AASTORE);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   954
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   955
        // invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   956
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   957
        mv.visitFieldInsn(Opcodes.GETFIELD, MH, "form", "Ljava/lang/invoke/LambdaForm;");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   958
        mv.visitInsn(Opcodes.SWAP);  // swap form and array; avoid local variable
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   959
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LF, "interpretWithArguments", "([Ljava/lang/Object;)Ljava/lang/Object;");
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   960
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   961
        // maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   962
        Class<?> rtype = invokerType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   963
        if (rtype.isPrimitive() && rtype != void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   964
            emitUnboxing(Wrapper.asWrapperType(rtype));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   965
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   966
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   967
        // return statement
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   968
        emitReturnInsn(rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   969
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   970
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   971
        bogusMethod(invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   972
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   973
        final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   974
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   975
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   976
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   977
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   978
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   979
     * Generate bytecode for a NamedFunction invoker.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   980
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   981
     * @param srcType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   982
     * @param dstType
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   983
     * @return
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   984
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   985
    static MemberName generateNamedFunctionInvoker(MethodTypeForm typeForm) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   986
        MethodType invokerType = LambdaForm.NamedFunction.INVOKER_METHOD_TYPE;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   987
        String invokerName = basicTypeCharSignature("invoke_", typeForm.erasedType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   988
        InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("NFI", invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   989
        return g.loadMethod(g.generateNamedFunctionInvokerImpl(typeForm));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   990
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   991
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   992
    static int nfi = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   993
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   994
    private byte[] generateNamedFunctionInvokerImpl(MethodTypeForm typeForm) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   995
        MethodType dstType = typeForm.erasedType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   996
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   997
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   998
        // Suppress this method in backtraces displayed to the user.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   999
        mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1000
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1001
        // Load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1002
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1003
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1004
        // Load arguments from array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1005
        for (int i = 0; i < dstType.parameterCount(); i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1006
            emitAloadInsn(1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1007
            emitIconstInsn(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1008
            mv.visitInsn(Opcodes.AALOAD);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1009
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1010
            // Maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1011
            Class<?> dptype = dstType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1012
            if (dptype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1013
                Class<?> sptype = dstType.basicType().wrap().parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1014
                Wrapper dstWrapper = Wrapper.forBasicType(dptype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1015
                Wrapper srcWrapper = dstWrapper.isSubwordOrInt() ? Wrapper.INT : dstWrapper;  // narrow subword from int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1016
                emitUnboxing(srcWrapper.wrapperType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1017
                emitPrimCast(srcWrapper.basicTypeChar(), dstWrapper.basicTypeChar());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1018
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1019
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1020
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1021
        // Invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1022
        String targetDesc = dstType.basicType().toMethodDescriptorString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1023
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", targetDesc);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1024
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1025
        // Box primitive types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1026
        Class<?> rtype = dstType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1027
        if (rtype != void.class && rtype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1028
            Wrapper srcWrapper = Wrapper.forBasicType(rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1029
            Wrapper dstWrapper = srcWrapper.isSubwordOrInt() ? Wrapper.INT : srcWrapper;  // widen subword to int
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1030
            // boolean casts not allowed
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1031
            emitPrimCast(srcWrapper.basicTypeChar(), dstWrapper.basicTypeChar());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1032
            emitBoxing(dstWrapper.primitiveType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1033
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1034
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1035
        // If the return type is void we return a null reference.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1036
        if (rtype == void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1037
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1038
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1039
        emitReturnInsn(Object.class);  // NOTE: NamedFunction invokers always return a reference value.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1040
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1041
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1042
        bogusMethod(dstType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1043
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1044
        final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1045
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1046
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1047
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1048
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1049
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1050
     * Emit a bogus method that just loads some string constants. This is to get the constants into the constant pool
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1051
     * for debugging purposes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1052
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1053
    private void bogusMethod(Object... os) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1054
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1055
            mv = cw.visitMethod(Opcodes.ACC_STATIC, "dummy", "()V", null, null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1056
            for (Object o : os) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1057
                mv.visitLdcInsn(o.toString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1058
                mv.visitInsn(Opcodes.POP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1059
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1060
            mv.visitInsn(Opcodes.RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1061
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1062
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1063
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1064
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1065
}