jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
author mhaupt
Wed, 06 Jul 2016 17:10:36 +0200
changeset 39483 3a7e016ba204
parent 39342 f66a89ed6fca
child 40211 8b78c9b31244
permissions -rw-r--r--
8160717: MethodHandles.loop() does not check for excessive signature Reviewed-by: psandoz, redestad
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
/*
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
13423
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
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    28
import jdk.internal.org.objectweb.asm.ClassWriter;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    29
import jdk.internal.org.objectweb.asm.Label;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    30
import jdk.internal.org.objectweb.asm.MethodVisitor;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    31
import jdk.internal.org.objectweb.asm.Opcodes;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    32
import jdk.internal.org.objectweb.asm.Type;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    33
import sun.invoke.util.VerifyAccess;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    34
import sun.invoke.util.VerifyType;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    35
import sun.invoke.util.Wrapper;
24974
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
    36
import sun.reflect.misc.ReflectUtil;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    37
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    38
import java.io.File;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    39
import java.io.FileOutputStream;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    40
import java.io.IOException;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    41
import java.lang.reflect.Modifier;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    42
import java.util.Arrays;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    43
import java.util.HashMap;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    44
import java.util.Map;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    45
import java.util.stream.Stream;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    46
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    47
import static java.lang.invoke.LambdaForm.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    48
import static java.lang.invoke.LambdaForm.BasicType.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    49
import static java.lang.invoke.MethodHandleNatives.Constants.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    50
import static java.lang.invoke.MethodHandleStatics.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    51
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    52
/**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    53
 * Code generation backend for LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    54
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    55
 * @author John Rose, JSR 292 EG
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    56
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    57
class InvokerBytecodeGenerator {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    58
    /** Define class names for convenience. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    59
    private static final String MH      = "java/lang/invoke/MethodHandle";
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23038
diff changeset
    60
    private static final String MHI     = "java/lang/invoke/MethodHandleImpl";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    61
    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
    62
    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
    63
    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
    64
    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
    65
    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
    66
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    67
    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
    68
    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
    69
    private static final String LL_SIG  = "(L" + OBJ + ";)L" + OBJ + ";";
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
    70
    private static final String LLV_SIG = "(L" + OBJ + ";L" + OBJ + ";)V";
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    71
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    72
    /** Name of its super class*/
26637
6eec8f530e94 8058661: Compiled LambdaForms should inherit from Object to improve class loading performance
vlivanov
parents: 26476
diff changeset
    73
    private static final String superName = OBJ;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    74
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    75
    /** Name of new class */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    76
    private final String className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    77
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    78
    /** 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
    79
    private final String sourceFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    80
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    81
    private final LambdaForm lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    82
    private final String     invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    83
    private final MethodType invokerType;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    84
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    85
    /** Info about local variables in compiled lambda form */
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    86
    private int[]       localsMap;    // index
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    87
    private Class<?>[]  localClasses; // type
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    88
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    89
    /** ASM bytecode generation. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    90
    private ClassWriter cw;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    91
    private MethodVisitor mv;
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
    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
    94
    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
    95
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    96
    /** Main constructor; other constructors delegate to this one. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    97
    private InvokerBytecodeGenerator(LambdaForm lambdaForm, int localsMapSize,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    98
                                     String className, String invokerName, MethodType invokerType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    99
        if (invokerName.contains(".")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24572
diff changeset
   100
            int p = invokerName.indexOf('.');
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   101
            className = invokerName.substring(0, p);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   102
            invokerName = invokerName.substring(p+1);
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
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   105
            className = makeDumpableClassName(className);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   106
        }
26637
6eec8f530e94 8058661: Compiled LambdaForms should inherit from Object to improve class loading performance
vlivanov
parents: 26476
diff changeset
   107
        this.className  = LF + "$" + className;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   108
        this.sourceFile = "LambdaForm$" + className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   109
        this.lambdaForm = lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   110
        this.invokerName = invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   111
        this.invokerType = invokerType;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   112
        this.localsMap = new int[localsMapSize+1]; // last entry of localsMap is count of allocated local slots
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   113
        this.localClasses = new Class<?>[localsMapSize+1];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   114
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   115
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   116
    /** For generating LambdaForm interpreter entry points. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   117
    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
   118
        this(null, invokerType.parameterCount(),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   119
             className, invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   120
        // 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
   121
        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
   122
            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
   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
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   126
    /** For generating customized code for a single LambdaForm. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   127
    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
   128
        this(form, form.names.length,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   129
             className, form.debugName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   130
        // 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
   131
        Name[] names = form.names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   132
        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
   133
            localsMap[i] = index;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   134
            if (i < names.length) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   135
                BasicType type = names[i].type();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   136
                index += type.basicTypeSlots();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   137
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   138
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   139
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   140
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   141
    /** instance counters for dumped classes */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   142
    private static final HashMap<String,Integer> DUMP_CLASS_FILES_COUNTERS;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   143
    /** debugging flag for saving generated class files */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   144
    private static final File DUMP_CLASS_FILES_DIR;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   145
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   146
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   147
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   148
            DUMP_CLASS_FILES_COUNTERS = new HashMap<>();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   149
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   150
                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
   151
                if (!dumpDir.exists()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   152
                    dumpDir.mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   153
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   154
                DUMP_CLASS_FILES_DIR = dumpDir;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   155
                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
   156
            } catch (Exception e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   157
                throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   158
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   159
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   160
            DUMP_CLASS_FILES_COUNTERS = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   161
            DUMP_CLASS_FILES_DIR = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   162
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   163
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   164
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   165
    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
   166
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   167
            java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29020
diff changeset
   168
            new java.security.PrivilegedAction<>() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   169
                public Void run() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   170
                    try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   171
                        String dumpName = className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   172
                        //dumpName = dumpName.replace('/', '-');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   173
                        File dumpFile = new File(DUMP_CLASS_FILES_DIR, dumpName+".class");
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   174
                        System.out.println("dump: " + dumpFile);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   175
                        dumpFile.getParentFile().mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   176
                        FileOutputStream file = new FileOutputStream(dumpFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   177
                        file.write(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   178
                        file.close();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   179
                        return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   180
                    } catch (IOException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   181
                        throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   182
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   183
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   184
            });
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
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   188
    private static String makeDumpableClassName(String className) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   189
        Integer ctr;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   190
        synchronized (DUMP_CLASS_FILES_COUNTERS) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   191
            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
   192
            if (ctr == null)  ctr = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   193
            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
   194
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   195
        String sfx = ctr.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   196
        while (sfx.length() < 3)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   197
            sfx = "0"+sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   198
        className += sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   199
        return className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   200
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   201
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   202
    class CpPatch {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   203
        final int index;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   204
        final String placeholder;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   205
        final Object value;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   206
        CpPatch(int index, String placeholder, Object value) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   207
            this.index = index;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   208
            this.placeholder = placeholder;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   209
            this.value = value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   210
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   211
        public String toString() {
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   212
            return "CpPatch/index="+index+",placeholder="+placeholder+",value="+value;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   213
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   214
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   215
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   216
    Map<Object, CpPatch> cpPatches = new HashMap<>();
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
    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
   219
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   220
    String constantPlaceholder(Object arg) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   221
        String cpPlaceholder = "CONSTANT_PLACEHOLDER_" + cph++;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   222
        if (DUMP_CLASS_FILES) cpPlaceholder += " <<" + debugString(arg) + ">>";  // debugging aid
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   223
        if (cpPatches.containsKey(cpPlaceholder)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   224
            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
   225
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   226
        // insert placeholder in CP and remember the patch
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 24974
diff changeset
   227
        int index = cw.newConst((Object) cpPlaceholder);  // TODO check if already in the constant pool
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   228
        cpPatches.put(cpPlaceholder, new CpPatch(index, cpPlaceholder, arg));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   229
        return cpPlaceholder;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   230
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   231
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   232
    Object[] cpPatches(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   233
        int size = getConstantPoolSize(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   234
        Object[] res = new Object[size];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   235
        for (CpPatch p : cpPatches.values()) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   236
            if (p.index >= size)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   237
                throw new InternalError("in cpool["+size+"]: "+p+"\n"+Arrays.toString(Arrays.copyOf(classFile, 20)));
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   238
            res[p.index] = p.value;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   239
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   240
        return res;
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
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   243
    private static String debugString(Object arg) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   244
        if (arg instanceof MethodHandle) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   245
            MethodHandle mh = (MethodHandle) arg;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   246
            MemberName member = mh.internalMemberName();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   247
            if (member != null)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   248
                return member.toString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   249
            return mh.debugString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   250
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   251
        return arg.toString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   252
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   253
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   254
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   255
     * 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
   256
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   257
     * @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
   258
     * @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
   259
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   260
    private static int getConstantPoolSize(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   261
        // The first few bytes:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   262
        // u4 magic;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   263
        // u2 minor_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   264
        // u2 major_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   265
        // u2 constant_pool_count;
13425
a8d96a0eda69 7190416: JSR 292: typo in InvokerBytecodeGenerator.getConstantPoolSize
twisti
parents: 13423
diff changeset
   266
        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
   267
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   268
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   269
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   270
     * 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
   271
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   272
    private MemberName loadMethod(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   273
        Class<?> invokerClass = loadAndInitializeInvokerClass(classFile, cpPatches(classFile));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   274
        return resolveInvokerMember(invokerClass, invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   275
    }
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
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   278
     * 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
   279
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   280
    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
   281
        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
   282
        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
   283
        return invokerClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   284
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   285
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   286
    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
   287
        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
   288
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   289
            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
   290
        } catch (ReflectiveOperationException e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   291
            throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   292
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   293
        return member;
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
     * Set up 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 classFilePrologue() {
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23039
diff changeset
   300
        final int NOT_ACC_PUBLIC = 0;  // not ACC_PUBLIC
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   301
        cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23039
diff changeset
   302
        cw.visit(Opcodes.V1_8, NOT_ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   303
        cw.visitSource(sourceFile, null);
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
        String invokerDesc = invokerType.toMethodDescriptorString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   306
        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
   307
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   308
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   309
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   310
     * Tear down class file generation.
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
    private void classFileEpilogue() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   313
        mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   314
        mv.visitEnd();
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
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   317
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   318
     * Low-level emit helpers.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   319
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   320
    private void emitConst(Object con) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   321
        if (con == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   322
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   323
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   324
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   325
        if (con instanceof Integer) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   326
            emitIconstInsn((int) con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   327
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   328
        }
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   329
        if (con instanceof Byte) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   330
            emitIconstInsn((byte)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   331
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   332
        }
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   333
        if (con instanceof Short) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   334
            emitIconstInsn((short)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   335
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   336
        }
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   337
        if (con instanceof Character) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   338
            emitIconstInsn((char)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   339
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   340
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   341
        if (con instanceof Long) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   342
            long x = (long) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   343
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   344
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   345
                if (sx >= 0 && sx <= 1) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   346
                    mv.visitInsn(Opcodes.LCONST_0 + (int) sx);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   347
                } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   348
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   349
                    mv.visitInsn(Opcodes.I2L);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   350
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   351
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   352
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   353
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   354
        if (con instanceof Float) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   355
            float x = (float) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   356
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   357
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   358
                if (sx >= 0 && sx <= 2) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   359
                    mv.visitInsn(Opcodes.FCONST_0 + (int) sx);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   360
                } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   361
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   362
                    mv.visitInsn(Opcodes.I2F);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   363
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   364
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   365
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   366
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   367
        if (con instanceof Double) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   368
            double x = (double) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   369
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   370
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   371
                if (sx >= 0 && sx <= 1) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   372
                    mv.visitInsn(Opcodes.DCONST_0 + (int) sx);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   373
                } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   374
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   375
                    mv.visitInsn(Opcodes.I2D);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   376
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   377
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   378
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   379
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   380
        if (con instanceof Boolean) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   381
            emitIconstInsn((boolean) con ? 1 : 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   382
            return;
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
        // fall through:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   385
        mv.visitLdcInsn(con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   386
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   387
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   388
    private void emitIconstInsn(final int cst) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   389
        if (cst >= -1 && cst <= 5) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   390
            mv.visitInsn(Opcodes.ICONST_0 + cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   391
        } else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   392
            mv.visitIntInsn(Opcodes.BIPUSH, cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   393
        } else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   394
            mv.visitIntInsn(Opcodes.SIPUSH, cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   395
        } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   396
            mv.visitLdcInsn(cst);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   397
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   398
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   399
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   400
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   401
     * 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
   402
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   403
    private void emitLoadInsn(BasicType type, int index) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   404
        int opcode = loadInsnOpcode(type);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   405
        mv.visitVarInsn(opcode, localsMap[index]);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   406
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   407
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   408
    private int loadInsnOpcode(BasicType type) throws InternalError {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   409
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   410
            case I_TYPE: return Opcodes.ILOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   411
            case J_TYPE: return Opcodes.LLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   412
            case F_TYPE: return Opcodes.FLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   413
            case D_TYPE: return Opcodes.DLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   414
            case L_TYPE: return Opcodes.ALOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   415
            default:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   416
                throw new InternalError("unknown type: " + type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   417
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   418
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   419
    private void emitAloadInsn(int index) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   420
        emitLoadInsn(L_TYPE, index);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   421
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   422
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   423
    private void emitStoreInsn(BasicType type, int index) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   424
        int opcode = storeInsnOpcode(type);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   425
        mv.visitVarInsn(opcode, localsMap[index]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   426
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   427
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   428
    private int storeInsnOpcode(BasicType type) throws InternalError {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   429
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   430
            case I_TYPE: return Opcodes.ISTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   431
            case J_TYPE: return Opcodes.LSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   432
            case F_TYPE: return Opcodes.FSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   433
            case D_TYPE: return Opcodes.DSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   434
            case L_TYPE: return Opcodes.ASTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   435
            default:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   436
                throw new InternalError("unknown type: " + type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   437
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   438
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   439
    private void emitAstoreInsn(int index) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   440
        emitStoreInsn(L_TYPE, index);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   441
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   442
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   443
    private byte arrayTypeCode(Wrapper elementType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   444
        switch (elementType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   445
            case BOOLEAN: return Opcodes.T_BOOLEAN;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   446
            case BYTE:    return Opcodes.T_BYTE;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   447
            case CHAR:    return Opcodes.T_CHAR;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   448
            case SHORT:   return Opcodes.T_SHORT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   449
            case INT:     return Opcodes.T_INT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   450
            case LONG:    return Opcodes.T_LONG;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   451
            case FLOAT:   return Opcodes.T_FLOAT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   452
            case DOUBLE:  return Opcodes.T_DOUBLE;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   453
            case OBJECT:  return 0; // in place of Opcodes.T_OBJECT
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   454
            default:      throw new InternalError();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   455
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   456
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   457
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   458
    private int arrayInsnOpcode(byte tcode, int aaop) throws InternalError {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   459
        assert(aaop == Opcodes.AASTORE || aaop == Opcodes.AALOAD);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   460
        int xas;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   461
        switch (tcode) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   462
            case Opcodes.T_BOOLEAN: xas = Opcodes.BASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   463
            case Opcodes.T_BYTE:    xas = Opcodes.BASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   464
            case Opcodes.T_CHAR:    xas = Opcodes.CASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   465
            case Opcodes.T_SHORT:   xas = Opcodes.SASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   466
            case Opcodes.T_INT:     xas = Opcodes.IASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   467
            case Opcodes.T_LONG:    xas = Opcodes.LASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   468
            case Opcodes.T_FLOAT:   xas = Opcodes.FASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   469
            case Opcodes.T_DOUBLE:  xas = Opcodes.DASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   470
            case 0:                 xas = Opcodes.AASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   471
            default:      throw new InternalError();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   472
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   473
        return xas - Opcodes.AASTORE + aaop;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   474
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   475
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   476
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   477
     * Emit a boxing call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   478
     *
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   479
     * @param wrapper primitive type class to box.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   480
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   481
    private void emitBoxing(Wrapper wrapper) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   482
        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
   483
        String name  = "valueOf";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   484
        String desc  = "(" + wrapper.basicTypeChar() + ")L" + owner + ";";
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   485
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, desc, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   486
    }
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
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   489
     * 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
   490
     *
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   491
     * @param wrapper wrapper type class to unbox.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   492
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   493
    private void emitUnboxing(Wrapper wrapper) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   494
        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
   495
        String name  = wrapper.primitiveSimpleName() + "Value";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   496
        String desc  = "()" + wrapper.basicTypeChar();
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   497
        emitReferenceCast(wrapper.wrapperType(), null);
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   498
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   499
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   500
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   501
    /**
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   502
     * Emit an implicit conversion for an argument which must be of the given pclass.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   503
     * This is usually a no-op, except when pclass is a subword type or a reference other than Object or an interface.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   504
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   505
     * @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
   506
     * @param pclass type of value required on stack
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   507
     * @param arg compile-time representation of value on stack (Node, constant) or null if none
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   508
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   509
    private void emitImplicitConversion(BasicType ptype, Class<?> pclass, Object arg) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   510
        assert(basicType(pclass) == ptype);  // boxing/unboxing handled by caller
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   511
        if (pclass == ptype.basicTypeClass() && ptype != L_TYPE)
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   512
            return;   // nothing to do
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   513
        switch (ptype) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   514
            case L_TYPE:
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   515
                if (VerifyType.isNullConversion(Object.class, pclass, false)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   516
                    if (PROFILE_LEVEL > 0)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   517
                        emitReferenceCast(Object.class, arg);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   518
                    return;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   519
                }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   520
                emitReferenceCast(pclass, arg);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   521
                return;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   522
            case I_TYPE:
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   523
                if (!VerifyType.isNullConversion(int.class, pclass, false))
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   524
                    emitPrimCast(ptype.basicTypeWrapper(), Wrapper.forPrimitiveType(pclass));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   525
                return;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   526
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   527
        throw newInternalError("bad implicit conversion: tc="+ptype+": "+pclass);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   528
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   529
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   530
    /** Update localClasses type map.  Return true if the information is already present. */
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   531
    private boolean assertStaticType(Class<?> cls, Name n) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   532
        int local = n.index();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   533
        Class<?> aclass = localClasses[local];
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   534
        if (aclass != null && (aclass == cls || cls.isAssignableFrom(aclass))) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   535
            return true;  // type info is already present
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   536
        } else if (aclass == null || aclass.isAssignableFrom(cls)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   537
            localClasses[local] = cls;  // type info can be improved
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   538
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   539
        return false;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   540
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   541
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   542
    private void emitReferenceCast(Class<?> cls, Object arg) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   543
        Name writeBack = null;  // local to write back result
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   544
        if (arg instanceof Name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   545
            Name n = (Name) arg;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   546
            if (lambdaForm.useCount(n) > 1) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   547
                // This guy gets used more than once.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   548
                writeBack = n;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   549
                if (assertStaticType(cls, n)) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   550
                    return; // this cast was already performed
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   551
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   552
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   553
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   554
        if (isStaticallyNameable(cls)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   555
            String sig = getInternalName(cls);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   556
            mv.visitTypeInsn(Opcodes.CHECKCAST, sig);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   557
        } else {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   558
            mv.visitLdcInsn(constantPlaceholder(cls));
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   559
            mv.visitTypeInsn(Opcodes.CHECKCAST, CLS);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   560
            mv.visitInsn(Opcodes.SWAP);
31802
cb6542e7d329 8062543: Replace uses of MethodHandleImpl.castReference with Class.cast
mhaupt
parents: 31253
diff changeset
   561
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CLS, "cast", LL_SIG, false);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   562
            if (Object[].class.isAssignableFrom(cls))
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   563
                mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   564
            else if (PROFILE_LEVEL > 0)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   565
                mv.visitTypeInsn(Opcodes.CHECKCAST, OBJ);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   566
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   567
        if (writeBack != null) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   568
            mv.visitInsn(Opcodes.DUP);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   569
            emitAstoreInsn(writeBack.index());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   570
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   571
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   572
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   573
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   574
     * 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
   575
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   576
    private void emitReturnInsn(BasicType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   577
        int opcode;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   578
        switch (type) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   579
        case I_TYPE:  opcode = Opcodes.IRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   580
        case J_TYPE:  opcode = Opcodes.LRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   581
        case F_TYPE:  opcode = Opcodes.FRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   582
        case D_TYPE:  opcode = Opcodes.DRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   583
        case L_TYPE:  opcode = Opcodes.ARETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   584
        case V_TYPE:  opcode = Opcodes.RETURN;   break;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   585
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   586
            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
   587
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   588
        mv.visitInsn(opcode);
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
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   591
    private static String getInternalName(Class<?> c) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   592
        if (c == Object.class)             return OBJ;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   593
        else if (c == Object[].class)      return OBJARY;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   594
        else if (c == Class.class)         return CLS;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   595
        else if (c == MethodHandle.class)  return MH;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   596
        assert(VerifyAccess.isTypeVisible(c, Object.class)) : c.getName();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   597
        return c.getName().replace('.', '/');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   598
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   599
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   600
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   601
     * 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
   602
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   603
    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
   604
        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
   605
        return g.loadMethod(g.generateCustomizedCodeBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   606
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   607
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   608
    /** Generates code to check that actual receiver and LambdaForm matches */
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   609
    private boolean checkActualReceiver() {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   610
        // Expects MethodHandle on the stack and actual receiver MethodHandle in slot #0
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   611
        mv.visitInsn(Opcodes.DUP);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   612
        mv.visitVarInsn(Opcodes.ALOAD, localsMap[0]);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   613
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, MHI, "assertSame", LLV_SIG, false);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   614
        return true;
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   615
    }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   616
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   617
    static String className(String cn) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   618
        assert checkClassName(cn): "Class not found: " + cn;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   619
        return cn;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   620
    }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   621
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   622
    static boolean checkClassName(String cn) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   623
        Type tp = Type.getType(cn);
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   624
        // additional sanity so only valid "L;" descriptors work
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   625
        if (tp.getSort() != Type.OBJECT) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   626
            return false;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   627
        }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   628
        try {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   629
            Class<?> c = Class.forName(tp.getClassName(), false, null);
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   630
            return true;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   631
        } catch (ClassNotFoundException e) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   632
            return false;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   633
        }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   634
    }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   635
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   636
    static final String  LF_HIDDEN_SIG = className("Ljava/lang/invoke/LambdaForm$Hidden;");
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   637
    static final String  LF_COMPILED_SIG = className("Ljava/lang/invoke/LambdaForm$Compiled;");
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   638
    static final String  FORCEINLINE_SIG = className("Ljdk/internal/vm/annotation/ForceInline;");
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   639
    static final String  DONTINLINE_SIG = className("Ljdk/internal/vm/annotation/DontInline;");
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   640
    static final String  INJECTEDPROFILE_SIG = className("Ljava/lang/invoke/InjectedProfile;");
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   641
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   642
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   643
     * 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
   644
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   645
    private byte[] generateCustomizedCodeBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   646
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   647
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   648
        // Suppress this method in backtraces displayed to the user.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   649
        mv.visitAnnotation(LF_HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   650
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   651
        // Mark this method as a compiled LambdaForm
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   652
        mv.visitAnnotation(LF_COMPILED_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   653
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   654
        if (lambdaForm.forceInline) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   655
            // Force inlining of this invoker method.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   656
            mv.visitAnnotation(FORCEINLINE_SIG, true);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   657
        } else {
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   658
            mv.visitAnnotation(DONTINLINE_SIG, true);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   659
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   660
37334
1169e9593135 8067247: Crash: assert(method_holder->data() == 0 ...) failed: a) MT-unsafe modification of inline cache
jcm
parents: 36016
diff changeset
   661
        constantPlaceholder(lambdaForm); // keep LambdaForm instance & its compiled form lifetime tightly coupled.
1169e9593135 8067247: Crash: assert(method_holder->data() == 0 ...) failed: a) MT-unsafe modification of inline cache
jcm
parents: 36016
diff changeset
   662
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   663
        if (lambdaForm.customized != null) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   664
            // Since LambdaForm is customized for a particular MethodHandle, it's safe to substitute
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   665
            // receiver MethodHandle (at slot #0) with an embedded constant and use it instead.
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   666
            // It enables more efficient code generation in some situations, since embedded constants
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   667
            // are compile-time constants for JIT compiler.
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   668
            mv.visitLdcInsn(constantPlaceholder(lambdaForm.customized));
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   669
            mv.visitTypeInsn(Opcodes.CHECKCAST, MH);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   670
            assert(checkActualReceiver()); // expects MethodHandle on top of the stack
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   671
            mv.visitVarInsn(Opcodes.ASTORE, localsMap[0]);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   672
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   673
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   674
        // 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
   675
        // start iterating at the first name following the arguments
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   676
        Name onStack = null;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   677
        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
   678
            Name name = lambdaForm.names[i];
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   679
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   680
            emitStoreResult(onStack);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   681
            onStack = name;  // unless otherwise modified below
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   682
            MethodHandleImpl.Intrinsic intr = name.function.intrinsicName();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   683
            switch (intr) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   684
                case SELECT_ALTERNATIVE:
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   685
                    assert lambdaForm.isSelectAlternative(i);
31253
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   686
                    if (PROFILE_GWT) {
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   687
                        assert(name.arguments[0] instanceof Name &&
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   688
                                ((Name)name.arguments[0]).refersTo(MethodHandleImpl.class, "profileBoolean"));
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   689
                        mv.visitAnnotation(INJECTEDPROFILE_SIG, true);
31253
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   690
                    }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   691
                    onStack = emitSelectAlternative(name, lambdaForm.names[i+1]);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   692
                    i++;  // skip MH.invokeBasic of the selectAlternative result
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   693
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   694
                case GUARD_WITH_CATCH:
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   695
                    assert lambdaForm.isGuardWithCatch(i);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   696
                    onStack = emitGuardWithCatch(i);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   697
                    i += 2; // jump to the end of GWC idiom
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   698
                    continue;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   699
                case TRY_FINALLY:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   700
                    assert lambdaForm.isTryFinally(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   701
                    onStack = emitTryFinally(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   702
                    i += 2; // jump to the end of the TF idiom
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   703
                    continue;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   704
                case LOOP:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   705
                    assert lambdaForm.isLoop(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   706
                    onStack = emitLoop(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   707
                    i += 2; // jump to the end of the LOOP idiom
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   708
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   709
                case NEW_ARRAY:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   710
                    Class<?> rtype = name.function.methodType().returnType();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   711
                    if (isStaticallyNameable(rtype)) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   712
                        emitNewArray(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   713
                        continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   714
                    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   715
                    break;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   716
                case ARRAY_LOAD:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   717
                    emitArrayLoad(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   718
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   719
                case ARRAY_STORE:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   720
                    emitArrayStore(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   721
                    continue;
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   722
                case ARRAY_LENGTH:
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   723
                    emitArrayLength(name);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   724
                    continue;
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   725
                case IDENTITY:
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   726
                    assert(name.arguments.length == 1);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   727
                    emitPushArguments(name);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   728
                    continue;
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   729
                case ZERO:
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   730
                    assert(name.arguments.length == 0);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   731
                    emitConst(name.type.basicTypeWrapper().zero());
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   732
                    continue;
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   733
                case NONE:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   734
                    // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   735
                    break;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   736
                default:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   737
                    throw newInternalError("Unknown intrinsic: "+intr);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   738
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   739
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   740
            MemberName member = name.function.member();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   741
            if (isStaticallyInvocable(member)) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   742
                emitStaticInvoke(member, name);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   743
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   744
                emitInvoke(name);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   745
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   746
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   747
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   748
        // return statement
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   749
        emitReturn(onStack);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   750
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   751
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   752
        bogusMethod(lambdaForm);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   753
39483
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   754
        final byte[] classFile;
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   755
        try {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   756
            classFile = cw.toByteArray();
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   757
        } catch (RuntimeException e) {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   758
            // ASM throws RuntimeException if something goes wrong - capture these and wrap them in a meaningful
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   759
            // exception to support falling back to LambdaForm interpretation
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   760
            throw new BytecodeGenerationException(e);
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   761
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   762
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   763
        return classFile;
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
39483
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   766
    @SuppressWarnings("serial")
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   767
    static final class BytecodeGenerationException extends RuntimeException {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   768
        BytecodeGenerationException(Exception cause) {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   769
            super(cause);
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   770
        }
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   771
    }
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   772
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   773
    void emitArrayLoad(Name name)   { emitArrayOp(name, Opcodes.AALOAD);      }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   774
    void emitArrayStore(Name name)  { emitArrayOp(name, Opcodes.AASTORE);     }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   775
    void emitArrayLength(Name name) { emitArrayOp(name, Opcodes.ARRAYLENGTH); }
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   776
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   777
    void emitArrayOp(Name name, int arrayOpcode) {
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   778
        assert arrayOpcode == Opcodes.AALOAD || arrayOpcode == Opcodes.AASTORE || arrayOpcode == Opcodes.ARRAYLENGTH;
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   779
        Class<?> elementType = name.function.methodType().parameterType(0).getComponentType();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   780
        assert elementType != null;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   781
        emitPushArguments(name);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   782
        if (arrayOpcode != Opcodes.ARRAYLENGTH && elementType.isPrimitive()) {
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   783
            Wrapper w = Wrapper.forPrimitiveType(elementType);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   784
            arrayOpcode = arrayInsnOpcode(arrayTypeCode(w), arrayOpcode);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   785
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   786
        mv.visitInsn(arrayOpcode);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   787
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   788
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   789
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   790
     * 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
   791
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   792
    void emitInvoke(Name name) {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   793
        assert(!name.isLinkerMethodInvoke());  // should use the static path for these
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   794
        if (true) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   795
            // push receiver
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   796
            MethodHandle target = name.function.resolvedHandle();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   797
            assert(target != null) : name.exprString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   798
            mv.visitLdcInsn(constantPlaceholder(target));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   799
            emitReferenceCast(MethodHandle.class, target);
13423
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
            // load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   802
            emitAloadInsn(0);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   803
            emitReferenceCast(MethodHandle.class, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   804
            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
   805
            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
   806
            // TODO more to come
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   807
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   808
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   809
        // push arguments
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   810
        emitPushArguments(name);
13423
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
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   813
        MethodType type = name.function.methodType();
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   814
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   815
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   816
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   817
    private static Class<?>[] STATICALLY_INVOCABLE_PACKAGES = {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   818
        // 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
   819
        java.lang.Object.class,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   820
        java.util.Arrays.class,
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32649
diff changeset
   821
        jdk.internal.misc.Unsafe.class
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   822
        //MethodHandle.class already covered
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   823
    };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   824
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   825
    static boolean isStaticallyInvocable(NamedFunction[] functions) {
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   826
        for (NamedFunction nf : functions) {
33846
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
   827
            if (!isStaticallyInvocable(nf.member())) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   828
                return false;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   829
            }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   830
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   831
        return true;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   832
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   833
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   834
    static boolean isStaticallyInvocable(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   835
        return isStaticallyInvocable(name.function.member());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   836
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   837
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   838
    static boolean isStaticallyInvocable(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   839
        if (member == null)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   840
        if (member.isConstructor())  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   841
        Class<?> cls = member.getDeclaringClass();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   842
        if (cls.isArray() || cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   843
            return false;  // FIXME
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   844
        if (cls.isAnonymousClass() || cls.isLocalClass())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   845
            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
   846
        if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   847
            return false;  // not on BCP
24974
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   848
        if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   849
            return false;
19816
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   850
        MethodType mtype = member.getMethodOrFieldType();
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   851
        if (!isStaticallyNameable(mtype.returnType()))
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   852
            return false;
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   853
        for (Class<?> ptype : mtype.parameterArray())
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   854
            if (!isStaticallyNameable(ptype))
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   855
                return false;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   856
        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
   857
            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
   858
        if (member.isPublic() && isStaticallyNameable(cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   859
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   860
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   861
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   862
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   863
    static boolean isStaticallyNameable(Class<?> cls) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   864
        if (cls == Object.class)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   865
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   866
        while (cls.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   867
            cls = cls.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   868
        if (cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   869
            return true;  // int[].class, for example
24974
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   870
        if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   871
            return false;
20872
8e486b70dff8 8022718: Runtime accessibility checking: protected class, if extended, should be accessible from another package
drchase
parents: 20535
diff changeset
   872
        // could use VerifyAccess.isClassAccessible but the following is a safe approximation
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   873
        if (cls.getClassLoader() != Object.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   874
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   875
        if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   876
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   877
        if (!Modifier.isPublic(cls.getModifiers()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   878
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   879
        for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   880
            if (VerifyAccess.isSamePackage(pkgcls, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   881
                return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   882
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   883
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   884
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   885
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   886
    void emitStaticInvoke(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   887
        emitStaticInvoke(name.function.member(), name);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   888
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   889
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   890
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   891
     * 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
   892
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   893
    void emitStaticInvoke(MemberName member, Name name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   894
        assert(member.equals(name.function.member()));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   895
        Class<?> defc = member.getDeclaringClass();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   896
        String cname = getInternalName(defc);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   897
        String mname = member.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   898
        String mtype;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   899
        byte refKind = member.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   900
        if (refKind == REF_invokeSpecial) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   901
            // 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
   902
            assert(member.canBeStaticallyBound()) : member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   903
            refKind = REF_invokeVirtual;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   904
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   905
30343
bf75edd66df0 8033465: JSR292: InvokerBytecodeGenerator: convert a check for REF_invokeVirtual on an interface into an assert
mhaupt
parents: 29020
diff changeset
   906
        assert(!(member.getDeclaringClass().isInterface() && refKind == REF_invokeVirtual));
22287
7bb2a658a502 8031502: JSR292: IncompatibleClassChangeError in LambdaForm for CharSequence.toString() method handle type converter
vlivanov
parents: 21618
diff changeset
   907
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   908
        // push arguments
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   909
        emitPushArguments(name);
13423
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
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   912
        if (member.isMethod()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   913
            mtype = member.getMethodType().toMethodDescriptorString();
21618
6632680b0683 8027232: Update j.l.invoke code generating class files to use ASM enhancements for invocation of non-abstract methods on ifaces
ksrini
parents: 20872
diff changeset
   914
            mv.visitMethodInsn(refKindOpcode(refKind), cname, mname, mtype,
6632680b0683 8027232: Update j.l.invoke code generating class files to use ASM enhancements for invocation of non-abstract methods on ifaces
ksrini
parents: 20872
diff changeset
   915
                               member.getDeclaringClass().isInterface());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   916
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   917
            mtype = MethodType.toFieldDescriptorString(member.getFieldType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   918
            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
   919
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   920
        // Issue a type assertion for the result, so we can avoid casts later.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   921
        if (name.type == L_TYPE) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   922
            Class<?> rtype = member.getInvocationType().returnType();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   923
            assert(!rtype.isPrimitive());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   924
            if (rtype != Object.class && !rtype.isInterface()) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   925
                assertStaticType(rtype, name);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   926
            }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   927
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   928
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   929
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   930
    void emitNewArray(Name name) throws InternalError {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   931
        Class<?> rtype = name.function.methodType().returnType();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   932
        if (name.arguments.length == 0) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   933
            // The array will be a constant.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   934
            Object emptyArray;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   935
            try {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   936
                emptyArray = name.function.resolvedHandle().invoke();
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   937
            } catch (Throwable ex) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   938
                throw newInternalError(ex);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   939
            }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   940
            assert(java.lang.reflect.Array.getLength(emptyArray) == 0);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   941
            assert(emptyArray.getClass() == rtype);  // exact typing
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   942
            mv.visitLdcInsn(constantPlaceholder(emptyArray));
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   943
            emitReferenceCast(rtype, emptyArray);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   944
            return;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   945
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   946
        Class<?> arrayElementType = rtype.getComponentType();
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   947
        assert(arrayElementType != null);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   948
        emitIconstInsn(name.arguments.length);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   949
        int xas = Opcodes.AASTORE;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   950
        if (!arrayElementType.isPrimitive()) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   951
            mv.visitTypeInsn(Opcodes.ANEWARRAY, getInternalName(arrayElementType));
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   952
        } else {
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   953
            byte tc = arrayTypeCode(Wrapper.forPrimitiveType(arrayElementType));
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   954
            xas = arrayInsnOpcode(tc, xas);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   955
            mv.visitIntInsn(Opcodes.NEWARRAY, tc);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   956
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   957
        // store arguments
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   958
        for (int i = 0; i < name.arguments.length; i++) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   959
            mv.visitInsn(Opcodes.DUP);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   960
            emitIconstInsn(i);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   961
            emitPushArgument(name, i);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   962
            mv.visitInsn(xas);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   963
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   964
        // the array is left on the stack
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   965
        assertStaticType(rtype, name);
13423
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
    int refKindOpcode(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   968
        switch (refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   969
        case REF_invokeVirtual:      return Opcodes.INVOKEVIRTUAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   970
        case REF_invokeStatic:       return Opcodes.INVOKESTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   971
        case REF_invokeSpecial:      return Opcodes.INVOKESPECIAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   972
        case REF_invokeInterface:    return Opcodes.INVOKEINTERFACE;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   973
        case REF_getField:           return Opcodes.GETFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   974
        case REF_putField:           return Opcodes.PUTFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   975
        case REF_getStatic:          return Opcodes.GETSTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   976
        case REF_putStatic:          return Opcodes.PUTSTATIC;
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
        throw new InternalError("refKind="+refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   979
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   980
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   981
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   982
     * Emit bytecode for the selectAlternative idiom.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   983
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   984
     * The pattern looks like (Cf. MethodHandleImpl.makeGuardWithTest):
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 19816
diff changeset
   985
     * <blockquote><pre>{@code
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   986
     *   Lambda(a0:L,a1:I)=>{
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   987
     *     t2:I=foo.test(a1:I);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   988
     *     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
   989
     *     t4:I=MethodHandle.invokeBasic(t3:L,a1:I);t4:I}
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 19816
diff changeset
   990
     * }</pre></blockquote>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   991
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   992
    private Name emitSelectAlternative(Name selectAlternativeName, Name invokeBasicName) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   993
        assert isStaticallyInvocable(invokeBasicName);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   994
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   995
        Name receiver = (Name) invokeBasicName.arguments[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   996
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   997
        Label L_fallback = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   998
        Label L_done     = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   999
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1000
        // load test result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1001
        emitPushArgument(selectAlternativeName, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1002
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1003
        // if_icmpne L_fallback
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1004
        mv.visitJumpInsn(Opcodes.IFEQ, L_fallback);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1005
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1006
        // invoke selectAlternativeName.arguments[1]
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1007
        Class<?>[] preForkClasses = localClasses.clone();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1008
        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
  1009
        emitAstoreInsn(receiver.index());  // store the MH in the receiver slot
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1010
        emitStaticInvoke(invokeBasicName);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1011
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1012
        // goto L_done
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1013
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1014
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1015
        // L_fallback:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1016
        mv.visitLabel(L_fallback);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1017
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1018
        // invoke selectAlternativeName.arguments[2]
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1019
        System.arraycopy(preForkClasses, 0, localClasses, 0, preForkClasses.length);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1020
        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
  1021
        emitAstoreInsn(receiver.index());  // store the MH in the receiver slot
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1022
        emitStaticInvoke(invokeBasicName);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1023
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1024
        // L_done:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1025
        mv.visitLabel(L_done);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1026
        // for now do not bother to merge typestate; just reset to the dominator state
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1027
        System.arraycopy(preForkClasses, 0, localClasses, 0, preForkClasses.length);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1028
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1029
        return invokeBasicName;  // return what's on stack
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1030
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1031
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1032
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1033
      * Emit bytecode for the guardWithCatch idiom.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1034
      *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1035
      * The pattern looks like (Cf. MethodHandleImpl.makeGuardWithCatch):
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1036
      * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1037
      *  guardWithCatch=Lambda(a0:L,a1:L,a2:L,a3:L,a4:L,a5:L,a6:L,a7:L)=>{
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1038
      *    t8:L=MethodHandle.invokeBasic(a4:L,a6:L,a7:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1039
      *    t9:L=MethodHandleImpl.guardWithCatch(a1:L,a2:L,a3:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1040
      *   t10:I=MethodHandle.invokeBasic(a5:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1041
      * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1042
      *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1043
      * It is compiled into bytecode equivalent of the following code:
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1044
      * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1045
      *  try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1046
      *      return a1.invokeBasic(a6, a7);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1047
      *  } catch (Throwable e) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1048
      *      if (!a2.isInstance(e)) throw e;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1049
      *      return a3.invokeBasic(ex, a6, a7);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1050
      *  }}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1051
      */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1052
    private Name emitGuardWithCatch(int pos) {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1053
        Name args    = lambdaForm.names[pos];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1054
        Name invoker = lambdaForm.names[pos+1];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1055
        Name result  = lambdaForm.names[pos+2];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1056
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1057
        Label L_startBlock = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1058
        Label L_endBlock = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1059
        Label L_handler = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1060
        Label L_done = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1061
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
  1062
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
  1063
        MethodType type = args.function.resolvedHandle().type()
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1064
                              .dropParameterTypes(0,1)
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1065
                              .changeReturnType(returnType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1066
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1067
        mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_handler, "java/lang/Throwable");
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1068
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1069
        // Normal case
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1070
        mv.visitLabel(L_startBlock);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1071
        // load target
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1072
        emitPushArgument(invoker, 0);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1073
        emitPushArguments(args, 1); // skip 1st argument: method handle
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1074
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1075
        mv.visitLabel(L_endBlock);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1076
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1077
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1078
        // Exceptional case
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1079
        mv.visitLabel(L_handler);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1080
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1081
        // Check exception's type
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1082
        mv.visitInsn(Opcodes.DUP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1083
        // load exception class
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1084
        emitPushArgument(invoker, 1);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1085
        mv.visitInsn(Opcodes.SWAP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1086
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "isInstance", "(Ljava/lang/Object;)Z", false);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1087
        Label L_rethrow = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1088
        mv.visitJumpInsn(Opcodes.IFEQ, L_rethrow);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1089
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1090
        // Invoke catcher
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1091
        // load catcher
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1092
        emitPushArgument(invoker, 2);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1093
        mv.visitInsn(Opcodes.SWAP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1094
        emitPushArguments(args, 1); // skip 1st argument: method handle
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1095
        MethodType catcherType = type.insertParameterTypes(0, Throwable.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1096
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", catcherType.basicType().toMethodDescriptorString(), false);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1097
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1098
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1099
        mv.visitLabel(L_rethrow);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1100
        mv.visitInsn(Opcodes.ATHROW);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1101
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1102
        mv.visitLabel(L_done);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1103
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1104
        return result;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1105
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1106
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1107
    /**
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1108
     * Emit bytecode for the tryFinally idiom.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1109
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1110
     * The pattern looks like (Cf. MethodHandleImpl.makeTryFinally):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1111
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1112
     * // a0: BMH
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1113
     * // a1: target, a2: cleanup
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1114
     * // a3: box, a4: unbox
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1115
     * // a5 (and following): arguments
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1116
     * tryFinally=Lambda(a0:L,a1:L,a2:L,a3:L,a4:L,a5:L)=>{
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1117
     *   t6:L=MethodHandle.invokeBasic(a3:L,a5:L);         // box the arguments into an Object[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1118
     *   t7:L=MethodHandleImpl.tryFinally(a1:L,a2:L,t6:L); // call the tryFinally executor
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1119
     *   t8:L=MethodHandle.invokeBasic(a4:L,t7:L);t8:L}    // unbox the result; return the result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1120
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1121
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1122
     * It is compiled into bytecode equivalent to the following code:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1123
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1124
     * Throwable t;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1125
     * Object r;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1126
     * try {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1127
     *     r = a1.invokeBasic(a5);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1128
     * } catch (Throwable thrown) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1129
     *     t = thrown;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1130
     *     throw t;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1131
     * } finally {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1132
     *     r = a2.invokeBasic(t, r, a5);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1133
     * }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1134
     * return r;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1135
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1136
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1137
     * Specifically, the bytecode will have the following form (the stack effects are given for the beginnings of
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1138
     * blocks, and for the situations after executing the given instruction - the code will have a slightly different
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1139
     * shape if the return type is {@code void}):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1140
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1141
     * TRY:                 (--)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1142
     *                      load target                             (-- target)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1143
     *                      load args                               (-- args... target)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1144
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (depends)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1145
     * FINALLY_NORMAL:      (-- r)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1146
     *                      load cleanup                            (-- cleanup r)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1147
     *                      SWAP                                    (-- r cleanup)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1148
     *                      ACONST_NULL                             (-- t r cleanup)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1149
     *                      SWAP                                    (-- r t cleanup)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1150
     *                      load args                               (-- args... r t cleanup)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1151
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (-- r)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1152
     *                      GOTO DONE
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1153
     * CATCH:               (-- t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1154
     *                      DUP                                     (-- t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1155
     * FINALLY_EXCEPTIONAL: (-- t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1156
     *                      load cleanup                            (-- cleanup t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1157
     *                      SWAP                                    (-- t cleanup t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1158
     *                      load default for r                      (-- r t cleanup t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1159
     *                      load args                               (-- args... r t cleanup t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1160
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (-- r t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1161
     *                      POP                                     (-- t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1162
     *                      ATHROW
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1163
     * DONE:                (-- r)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1164
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1165
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1166
    private Name emitTryFinally(int pos) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1167
        Name args    = lambdaForm.names[pos];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1168
        Name invoker = lambdaForm.names[pos+1];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1169
        Name result  = lambdaForm.names[pos+2];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1170
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1171
        Label lFrom = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1172
        Label lTo = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1173
        Label lCatch = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1174
        Label lDone = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1175
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1176
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1177
        boolean isNonVoid = returnType != void.class;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1178
        MethodType type = args.function.resolvedHandle().type()
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1179
                .dropParameterTypes(0,1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1180
                .changeReturnType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1181
        MethodType cleanupType = type.insertParameterTypes(0, Throwable.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1182
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1183
            cleanupType = cleanupType.insertParameterTypes(1, returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1184
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1185
        String cleanupDesc = cleanupType.basicType().toMethodDescriptorString();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1186
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1187
        // exception handler table
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1188
        mv.visitTryCatchBlock(lFrom, lTo, lCatch, "java/lang/Throwable");
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1189
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1190
        // TRY:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1191
        mv.visitLabel(lFrom);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1192
        emitPushArgument(invoker, 0); // load target
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1193
        emitPushArguments(args, 1); // load args (skip 0: method handle)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1194
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1195
        mv.visitLabel(lTo);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1196
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1197
        // FINALLY_NORMAL:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1198
        emitPushArgument(invoker, 1); // load cleanup
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1199
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1200
            mv.visitInsn(Opcodes.SWAP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1201
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1202
        mv.visitInsn(Opcodes.ACONST_NULL);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1203
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1204
            mv.visitInsn(Opcodes.SWAP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1205
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1206
        emitPushArguments(args, 1); // load args (skip 0: method handle)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1207
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1208
        mv.visitJumpInsn(Opcodes.GOTO, lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1209
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1210
        // CATCH:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1211
        mv.visitLabel(lCatch);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1212
        mv.visitInsn(Opcodes.DUP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1213
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1214
        // FINALLY_EXCEPTIONAL:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1215
        emitPushArgument(invoker, 1); // load cleanup
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1216
        mv.visitInsn(Opcodes.SWAP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1217
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1218
            emitZero(BasicType.basicType(returnType)); // load default for result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1219
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1220
        emitPushArguments(args, 1); // load args (skip 0: method handle)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1221
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1222
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1223
            mv.visitInsn(Opcodes.POP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1224
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1225
        mv.visitInsn(Opcodes.ATHROW);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1226
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1227
        // DONE:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1228
        mv.visitLabel(lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1229
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1230
        return result;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1231
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1232
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1233
    /**
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1234
     * Emit bytecode for the loop idiom.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1235
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1236
     * The pattern looks like (Cf. MethodHandleImpl.loop):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1237
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1238
     * // a0: BMH
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1239
     * // a1: inits, a2: steps, a3: preds, a4: finis
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1240
     * // a5: box, a6: unbox
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1241
     * // a7 (and following): arguments
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1242
     * loop=Lambda(a0:L,a1:L,a2:L,a3:L,a4:L,a5:L,a6:L,a7:L)=>{
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1243
     *   t8:L=MethodHandle.invokeBasic(a5:L,a7:L);                  // box the arguments into an Object[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1244
     *   t9:L=MethodHandleImpl.loop(bt:L,a1:L,a2:L,a3:L,a4:L,t8:L); // call the loop executor (with supplied types in bt)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1245
     *   t10:L=MethodHandle.invokeBasic(a6:L,t9:L);t10:L}           // unbox the result; return the result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1246
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1247
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1248
     * It is compiled into bytecode equivalent to the code seen in {@link MethodHandleImpl#loop(BasicType[],
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1249
     * MethodHandle[], MethodHandle[], MethodHandle[], MethodHandle[], Object...)}, with the difference that no arrays
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1250
     * will be used for local state storage. Instead, the local state will be mapped to actual stack slots.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1251
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1252
     * Bytecode generation applies an unrolling scheme to enable better bytecode generation regarding local state type
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1253
     * handling. The generated bytecode will have the following form ({@code void} types are ignored for convenience).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1254
     * Assume there are {@code C} clauses in the loop.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1255
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1256
     * INIT: (INIT_SEQ for clause 1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1257
     *       ...
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1258
     *       (INIT_SEQ for clause C)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1259
     * LOOP: (LOOP_SEQ for clause 1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1260
     *       ...
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1261
     *       (LOOP_SEQ for clause C)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1262
     *       GOTO LOOP
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1263
     * DONE: ...
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1264
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1265
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1266
     * The {@code INIT_SEQ_x} sequence for clause {@code x} (with {@code x} ranging from {@code 0} to {@code C-1}) has
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1267
     * the following shape. Assume slot {@code vx} is used to hold the state for clause {@code x}.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1268
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1269
     * INIT_SEQ_x:  ALOAD inits
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1270
     *              CHECKCAST MethodHandle[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1271
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1272
     *              AALOAD      // load the init handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1273
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1274
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1275
     *              store vx
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1276
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1277
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1278
     * The {@code LOOP_SEQ_x} sequence for clause {@code x} (with {@code x} ranging from {@code 0} to {@code C-1}) has
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1279
     * the following shape. Again, assume slot {@code vx} is used to hold the state for clause {@code x}.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1280
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1281
     * LOOP_SEQ_x:  ALOAD steps
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1282
     *              CHECKCAST MethodHandle[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1283
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1284
     *              AALOAD              // load the step handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1285
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1286
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1287
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1288
     *              store vx
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1289
     *              ALOAD preds
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1290
     *              CHECKCAST MethodHandle[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1291
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1292
     *              AALOAD              // load the pred handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1293
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1294
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1295
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1296
     *              IFNE LOOP_SEQ_x+1   // predicate returned false -> jump to next clause
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1297
     *              ALOAD finis
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1298
     *              CHECKCAST MethodHandle[]
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1299
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1300
     *              AALOAD              // load the fini handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1301
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1302
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1303
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1304
     *              GOTO DONE           // jump beyond end of clauses to return from loop
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1305
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1306
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1307
    private Name emitLoop(int pos) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1308
        Name args    = lambdaForm.names[pos];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1309
        Name invoker = lambdaForm.names[pos+1];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1310
        Name result  = lambdaForm.names[pos+2];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1311
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1312
        // extract clause and loop-local state types
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1313
        // find the type info in the loop invocation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1314
        BasicType[] loopClauseTypes = (BasicType[]) invoker.arguments[0];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1315
        Class<?>[] loopLocalStateTypes = Stream.of(loopClauseTypes).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1316
                filter(bt -> bt != BasicType.V_TYPE).map(BasicType::basicTypeClass).toArray(Class<?>[]::new);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1317
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1318
        final int firstLoopStateIndex = extendLocalsMap(loopLocalStateTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1319
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1320
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1321
        MethodType loopType = args.function.resolvedHandle().type()
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1322
                .dropParameterTypes(0,1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1323
                .changeReturnType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1324
        MethodType loopHandleType = loopType.insertParameterTypes(0, loopLocalStateTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1325
        MethodType predType = loopHandleType.changeReturnType(boolean.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1326
        MethodType finiType = loopHandleType;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1327
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1328
        final int nClauses = loopClauseTypes.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1329
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1330
        // indices to invoker arguments to load method handle arrays
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1331
        final int inits = 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1332
        final int steps = 2;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1333
        final int preds = 3;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1334
        final int finis = 4;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1335
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1336
        Label lLoop = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1337
        Label lDone = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1338
        Label lNext;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1339
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1340
        // INIT:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1341
        for (int c = 0, state = 0; c < nClauses; ++c) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1342
            MethodType cInitType = loopType.changeReturnType(loopClauseTypes[c].basicTypeClass());
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1343
            emitLoopHandleInvoke(invoker, inits, c, args, false, cInitType, loopLocalStateTypes, firstLoopStateIndex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1344
            if (cInitType.returnType() != void.class) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1345
                emitStoreInsn(BasicType.basicType(cInitType.returnType()), firstLoopStateIndex + state);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1346
                ++state;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1347
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1348
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1349
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1350
        // LOOP:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1351
        mv.visitLabel(lLoop);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1352
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1353
        for (int c = 0, state = 0; c < nClauses; ++c) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1354
            lNext = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1355
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1356
            MethodType stepType = loopHandleType.changeReturnType(loopClauseTypes[c].basicTypeClass());
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1357
            boolean isVoid = stepType.returnType() == void.class;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1358
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1359
            // invoke loop step
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1360
            emitLoopHandleInvoke(invoker, steps, c, args, true, stepType, loopLocalStateTypes, firstLoopStateIndex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1361
            if (!isVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1362
                emitStoreInsn(BasicType.basicType(stepType.returnType()), firstLoopStateIndex + state);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1363
                ++state;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1364
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1365
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1366
            // invoke loop predicate
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1367
            emitLoopHandleInvoke(invoker, preds, c, args, true, predType, loopLocalStateTypes, firstLoopStateIndex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1368
            mv.visitJumpInsn(Opcodes.IFNE, lNext);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1369
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1370
            // invoke fini
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1371
            emitLoopHandleInvoke(invoker, finis, c, args, true, finiType, loopLocalStateTypes, firstLoopStateIndex);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1372
            mv.visitJumpInsn(Opcodes.GOTO, lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1373
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1374
            // this is the beginning of the next loop clause
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1375
            mv.visitLabel(lNext);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1376
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1377
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1378
        mv.visitJumpInsn(Opcodes.GOTO, lLoop);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1379
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1380
        // DONE:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1381
        mv.visitLabel(lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1382
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1383
        return result;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1384
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1385
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1386
    private int extendLocalsMap(Class<?>[] types) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1387
        int firstSlot = localsMap.length - 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1388
        localsMap = Arrays.copyOf(localsMap, localsMap.length + types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1389
        localClasses = Arrays.copyOf(localClasses, localClasses.length + types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1390
        System.arraycopy(types, 0, localClasses, firstSlot, types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1391
        int index = localsMap[firstSlot - 1] + 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1392
        int lastSlots = 0;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1393
        for (int i = 0; i < types.length; ++i) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1394
            localsMap[firstSlot + i] = index;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1395
            lastSlots = BasicType.basicType(localClasses[firstSlot + i]).basicTypeSlots();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1396
            index += lastSlots;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1397
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1398
        localsMap[localsMap.length - 1] = index - lastSlots;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1399
        return firstSlot;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1400
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1401
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1402
    private void emitLoopHandleInvoke(Name holder, int handles, int clause, Name args, boolean pushLocalState,
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1403
                                      MethodType type, Class<?>[] loopLocalStateTypes, int firstLoopStateSlot) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1404
        // load handle for clause
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1405
        emitPushArgument(holder, handles);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1406
        emitIconstInsn(clause);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1407
        mv.visitInsn(Opcodes.AALOAD);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1408
        // load loop state (preceding the other arguments)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1409
        if (pushLocalState) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1410
            for (int s = 0; s < loopLocalStateTypes.length; ++s) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1411
                emitLoadInsn(BasicType.basicType(loopLocalStateTypes[s]), firstLoopStateSlot + s);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1412
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1413
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1414
        // load loop args (skip 0: method handle)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1415
        emitPushArguments(args, 1);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1416
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.toMethodDescriptorString(), false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1417
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1418
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1419
    private void emitZero(BasicType type) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1420
        switch (type) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1421
            case I_TYPE: mv.visitInsn(Opcodes.ICONST_0); break;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1422
            case J_TYPE: mv.visitInsn(Opcodes.LCONST_0); break;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1423
            case F_TYPE: mv.visitInsn(Opcodes.FCONST_0); break;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1424
            case D_TYPE: mv.visitInsn(Opcodes.DCONST_0); break;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1425
            case L_TYPE: mv.visitInsn(Opcodes.ACONST_NULL); break;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1426
            default: throw new InternalError("unknown type: " + type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1427
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1428
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1429
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1430
    private void emitPushArguments(Name args) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1431
        emitPushArguments(args, 0);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1432
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1433
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1434
    private void emitPushArguments(Name args, int start) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1435
        for (int i = start; i < args.arguments.length; i++) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1436
            emitPushArgument(args, i);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1437
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1438
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1439
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1440
    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
  1441
        Object arg = name.arguments[paramIndex];
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1442
        Class<?> ptype = name.function.methodType().parameterType(paramIndex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1443
        emitPushArgument(ptype, arg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1444
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1445
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1446
    private void emitPushArgument(Class<?> ptype, Object arg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1447
        BasicType bptype = basicType(ptype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1448
        if (arg instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1449
            Name n = (Name) arg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1450
            emitLoadInsn(n.type, n.index());
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1451
            emitImplicitConversion(n.type, ptype, n);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1452
        } else if ((arg == null || arg instanceof String) && bptype == L_TYPE) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1453
            emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1454
        } else {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1455
            if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1456
                emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1457
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1458
                mv.visitLdcInsn(constantPlaceholder(arg));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1459
                emitImplicitConversion(L_TYPE, ptype, arg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1460
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1461
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1462
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1463
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1464
    /**
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1465
     * Store the name to its local, if necessary.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1466
     */
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1467
    private void emitStoreResult(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1468
        if (name != null && name.type != V_TYPE) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1469
            // non-void: actually assign
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1470
            emitStoreInsn(name.type, name.index());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1471
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1472
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1473
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1474
    /**
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1475
     * 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
  1476
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1477
    private void emitReturn(Name onStack) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1478
        // return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1479
        Class<?> rclass = invokerType.returnType();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1480
        BasicType rtype = lambdaForm.returnType();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1481
        assert(rtype == basicType(rclass));  // must agree
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1482
        if (rtype == V_TYPE) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1483
            // void
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1484
            mv.visitInsn(Opcodes.RETURN);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1485
            // it doesn't matter what rclass is; the JVM will discard any value
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1486
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1487
            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
  1488
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1489
            // put return value on the stack if it is not already there
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1490
            if (rn != onStack) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1491
                emitLoadInsn(rtype, lambdaForm.result);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1492
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1493
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1494
            emitImplicitConversion(rtype, rclass, rn);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1495
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1496
            // generate actual return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1497
            emitReturnInsn(rtype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1498
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1499
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1500
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1501
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1502
     * 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
  1503
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1504
    private void emitPrimCast(Wrapper from, Wrapper to) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1505
        // Here's how.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1506
        // -   indicates forbidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1507
        // <-> indicates implicit
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1508
        //      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
  1509
        // from boolean    <->        -        -        -        -        -        -        -
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1510
        //      byte        -       <->       i2s      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1511
        //      short       -       i2b       <->      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1512
        //      char        -       i2b       i2s      <->      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1513
        //      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
  1514
        //      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
  1515
        //      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
  1516
        //      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
  1517
        if (from == to) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1518
            // 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
  1519
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1520
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1521
        if (from.isSubwordOrInt()) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1522
            // 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
  1523
            emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1524
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1525
            // cast from {long,float,double} to anything
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1526
            if (to.isSubwordOrInt()) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1527
                // cast to {byte,short,char,int}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1528
                emitX2I(from);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1529
                if (to.bitWidth() < 32) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1530
                    // 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
  1531
                    emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1532
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1533
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1534
                // 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
  1535
                boolean error = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1536
                switch (from) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1537
                case LONG:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1538
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1539
                    case FLOAT:   mv.visitInsn(Opcodes.L2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1540
                    case DOUBLE:  mv.visitInsn(Opcodes.L2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1541
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1542
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1543
                    break;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1544
                case FLOAT:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1545
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1546
                    case LONG :   mv.visitInsn(Opcodes.F2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1547
                    case DOUBLE:  mv.visitInsn(Opcodes.F2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1548
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1549
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1550
                    break;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1551
                case DOUBLE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1552
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1553
                    case LONG :   mv.visitInsn(Opcodes.D2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1554
                    case FLOAT:   mv.visitInsn(Opcodes.D2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1555
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1556
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1557
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1558
                default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1559
                    error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1560
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1561
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1562
                if (error) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1563
                    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
  1564
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1565
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1566
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1567
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1568
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1569
    private void emitI2X(Wrapper type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1570
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1571
        case BYTE:    mv.visitInsn(Opcodes.I2B);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1572
        case SHORT:   mv.visitInsn(Opcodes.I2S);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1573
        case CHAR:    mv.visitInsn(Opcodes.I2C);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1574
        case INT:     /* naught */                break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1575
        case LONG:    mv.visitInsn(Opcodes.I2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1576
        case FLOAT:   mv.visitInsn(Opcodes.I2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1577
        case DOUBLE:  mv.visitInsn(Opcodes.I2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1578
        case BOOLEAN:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1579
            // For compatibility with ValueConversions and explicitCastArguments:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1580
            mv.visitInsn(Opcodes.ICONST_1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1581
            mv.visitInsn(Opcodes.IAND);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1582
            break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1583
        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
  1584
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1585
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1586
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1587
    private void emitX2I(Wrapper type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1588
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1589
        case LONG:    mv.visitInsn(Opcodes.L2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1590
        case FLOAT:   mv.visitInsn(Opcodes.F2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1591
        case DOUBLE:  mv.visitInsn(Opcodes.D2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1592
        default:      throw new InternalError("unknown type: " + type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1593
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1594
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1595
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1596
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1597
     * 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
  1598
     */
34429
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1599
    static MemberName generateLambdaFormInterpreterEntryPoint(MethodType mt) {
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1600
        assert(isValidSignature(basicTypeSignature(mt)));
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1601
        String name = "interpret_"+basicTypeChar(mt.returnType());
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1602
        MethodType type = mt;  // includes leading argument
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1603
        type = type.changeParameterType(0, MethodHandle.class);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1604
        InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("LFI", name, type);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1605
        return g.loadMethod(g.generateLambdaFormInterpreterEntryPointBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1606
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1607
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1608
    private byte[] generateLambdaFormInterpreterEntryPointBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1609
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1610
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1611
        // Suppress this method in backtraces displayed to the user.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1612
        mv.visitAnnotation(LF_HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1613
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1614
        // Don't inline the interpreter entry.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1615
        mv.visitAnnotation(DONTINLINE_SIG, true);
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1616
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1617
        // create parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1618
        emitIconstInsn(invokerType.parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1619
        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
  1620
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1621
        // fill parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1622
        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
  1623
            Class<?> ptype = invokerType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1624
            mv.visitInsn(Opcodes.DUP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1625
            emitIconstInsn(i);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1626
            emitLoadInsn(basicType(ptype), i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1627
            // box if primitive type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1628
            if (ptype.isPrimitive()) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1629
                emitBoxing(Wrapper.forPrimitiveType(ptype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1630
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1631
            mv.visitInsn(Opcodes.AASTORE);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1632
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1633
        // invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1634
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1635
        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
  1636
        mv.visitInsn(Opcodes.SWAP);  // swap form and array; avoid local variable
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
  1637
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LF, "interpretWithArguments", "([Ljava/lang/Object;)Ljava/lang/Object;", false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1638
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1639
        // maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1640
        Class<?> rtype = invokerType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1641
        if (rtype.isPrimitive() && rtype != void.class) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1642
            emitUnboxing(Wrapper.forPrimitiveType(rtype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1643
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1644
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1645
        // return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1646
        emitReturnInsn(basicType(rtype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1647
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1648
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1649
        bogusMethod(invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1650
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1651
        final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1652
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1653
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1654
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1655
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1656
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1657
     * Generate bytecode for a NamedFunction invoker.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1658
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1659
    static MemberName generateNamedFunctionInvoker(MethodTypeForm typeForm) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1660
        MethodType invokerType = NamedFunction.INVOKER_METHOD_TYPE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1661
        String invokerName = "invoke_" + shortenSignature(basicTypeSignature(typeForm.erasedType()));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1662
        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
  1663
        return g.loadMethod(g.generateNamedFunctionInvokerImpl(typeForm));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1664
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1665
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1666
    private byte[] generateNamedFunctionInvokerImpl(MethodTypeForm typeForm) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1667
        MethodType dstType = typeForm.erasedType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1668
        classFilePrologue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1669
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1670
        // Suppress this method in backtraces displayed to the user.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1671
        mv.visitAnnotation(LF_HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1672
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1673
        // Force inlining of this invoker method.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1674
        mv.visitAnnotation(FORCEINLINE_SIG, true);
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1675
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1676
        // Load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1677
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1678
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1679
        // Load arguments from array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1680
        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
  1681
            emitAloadInsn(1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1682
            emitIconstInsn(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1683
            mv.visitInsn(Opcodes.AALOAD);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1684
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1685
            // Maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1686
            Class<?> dptype = dstType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1687
            if (dptype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1688
                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
  1689
                Wrapper dstWrapper = Wrapper.forBasicType(dptype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1690
                Wrapper srcWrapper = dstWrapper.isSubwordOrInt() ? Wrapper.INT : dstWrapper;  // narrow subword from int
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1691
                emitUnboxing(srcWrapper);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1692
                emitPrimCast(srcWrapper, dstWrapper);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1693
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1694
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1695
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1696
        // Invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1697
        String targetDesc = dstType.basicType().toMethodDescriptorString();
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
  1698
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", targetDesc, false);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1699
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1700
        // Box primitive types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1701
        Class<?> rtype = dstType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1702
        if (rtype != void.class && rtype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1703
            Wrapper srcWrapper = Wrapper.forBasicType(rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1704
            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
  1705
            // boolean casts not allowed
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1706
            emitPrimCast(srcWrapper, dstWrapper);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1707
            emitBoxing(dstWrapper);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1708
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1709
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1710
        // 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
  1711
        if (rtype == void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1712
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1713
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1714
        emitReturnInsn(L_TYPE);  // NOTE: NamedFunction invokers always return a reference value.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1715
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1716
        classFileEpilogue();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1717
        bogusMethod(dstType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1718
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1719
        final byte[] classFile = cw.toByteArray();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1720
        maybeDump(className, classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1721
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1722
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1723
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1724
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1725
     * 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
  1726
     * for debugging purposes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1727
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1728
    private void bogusMethod(Object... os) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1729
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1730
            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
  1731
            for (Object o : os) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1732
                mv.visitLdcInsn(o.toString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1733
                mv.visitInsn(Opcodes.POP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1734
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1735
            mv.visitInsn(Opcodes.RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1736
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1737
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1738
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1739
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1740
}