src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
author jvernee
Thu, 14 Nov 2019 10:55:46 +0100
changeset 59075 355f4f42dda5
parent 58038 aa3715655834
permissions -rw-r--r--
8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type Reviewed-by: redestad, vlivanov, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
     1
/*
58038
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
     2
 * Copyright (c) 2012, 2019, 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;
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    42
import java.util.ArrayList;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    43
import java.util.Arrays;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    44
import java.util.HashMap;
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
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    47
import static java.lang.invoke.LambdaForm.BasicType;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    48
import static java.lang.invoke.LambdaForm.BasicType.*;
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    49
import static java.lang.invoke.LambdaForm.*;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    50
import static java.lang.invoke.MethodHandleNatives.Constants.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    51
import static java.lang.invoke.MethodHandleStatics.*;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    52
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    53
/**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    54
 * Code generation backend for LambdaForm.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    55
 * <p>
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    56
 * @author John Rose, JSR 292 EG
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    57
 */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    58
class InvokerBytecodeGenerator {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    59
    /** Define class names for convenience. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    60
    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
    61
    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
    62
    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
    63
    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
    64
    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
    65
    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
    66
    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
    67
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    68
    private static final String LOOP_CLAUSES = MHI + "$LoopClauses";
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    69
    private static final String MHARY2       = "[[L" + MH + ";";
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
    70
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    71
    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
    72
    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
    73
    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
    74
    private static final String LLV_SIG = "(L" + OBJ + ";L" + OBJ + ";)V";
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
    75
    private static final String CLASS_PREFIX = LF + "$";
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
    76
    private static final String SOURCE_PREFIX = "LambdaForm$";
13423
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 its super class*/
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40211
diff changeset
    79
    static final String INVOKER_SUPER_NAME = OBJ;
13423
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
    /** Name of new class */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    82
    private final String className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    83
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    84
    private final LambdaForm lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    85
    private final String     invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    86
    private final MethodType invokerType;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    87
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
    88
    /** 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
    89
    private int[]       localsMap;    // index
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
    90
    private Class<?>[]  localClasses; // type
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    91
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    92
    /** ASM bytecode generation. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    93
    private ClassWriter cw;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    94
    private MethodVisitor mv;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
    95
49116
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
    96
    /** Single element internal class name lookup cache. */
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
    97
    private Class<?> lastClass;
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
    98
    private String lastInternalName;
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
    99
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   100
    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
   101
    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
   102
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   103
    /** 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
   104
    private InvokerBytecodeGenerator(LambdaForm lambdaForm, int localsMapSize,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   105
                                     String className, String invokerName, MethodType invokerType) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   106
        int p = invokerName.indexOf('.');
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   107
        if (p > -1) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   108
            className = invokerName.substring(0, p);
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   109
            invokerName = invokerName.substring(p + 1);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   110
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   111
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   112
            className = makeDumpableClassName(className);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   113
        }
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   114
        this.className  = className;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   115
        this.lambdaForm = lambdaForm;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   116
        this.invokerName = invokerName;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   117
        this.invokerType = invokerType;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   118
        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
   119
        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
   120
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   121
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   122
    /** 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
   123
    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
   124
        this(null, invokerType.parameterCount(),
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   125
             className, invokerName, invokerType);
58038
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   126
        MethodType mt = invokerType.erase();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   127
        // Create an array to map name indexes to locals indexes.
58038
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   128
        localsMap[0] = 0; // localsMap has at least one element
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   129
        for (int i = 1, index = 0; i < localsMap.length; i++) {
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   130
            Wrapper w = Wrapper.forBasicType(mt.parameterType(i - 1));
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   131
            index += w.stackSlots();
aa3715655834 8230662: Remove dead code from MethodTypeForm
redestad
parents: 53435
diff changeset
   132
            localsMap[i] = index;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   133
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   134
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   135
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   136
    /** For generating customized code for a single LambdaForm. */
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   137
    private InvokerBytecodeGenerator(String className, LambdaForm form, MethodType invokerType) {
43893
0e72a2804a0c 8175233: Remove LambdaForm.debugName
redestad
parents: 42105
diff changeset
   138
        this(className, form.lambdaName(), form, invokerType);
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   139
    }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   140
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   141
    /** For generating customized code for a single LambdaForm. */
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   142
    InvokerBytecodeGenerator(String className, String invokerName,
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   143
            LambdaForm form, MethodType invokerType) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   144
        this(form, form.names.length,
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   145
             className, invokerName, invokerType);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   146
        // 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
   147
        Name[] names = form.names;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   148
        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
   149
            localsMap[i] = index;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   150
            if (i < names.length) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   151
                BasicType type = names[i].type();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   152
                index += type.basicTypeSlots();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   153
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   154
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   155
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   156
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   157
    /** instance counters for dumped classes */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   158
    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
   159
    /** debugging flag for saving generated class files */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   160
    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
   161
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   162
    static {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   163
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   164
            DUMP_CLASS_FILES_COUNTERS = new HashMap<>();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   165
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   166
                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
   167
                if (!dumpDir.exists()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   168
                    dumpDir.mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   169
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   170
                DUMP_CLASS_FILES_DIR = dumpDir;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   171
                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
   172
            } catch (Exception e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   173
                throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   174
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   175
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   176
            DUMP_CLASS_FILES_COUNTERS = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   177
            DUMP_CLASS_FILES_DIR = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   178
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   179
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   180
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   181
    private void maybeDump(final byte[] classFile) {
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   182
        if (DUMP_CLASS_FILES) {
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   183
            maybeDump(CLASS_PREFIX + className, classFile);
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   184
        }
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   185
    }
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   186
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   187
    // Also used from BoundMethodHandle
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   188
    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
   189
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   190
            java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29020
diff changeset
   191
            new java.security.PrivilegedAction<>() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   192
                public Void run() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   193
                    try {
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47216
diff changeset
   194
                        String dumpName = className.replace('.','/');
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   195
                        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
   196
                        System.out.println("dump: " + dumpFile);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   197
                        dumpFile.getParentFile().mkdirs();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   198
                        FileOutputStream file = new FileOutputStream(dumpFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   199
                        file.write(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   200
                        file.close();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   201
                        return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   202
                    } catch (IOException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   203
                        throw newInternalError(ex);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   204
                    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   205
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   206
            });
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   207
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   208
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   209
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   210
    private static String makeDumpableClassName(String className) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   211
        Integer ctr;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   212
        synchronized (DUMP_CLASS_FILES_COUNTERS) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   213
            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
   214
            if (ctr == null)  ctr = 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   215
            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
   216
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   217
        String sfx = ctr.toString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   218
        while (sfx.length() < 3)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   219
            sfx = "0"+sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   220
        className += sfx;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   221
        return className;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   222
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   223
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   224
    class CpPatch {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   225
        final int index;
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   226
        final Object value;
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   227
        CpPatch(int index, Object value) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   228
            this.index = index;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   229
            this.value = value;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   230
        }
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   231
        public String toString() {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   232
            return "CpPatch/index="+index+",value="+value;
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   233
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   234
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   235
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   236
    private final ArrayList<CpPatch> cpPatches = new ArrayList<>();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   237
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   238
    private int cph = 0;  // for counting constant placeholders
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
    String constantPlaceholder(Object arg) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   241
        String cpPlaceholder = "CONSTANT_PLACEHOLDER_" + cph++;
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   242
        if (DUMP_CLASS_FILES) cpPlaceholder += " <<" + debugString(arg) + ">>";
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   243
        // TODO check if arg is already in the constant pool
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   244
        // insert placeholder in CP and remember the patch
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   245
        int index = cw.newConst((Object) cpPlaceholder);
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   246
        cpPatches.add(new CpPatch(index, arg));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   247
        return cpPlaceholder;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   248
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   249
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   250
    Object[] cpPatches(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   251
        int size = getConstantPoolSize(classFile);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   252
        Object[] res = new Object[size];
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   253
        for (CpPatch p : cpPatches) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   254
            if (p.index >= size)
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13425
diff changeset
   255
                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
   256
            res[p.index] = p.value;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   257
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   258
        return res;
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
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   261
    private static String debugString(Object arg) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   262
        if (arg instanceof MethodHandle) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   263
            MethodHandle mh = (MethodHandle) arg;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   264
            MemberName member = mh.internalMemberName();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   265
            if (member != null)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   266
                return member.toString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   267
            return mh.debugString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   268
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   269
        return arg.toString();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   270
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   271
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   272
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   273
     * 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
   274
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   275
     * @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
   276
     * @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
   277
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   278
    private static int getConstantPoolSize(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   279
        // The first few bytes:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   280
        // u4 magic;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   281
        // u2 minor_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   282
        // u2 major_version;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   283
        // u2 constant_pool_count;
13425
a8d96a0eda69 7190416: JSR 292: typo in InvokerBytecodeGenerator.getConstantPoolSize
twisti
parents: 13423
diff changeset
   284
        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
   285
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   286
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   287
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   288
     * 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
   289
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   290
    private MemberName loadMethod(byte[] classFile) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   291
        Class<?> invokerClass = loadAndInitializeInvokerClass(classFile, cpPatches(classFile));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   292
        return resolveInvokerMember(invokerClass, invokerName, invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   293
    }
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
     * 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
   297
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   298
    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
   299
        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
   300
        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
   301
        return invokerClass;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   302
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   303
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   304
    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
   305
        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
   306
        try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   307
            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
   308
        } catch (ReflectiveOperationException e) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   309
            throw newInternalError(e);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   310
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   311
        return member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   312
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   313
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   314
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   315
     * Set up class file generation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   316
     */
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   317
    private ClassWriter classFilePrologue() {
23918
5f40f1d88890 8032686: Issues with method invoke
twisti
parents: 23039
diff changeset
   318
        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
   319
        cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   320
        cw.visit(Opcodes.V1_8, NOT_ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   321
                CLASS_PREFIX + className, null, INVOKER_SUPER_NAME, null);
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   322
        cw.visitSource(SOURCE_PREFIX + className, null);
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   323
        return cw;
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   324
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   325
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   326
    private void methodPrologue() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   327
        String invokerDesc = invokerType.toMethodDescriptorString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   328
        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
   329
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   330
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   331
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   332
     * Tear down class file generation.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   333
     */
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   334
    private void methodEpilogue() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   335
        mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   336
        mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   337
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   338
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   339
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   340
     * Low-level emit helpers.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   341
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   342
    private void emitConst(Object con) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   343
        if (con == null) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   344
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   345
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   346
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   347
        if (con instanceof Integer) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   348
            emitIconstInsn((int) con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   349
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   350
        }
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   351
        if (con instanceof Byte) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   352
            emitIconstInsn((byte)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   353
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   354
        }
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   355
        if (con instanceof Short) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   356
            emitIconstInsn((short)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   357
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   358
        }
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   359
        if (con instanceof Character) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   360
            emitIconstInsn((char)con);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   361
            return;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   362
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   363
        if (con instanceof Long) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   364
            long x = (long) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   365
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   366
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   367
                if (sx >= 0 && sx <= 1) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   368
                    mv.visitInsn(Opcodes.LCONST_0 + (int) sx);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   369
                } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   370
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   371
                    mv.visitInsn(Opcodes.I2L);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   372
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   373
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   374
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   375
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   376
        if (con instanceof Float) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   377
            float x = (float) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   378
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   379
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   380
                if (sx >= 0 && sx <= 2) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   381
                    mv.visitInsn(Opcodes.FCONST_0 + (int) sx);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   382
                } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   383
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   384
                    mv.visitInsn(Opcodes.I2F);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   385
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   386
                return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   387
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   388
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   389
        if (con instanceof Double) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   390
            double x = (double) con;
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   391
            short sx = (short)x;
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   392
            if (x == sx) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   393
                if (sx >= 0 && sx <= 1) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   394
                    mv.visitInsn(Opcodes.DCONST_0 + (int) sx);
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
                    emitIconstInsn((int) x);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   397
                    mv.visitInsn(Opcodes.I2D);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   398
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   399
                return;
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
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   402
        if (con instanceof Boolean) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   403
            emitIconstInsn((boolean) con ? 1 : 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   404
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   405
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   406
        // fall through:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   407
        mv.visitLdcInsn(con);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   408
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   409
34530
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   410
    private void emitIconstInsn(final int cst) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   411
        if (cst >= -1 && cst <= 5) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   412
            mv.visitInsn(Opcodes.ICONST_0 + cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   413
        } else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   414
            mv.visitIntInsn(Opcodes.BIPUSH, cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   415
        } else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   416
            mv.visitIntInsn(Opcodes.SIPUSH, cst);
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   417
        } else {
364376f150cf 8143127: InvokerBytecodeGenerator emitConst should handle Byte, Short, Character
redestad
parents: 34429
diff changeset
   418
            mv.visitLdcInsn(cst);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   419
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   420
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   421
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   422
    /*
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   423
     * 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
   424
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   425
    private void emitLoadInsn(BasicType type, int index) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   426
        int opcode = loadInsnOpcode(type);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   427
        mv.visitVarInsn(opcode, localsMap[index]);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   428
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   429
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   430
    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
   431
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   432
            case I_TYPE: return Opcodes.ILOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   433
            case J_TYPE: return Opcodes.LLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   434
            case F_TYPE: return Opcodes.FLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   435
            case D_TYPE: return Opcodes.DLOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   436
            case L_TYPE: return Opcodes.ALOAD;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   437
            default:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   438
                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
   439
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   440
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   441
    private void emitAloadInsn(int index) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   442
        emitLoadInsn(L_TYPE, index);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   443
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   444
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   445
    private void emitStoreInsn(BasicType type, int index) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   446
        int opcode = storeInsnOpcode(type);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   447
        mv.visitVarInsn(opcode, localsMap[index]);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   448
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   449
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   450
    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
   451
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   452
            case I_TYPE: return Opcodes.ISTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   453
            case J_TYPE: return Opcodes.LSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   454
            case F_TYPE: return Opcodes.FSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   455
            case D_TYPE: return Opcodes.DSTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   456
            case L_TYPE: return Opcodes.ASTORE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   457
            default:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   458
                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
   459
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   460
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   461
    private void emitAstoreInsn(int index) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   462
        emitStoreInsn(L_TYPE, index);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   463
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   464
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   465
    private byte arrayTypeCode(Wrapper elementType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   466
        switch (elementType) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   467
            case BOOLEAN: return Opcodes.T_BOOLEAN;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   468
            case BYTE:    return Opcodes.T_BYTE;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   469
            case CHAR:    return Opcodes.T_CHAR;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   470
            case SHORT:   return Opcodes.T_SHORT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   471
            case INT:     return Opcodes.T_INT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   472
            case LONG:    return Opcodes.T_LONG;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   473
            case FLOAT:   return Opcodes.T_FLOAT;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   474
            case DOUBLE:  return Opcodes.T_DOUBLE;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   475
            case OBJECT:  return 0; // in place of Opcodes.T_OBJECT
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   476
            default:      throw new InternalError();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   477
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   478
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   479
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   480
    private int arrayInsnOpcode(byte tcode, int aaop) throws InternalError {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   481
        assert(aaop == Opcodes.AASTORE || aaop == Opcodes.AALOAD);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   482
        int xas;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   483
        switch (tcode) {
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   484
            case Opcodes.T_BOOLEAN: xas = Opcodes.BASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   485
            case Opcodes.T_BYTE:    xas = Opcodes.BASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   486
            case Opcodes.T_CHAR:    xas = Opcodes.CASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   487
            case Opcodes.T_SHORT:   xas = Opcodes.SASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   488
            case Opcodes.T_INT:     xas = Opcodes.IASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   489
            case Opcodes.T_LONG:    xas = Opcodes.LASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   490
            case Opcodes.T_FLOAT:   xas = Opcodes.FASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   491
            case Opcodes.T_DOUBLE:  xas = Opcodes.DASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   492
            case 0:                 xas = Opcodes.AASTORE; break;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   493
            default:      throw new InternalError();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   494
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   495
        return xas - Opcodes.AASTORE + aaop;
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   496
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   497
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   498
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   499
     * Emit a boxing call.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   500
     *
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   501
     * @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
   502
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   503
    private void emitBoxing(Wrapper wrapper) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   504
        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
   505
        String name  = "valueOf";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   506
        String desc  = "(" + wrapper.basicTypeChar() + ")L" + owner + ";";
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   507
        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
   508
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   509
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   510
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   511
     * 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
   512
     *
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   513
     * @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
   514
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   515
    private void emitUnboxing(Wrapper wrapper) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   516
        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
   517
        String name  = wrapper.primitiveSimpleName() + "Value";
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   518
        String desc  = "()" + wrapper.basicTypeChar();
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   519
        emitReferenceCast(wrapper.wrapperType(), null);
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   520
        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
   521
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   522
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   523
    /**
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   524
     * 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
   525
     * 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
   526
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   527
     * @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
   528
     * @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
   529
     * @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
   530
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   531
    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
   532
        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
   533
        if (pclass == ptype.basicTypeClass() && ptype != L_TYPE)
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   534
            return;   // nothing to do
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   535
        switch (ptype) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   536
            case L_TYPE:
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   537
                if (VerifyType.isNullConversion(Object.class, pclass, false)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   538
                    if (PROFILE_LEVEL > 0)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   539
                        emitReferenceCast(Object.class, arg);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   540
                    return;
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
                emitReferenceCast(pclass, arg);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   543
                return;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   544
            case I_TYPE:
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   545
                if (!VerifyType.isNullConversion(int.class, pclass, false))
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   546
                    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
   547
                return;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   548
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   549
        throw newInternalError("bad implicit conversion: tc="+ptype+": "+pclass);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   550
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   551
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   552
    /** 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
   553
    private boolean assertStaticType(Class<?> cls, Name n) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   554
        int local = n.index();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   555
        Class<?> aclass = localClasses[local];
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   556
        if (aclass != null && (aclass == cls || cls.isAssignableFrom(aclass))) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   557
            return true;  // type info is already present
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   558
        } else if (aclass == null || aclass.isAssignableFrom(cls)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   559
            localClasses[local] = cls;  // type info can be improved
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   560
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   561
        return false;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   562
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   563
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   564
    private void emitReferenceCast(Class<?> cls, Object arg) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   565
        Name writeBack = null;  // local to write back result
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   566
        if (arg instanceof Name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   567
            Name n = (Name) arg;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   568
            if (lambdaForm.useCount(n) > 1) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   569
                // This guy gets used more than once.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   570
                writeBack = n;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   571
                if (assertStaticType(cls, n)) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   572
                    return; // this cast was already performed
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   573
                }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   574
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   575
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   576
        if (isStaticallyNameable(cls)) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   577
            String sig = getInternalName(cls);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   578
            mv.visitTypeInsn(Opcodes.CHECKCAST, sig);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   579
        } else {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   580
            mv.visitLdcInsn(constantPlaceholder(cls));
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   581
            mv.visitTypeInsn(Opcodes.CHECKCAST, CLS);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   582
            mv.visitInsn(Opcodes.SWAP);
31802
cb6542e7d329 8062543: Replace uses of MethodHandleImpl.castReference with Class.cast
mhaupt
parents: 31253
diff changeset
   583
            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
   584
            if (Object[].class.isAssignableFrom(cls))
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   585
                mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   586
            else if (PROFILE_LEVEL > 0)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   587
                mv.visitTypeInsn(Opcodes.CHECKCAST, OBJ);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   588
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   589
        if (writeBack != null) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   590
            mv.visitInsn(Opcodes.DUP);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   591
            emitAstoreInsn(writeBack.index());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   592
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   593
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   594
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   595
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   596
     * 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
   597
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   598
    private void emitReturnInsn(BasicType type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   599
        int opcode;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   600
        switch (type) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   601
        case I_TYPE:  opcode = Opcodes.IRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   602
        case J_TYPE:  opcode = Opcodes.LRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   603
        case F_TYPE:  opcode = Opcodes.FRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   604
        case D_TYPE:  opcode = Opcodes.DRETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   605
        case L_TYPE:  opcode = Opcodes.ARETURN;  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
   606
        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
   607
        default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   608
            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
   609
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   610
        mv.visitInsn(opcode);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   611
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   612
49116
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   613
    private String getInternalName(Class<?> c) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   614
        if (c == Object.class)             return OBJ;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   615
        else if (c == Object[].class)      return OBJARY;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   616
        else if (c == Class.class)         return CLS;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   617
        else if (c == MethodHandle.class)  return MH;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   618
        assert(VerifyAccess.isTypeVisible(c, Object.class)) : c.getName();
49116
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   619
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   620
        if (c == lastClass) {
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   621
            return lastInternalName;
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   622
        }
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   623
        lastClass = c;
ef9ddc426975 8198888: Reduce string allocation churn in InvokerBytecodeGenerator
redestad
parents: 48993
diff changeset
   624
        return lastInternalName = c.getName().replace('.', '/');
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   625
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   626
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   627
    private static MemberName resolveFrom(String name, MethodType type, Class<?> holder) {
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   628
        MemberName member = new MemberName(holder, name, type, REF_invokeStatic);
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   629
        MemberName resolvedMember = MemberName.getFactory().resolveOrNull(REF_invokeStatic, member, holder);
40559
e727ba540ee6 8163371: Enable tracing which JLI classes can be pre-generated
redestad
parents: 40543
diff changeset
   630
        if (TRACE_RESOLVE) {
e727ba540ee6 8163371: Enable tracing which JLI classes can be pre-generated
redestad
parents: 40543
diff changeset
   631
            System.out.println("[LF_RESOLVE] " + holder.getName() + " " + name + " " +
e727ba540ee6 8163371: Enable tracing which JLI classes can be pre-generated
redestad
parents: 40543
diff changeset
   632
                    shortenSignature(basicTypeSignature(type)) + (resolvedMember != null ? " (success)" : " (fail)") );
e727ba540ee6 8163371: Enable tracing which JLI classes can be pre-generated
redestad
parents: 40543
diff changeset
   633
        }
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   634
        return resolvedMember;
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   635
    }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   636
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   637
    private static MemberName lookupPregenerated(LambdaForm form, MethodType invokerType) {
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   638
        if (form.customized != null) {
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   639
            // No pre-generated version for customized LF
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   640
            return null;
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   641
        }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   642
        String name = form.kind.methodName;
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   643
        switch (form.kind) {
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   644
            case BOUND_REINVOKER: {
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents: 47216
diff changeset
   645
                name = name + "_" + BoundMethodHandle.speciesDataFor(form).key();
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   646
                return resolveFrom(name, invokerType, DelegatingMethodHandle.Holder.class);
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   647
            }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   648
            case DELEGATE:                  return resolveFrom(name, invokerType, DelegatingMethodHandle.Holder.class);
40461
e372330c0d8c 8164525: Re-examine zero form link time pre-generation
redestad
parents: 40450
diff changeset
   649
            case ZERO:                      // fall-through
40419
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40408
diff changeset
   650
            case IDENTITY: {
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40408
diff changeset
   651
                name = name + "_" + form.returnType().basicTypeChar();
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40408
diff changeset
   652
                return resolveFrom(name, invokerType, LambdaForm.Holder.class);
20e2e4b25a40 8164451: Generate all zero and identity forms at link time
redestad
parents: 40408
diff changeset
   653
            }
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40542
diff changeset
   654
            case EXACT_INVOKER:             // fall-through
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40542
diff changeset
   655
            case EXACT_LINKER:              // fall-through
49240
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 49206
diff changeset
   656
            case LINK_TO_CALL_SITE:         // fall-through
5290e816c64c 8199471: Enable generation of callSiteForms at link time
redestad
parents: 49206
diff changeset
   657
            case LINK_TO_TARGET_METHOD:     // fall-through
40543
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40542
diff changeset
   658
            case GENERIC_INVOKER:           // fall-through
629f1f599595 8164569: Generate non-customized invoker forms at link time
redestad
parents: 40542
diff changeset
   659
            case GENERIC_LINKER:            return resolveFrom(name, invokerType.basicType(), Invokers.Holder.class);
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 49935
diff changeset
   660
            case GET_REFERENCE:             // fall-through
40542
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   661
            case GET_BOOLEAN:               // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   662
            case GET_BYTE:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   663
            case GET_CHAR:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   664
            case GET_SHORT:                 // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   665
            case GET_INT:                   // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   666
            case GET_LONG:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   667
            case GET_FLOAT:                 // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   668
            case GET_DOUBLE:                // fall-through
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 49935
diff changeset
   669
            case PUT_REFERENCE:             // fall-through
40542
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   670
            case PUT_BOOLEAN:               // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   671
            case PUT_BYTE:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   672
            case PUT_CHAR:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   673
            case PUT_SHORT:                 // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   674
            case PUT_INT:                   // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   675
            case PUT_LONG:                  // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   676
            case PUT_FLOAT:                 // fall-through
e7be26f852fa 8164483: Generate field lambda forms at link time
redestad
parents: 40461
diff changeset
   677
            case PUT_DOUBLE:                // fall-through
49206
e991d0be4cc9 8199453: Enable link-time generation of constructor forms
redestad
parents: 49116
diff changeset
   678
            case DIRECT_NEW_INVOKE_SPECIAL: // fall-through
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   679
            case DIRECT_INVOKE_INTERFACE:   // fall-through
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   680
            case DIRECT_INVOKE_SPECIAL:     // fall-through
49935
2ace90aec488 8200167: Validate more special case invocations
dholmes
parents: 49240
diff changeset
   681
            case DIRECT_INVOKE_SPECIAL_IFC: // fall-through
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   682
            case DIRECT_INVOKE_STATIC:      // fall-through
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   683
            case DIRECT_INVOKE_STATIC_INIT: // fall-through
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   684
            case DIRECT_INVOKE_VIRTUAL:     return resolveFrom(name, invokerType, DirectMethodHandle.Holder.class);
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   685
        }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   686
        return null;
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   687
    }
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   688
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   689
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   690
     * 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
   691
     */
44592
6b028630b652 8178480: Wrong exception being thrown on an invalid MethodType
redestad
parents: 44591
diff changeset
   692
    static MemberName generateCustomizedCode(LambdaForm form, MethodType invokerType) {
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   693
        MemberName pregenerated = lookupPregenerated(form, invokerType);
40408
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   694
        if (pregenerated != null)  return pregenerated; // pre-generated bytecode
cf7e826d4d63 8164044: Generate corresponding simple DelegatingMethodHandles when generating a DirectMethodHandle at link time
redestad
parents: 40272
diff changeset
   695
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   696
        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
   697
        return g.loadMethod(g.generateCustomizedCodeBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   698
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   699
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   700
    /** 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
   701
    private boolean checkActualReceiver() {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   702
        // 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
   703
        mv.visitInsn(Opcodes.DUP);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   704
        mv.visitVarInsn(Opcodes.ALOAD, localsMap[0]);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   705
        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
   706
        return true;
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   707
    }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   708
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   709
    static String className(String cn) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   710
        assert checkClassName(cn): "Class not found: " + cn;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   711
        return cn;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   712
    }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   713
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   714
    static boolean checkClassName(String cn) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   715
        Type tp = Type.getType(cn);
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   716
        // 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
   717
        if (tp.getSort() != Type.OBJECT) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   718
            return false;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   719
        }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   720
        try {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   721
            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
   722
            return true;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   723
        } catch (ClassNotFoundException e) {
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   724
            return false;
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   725
        }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   726
    }
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   727
53435
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   728
    static final String      DONTINLINE_SIG = className("Ljdk/internal/vm/annotation/DontInline;");
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   729
    static final String     FORCEINLINE_SIG = className("Ljdk/internal/vm/annotation/ForceInline;");
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   730
    static final String          HIDDEN_SIG = className("Ljdk/internal/vm/annotation/Hidden;");
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   731
    static final String INJECTEDPROFILE_SIG = className("Ljava/lang/invoke/InjectedProfile;");
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   732
    static final String     LF_COMPILED_SIG = className("Ljava/lang/invoke/LambdaForm$Compiled;");
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   733
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   734
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   735
     * 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
   736
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   737
    private byte[] generateCustomizedCodeBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   738
        classFilePrologue();
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   739
        addMethod();
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   740
        bogusMethod(lambdaForm);
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   741
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   742
        final byte[] classFile = toByteArray();
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
   743
        maybeDump(classFile);
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   744
        return classFile;
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   745
    }
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   746
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40211
diff changeset
   747
    void setClassWriter(ClassWriter cw) {
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   748
        this.cw = cw;
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   749
    }
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   750
40258
e6ba2e5c7a29 8163373: Rewrite GenerateJLIClassesPlugin to avoid reflective calls into java.lang.invoke
redestad
parents: 40211
diff changeset
   751
    void addMethod() {
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   752
        methodPrologue();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   753
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   754
        // Suppress this method in backtraces displayed to the user.
53435
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
   755
        mv.visitAnnotation(HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   756
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   757
        // 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
   758
        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
   759
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   760
        if (lambdaForm.forceInline) {
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   761
            // Force inlining of this invoker method.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   762
            mv.visitAnnotation(FORCEINLINE_SIG, true);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   763
        } else {
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
   764
            mv.visitAnnotation(DONTINLINE_SIG, true);
27295
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   765
        }
8ed31033bf48 8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents: 26637
diff changeset
   766
37334
1169e9593135 8067247: Crash: assert(method_holder->data() == 0 ...) failed: a) MT-unsafe modification of inline cache
jcm
parents: 36016
diff changeset
   767
        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
   768
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   769
        if (lambdaForm.customized != null) {
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   770
            // 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
   771
            // 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
   772
            // 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
   773
            // are compile-time constants for JIT compiler.
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   774
            mv.visitLdcInsn(constantPlaceholder(lambdaForm.customized));
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   775
            mv.visitTypeInsn(Opcodes.CHECKCAST, MH);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   776
            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
   777
            mv.visitVarInsn(Opcodes.ASTORE, localsMap[0]);
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   778
        }
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 29019
diff changeset
   779
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   780
        // 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
   781
        // 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
   782
        Name onStack = null;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   783
        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
   784
            Name name = lambdaForm.names[i];
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   785
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   786
            emitStoreResult(onStack);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   787
            onStack = name;  // unless otherwise modified below
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   788
            MethodHandleImpl.Intrinsic intr = name.function.intrinsicName();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   789
            switch (intr) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   790
                case SELECT_ALTERNATIVE:
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   791
                    assert lambdaForm.isSelectAlternative(i);
31253
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   792
                    if (PROFILE_GWT) {
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   793
                        assert(name.arguments[0] instanceof Name &&
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   794
                                ((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
   795
                        mv.visitAnnotation(INJECTEDPROFILE_SIG, true);
31253
5efb78c8a07d 8074551: GWT can be marked non-compilable due to deopt count pollution
vlivanov
parents: 30365
diff changeset
   796
                    }
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   797
                    onStack = emitSelectAlternative(name, lambdaForm.names[i+1]);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   798
                    i++;  // skip MH.invokeBasic of the selectAlternative result
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   799
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   800
                case GUARD_WITH_CATCH:
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   801
                    assert lambdaForm.isGuardWithCatch(i);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   802
                    onStack = emitGuardWithCatch(i);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   803
                    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
   804
                    continue;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   805
                case TRY_FINALLY:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   806
                    assert lambdaForm.isTryFinally(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   807
                    onStack = emitTryFinally(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   808
                    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
   809
                    continue;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   810
                case LOOP:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   811
                    assert lambdaForm.isLoop(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   812
                    onStack = emitLoop(i);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   813
                    i += 2; // jump to the end of the LOOP idiom
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   814
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   815
                case NEW_ARRAY:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   816
                    Class<?> rtype = name.function.methodType().returnType();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   817
                    if (isStaticallyNameable(rtype)) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   818
                        emitNewArray(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   819
                        continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   820
                    }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   821
                    break;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   822
                case ARRAY_LOAD:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   823
                    emitArrayLoad(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   824
                    continue;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   825
                case ARRAY_STORE:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   826
                    emitArrayStore(name);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   827
                    continue;
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   828
                case ARRAY_LENGTH:
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   829
                    emitArrayLength(name);
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   830
                    continue;
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   831
                case IDENTITY:
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   832
                    assert(name.arguments.length == 1);
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   833
                    emitPushArguments(name, 0);
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26473
diff changeset
   834
                    continue;
26476
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   835
                case ZERO:
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   836
                    assert(name.arguments.length == 0);
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   837
                    emitConst(name.type.basicTypeWrapper().zero());
2b814465aaa3 8050887: Intrinsify constants for default values
vlivanov
parents: 26475
diff changeset
   838
                    continue;
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   839
                case NONE:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   840
                    // no intrinsic associated
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   841
                    break;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   842
                default:
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   843
                    throw newInternalError("Unknown intrinsic: "+intr);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   844
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   845
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   846
            MemberName member = name.function.member();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   847
            if (isStaticallyInvocable(member)) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
   848
                emitStaticInvoke(member, name);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   849
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   850
                emitInvoke(name);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   851
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   852
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   853
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   854
        // return statement
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   855
        emitReturn(onStack);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   856
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   857
        methodEpilogue();
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   858
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   859
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   860
    /*
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   861
     * @throws BytecodeGenerationException if something goes wrong when
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   862
     *         generating the byte code
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   863
     */
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   864
    private byte[] toByteArray() {
39483
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   865
        try {
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
   866
            return cw.toByteArray();
39483
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   867
        } catch (RuntimeException e) {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   868
            throw new BytecodeGenerationException(e);
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   869
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   870
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   871
39483
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   872
    @SuppressWarnings("serial")
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   873
    static final class BytecodeGenerationException extends RuntimeException {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   874
        BytecodeGenerationException(Exception cause) {
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   875
            super(cause);
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   876
        }
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   877
    }
3a7e016ba204 8160717: MethodHandles.loop() does not check for excessive signature
mhaupt
parents: 39342
diff changeset
   878
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   879
    void emitArrayLoad(Name name)   { emitArrayOp(name, Opcodes.AALOAD);      }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   880
    void emitArrayStore(Name name)  { emitArrayOp(name, Opcodes.AASTORE);     }
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   881
    void emitArrayLength(Name name) { emitArrayOp(name, Opcodes.ARRAYLENGTH); }
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   882
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   883
    void emitArrayOp(Name name, int arrayOpcode) {
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   884
        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
   885
        Class<?> elementType = name.function.methodType().parameterType(0).getComponentType();
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   886
        assert elementType != null;
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   887
        emitPushArguments(name, 0);
38376
08549412f0dc 8156915: introduce MethodHandle factory for array length
mhaupt
parents: 37334
diff changeset
   888
        if (arrayOpcode != Opcodes.ARRAYLENGTH && elementType.isPrimitive()) {
26465
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   889
            Wrapper w = Wrapper.forPrimitiveType(elementType);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   890
            arrayOpcode = arrayInsnOpcode(arrayTypeCode(w), arrayOpcode);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   891
        }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   892
        mv.visitInsn(arrayOpcode);
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   893
    }
5ff735dd0d52 8038261: JSR292: cache and reuse typed array accessors
vlivanov
parents: 26464
diff changeset
   894
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   895
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   896
     * 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
   897
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   898
    void emitInvoke(Name name) {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
   899
        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
   900
        if (true) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   901
            // push receiver
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   902
            MethodHandle target = name.function.resolvedHandle();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   903
            assert(target != null) : name.exprString();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   904
            mv.visitLdcInsn(constantPlaceholder(target));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   905
            emitReferenceCast(MethodHandle.class, target);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   906
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   907
            // load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   908
            emitAloadInsn(0);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   909
            emitReferenceCast(MethodHandle.class, null);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   910
            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
   911
            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
   912
            // TODO more to come
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   913
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   914
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   915
        // push arguments
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
   916
        emitPushArguments(name, 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   917
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   918
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   919
        MethodType type = name.function.methodType();
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
   920
        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
   921
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   922
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31802
diff changeset
   923
    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
   924
        // 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
   925
        java.lang.Object.class,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   926
        java.util.Arrays.class,
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32649
diff changeset
   927
        jdk.internal.misc.Unsafe.class
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   928
        //MethodHandle.class already covered
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   929
    };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   930
44590
15a77e5b7612 8178384: Reduce work in java.lang.invoke initializers
redestad
parents: 43893
diff changeset
   931
    static boolean isStaticallyInvocable(NamedFunction ... functions) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   932
        for (NamedFunction nf : functions) {
33846
049daf20246e 8143142: AssertionError in MethodHandleImpl
redestad
parents: 33841
diff changeset
   933
            if (!isStaticallyInvocable(nf.member())) {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   934
                return false;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   935
            }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   936
        }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   937
        return true;
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   938
    }
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
   939
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   940
    static boolean isStaticallyInvocable(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   941
        return isStaticallyInvocable(name.function.member());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   942
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   943
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   944
    static boolean isStaticallyInvocable(MemberName member) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   945
        if (member == null)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   946
        if (member.isConstructor())  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   947
        Class<?> cls = member.getDeclaringClass();
48993
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   948
        // Fast-path non-private members declared by MethodHandles, which is a common
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   949
        // case
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   950
        if (MethodHandle.class.isAssignableFrom(cls) && !member.isPrivate()) {
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   951
            assert(isStaticallyInvocableType(member.getMethodOrFieldType()));
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   952
            return true;
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   953
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   954
        if (cls.isArray() || cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   955
            return false;  // FIXME
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   956
        if (cls.isAnonymousClass() || cls.isLocalClass())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   957
            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
   958
        if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   959
            return false;  // not on BCP
24974
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   960
        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
   961
            return false;
48993
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   962
        if (!isStaticallyInvocableType(member.getMethodOrFieldType()))
19816
4136f2a0645d 8024283: 10 nashorn tests fail with similar stack trace InternalError with cause being NoClassDefFoundError
rfield
parents: 14852
diff changeset
   963
            return false;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   964
        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
   965
            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
   966
        if (member.isPublic() && isStaticallyNameable(cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   967
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   968
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   969
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   970
48993
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   971
    private static boolean isStaticallyInvocableType(MethodType mtype) {
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   972
        if (!isStaticallyNameable(mtype.returnType()))
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   973
            return false;
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   974
        for (Class<?> ptype : mtype.parameterArray())
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   975
            if (!isStaticallyNameable(ptype))
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   976
                return false;
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   977
        return true;
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   978
    }
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   979
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   980
    static boolean isStaticallyNameable(Class<?> cls) {
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   981
        if (cls == Object.class)
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
   982
            return true;
48993
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   983
        if (MethodHandle.class.isAssignableFrom(cls)) {
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   984
            assert(!ReflectUtil.isVMAnonymousClass(cls));
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   985
            return true;
aef762ff9b23 8198755: Reduce cost of InvokerBytecodeGenerator::isStaticallyInvocable/-Nameable
redestad
parents: 47753
diff changeset
   986
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   987
        while (cls.isArray())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   988
            cls = cls.getComponentType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   989
        if (cls.isPrimitive())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   990
            return true;  // int[].class, for example
24974
e4a51c336a52 8046903: VM anonymous class members can't be statically invocable
vlivanov
parents: 24685
diff changeset
   991
        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
   992
            return false;
20872
8e486b70dff8 8022718: Runtime accessibility checking: protected class, if extended, should be accessible from another package
drchase
parents: 20535
diff changeset
   993
        // 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
   994
        if (cls.getClassLoader() != Object.class.getClassLoader())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   995
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   996
        if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   997
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   998
        if (!Modifier.isPublic(cls.getModifiers()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
   999
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1000
        for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1001
            if (VerifyAccess.isSamePackage(pkgcls, cls))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1002
                return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1003
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1004
        return false;
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
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1007
    void emitStaticInvoke(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1008
        emitStaticInvoke(name.function.member(), name);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1009
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1010
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
     * 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
  1013
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1014
    void emitStaticInvoke(MemberName member, Name name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1015
        assert(member.equals(name.function.member()));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1016
        Class<?> defc = member.getDeclaringClass();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1017
        String cname = getInternalName(defc);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1018
        String mname = member.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1019
        String mtype;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1020
        byte refKind = member.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1021
        if (refKind == REF_invokeSpecial) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1022
            // 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
  1023
            assert(member.canBeStaticallyBound()) : member;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1024
            refKind = REF_invokeVirtual;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1025
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1026
30343
bf75edd66df0 8033465: JSR292: InvokerBytecodeGenerator: convert a check for REF_invokeVirtual on an interface into an assert
mhaupt
parents: 29020
diff changeset
  1027
        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
  1028
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1029
        // push arguments
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
  1030
        emitPushArguments(name, 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1031
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1032
        // invocation
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1033
        if (member.isMethod()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1034
            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
  1035
            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
  1036
                               member.getDeclaringClass().isInterface());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1037
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1038
            mtype = MethodType.toFieldDescriptorString(member.getFieldType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1039
            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
  1040
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1041
        // 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
  1042
        if (name.type == L_TYPE) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1043
            Class<?> rtype = member.getInvocationType().returnType();
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1044
            assert(!rtype.isPrimitive());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1045
            if (rtype != Object.class && !rtype.isInterface()) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1046
                assertStaticType(rtype, name);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1047
            }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1048
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1049
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1050
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1051
    void emitNewArray(Name name) throws InternalError {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1052
        Class<?> rtype = name.function.methodType().returnType();
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1053
        if (name.arguments.length == 0) {
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1054
            // The array will be a constant.
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1055
            Object emptyArray;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1056
            try {
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
  1057
                emptyArray = name.function.resolvedHandle().invoke();
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1058
            } catch (Throwable ex) {
42105
d04ede5f10e7 8163553: java.lang.LinkageError from test java/lang/ThreadGroup/Stop.java
psandoz
parents: 41125
diff changeset
  1059
                throw uncaughtException(ex);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1060
            }
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1061
            assert(java.lang.reflect.Array.getLength(emptyArray) == 0);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1062
            assert(emptyArray.getClass() == rtype);  // exact typing
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1063
            mv.visitLdcInsn(constantPlaceholder(emptyArray));
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1064
            emitReferenceCast(rtype, emptyArray);
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1065
            return;
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1066
        }
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1067
        Class<?> arrayElementType = rtype.getComponentType();
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1068
        assert(arrayElementType != null);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1069
        emitIconstInsn(name.arguments.length);
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1070
        int xas = Opcodes.AASTORE;
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1071
        if (!arrayElementType.isPrimitive()) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1072
            mv.visitTypeInsn(Opcodes.ANEWARRAY, getInternalName(arrayElementType));
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1073
        } else {
26473
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1074
            byte tc = arrayTypeCode(Wrapper.forPrimitiveType(arrayElementType));
0abc9399a1a6 8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents: 26466
diff changeset
  1075
            xas = arrayInsnOpcode(tc, xas);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1076
            mv.visitIntInsn(Opcodes.NEWARRAY, tc);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1077
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1078
        // store arguments
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1079
        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
  1080
            mv.visitInsn(Opcodes.DUP);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1081
            emitIconstInsn(i);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1082
            emitPushArgument(name, i);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1083
            mv.visitInsn(xas);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1084
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1085
        // the array is left on the stack
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1086
        assertStaticType(rtype, name);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1087
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1088
    int refKindOpcode(byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1089
        switch (refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1090
        case REF_invokeVirtual:      return Opcodes.INVOKEVIRTUAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1091
        case REF_invokeStatic:       return Opcodes.INVOKESTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1092
        case REF_invokeSpecial:      return Opcodes.INVOKESPECIAL;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1093
        case REF_invokeInterface:    return Opcodes.INVOKEINTERFACE;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1094
        case REF_getField:           return Opcodes.GETFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1095
        case REF_putField:           return Opcodes.PUTFIELD;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1096
        case REF_getStatic:          return Opcodes.GETSTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1097
        case REF_putStatic:          return Opcodes.PUTSTATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1098
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1099
        throw new InternalError("refKind="+refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1100
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1101
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1102
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1103
     * Emit bytecode for the selectAlternative idiom.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1104
     *
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1105
     * The pattern looks like (Cf. MethodHandleImpl.makeGuardWithTest):
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 19816
diff changeset
  1106
     * <blockquote><pre>{@code
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1107
     *   Lambda(a0:L,a1:I)=>{
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1108
     *     t2:I=foo.test(a1:I);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1109
     *     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
  1110
     *     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
  1111
     * }</pre></blockquote>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1112
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1113
    private Name emitSelectAlternative(Name selectAlternativeName, Name invokeBasicName) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1114
        assert isStaticallyInvocable(invokeBasicName);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1115
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1116
        Name receiver = (Name) invokeBasicName.arguments[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1117
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1118
        Label L_fallback = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1119
        Label L_done     = new Label();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1120
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1121
        // load test result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1122
        emitPushArgument(selectAlternativeName, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1123
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1124
        // if_icmpne L_fallback
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1125
        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
  1126
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1127
        // invoke selectAlternativeName.arguments[1]
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1128
        Class<?>[] preForkClasses = localClasses.clone();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1129
        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
  1130
        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
  1131
        emitStaticInvoke(invokeBasicName);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1132
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1133
        // goto L_done
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1134
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1135
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1136
        // L_fallback:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1137
        mv.visitLabel(L_fallback);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1138
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1139
        // invoke selectAlternativeName.arguments[2]
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1140
        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
  1141
        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
  1142
        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
  1143
        emitStaticInvoke(invokeBasicName);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1144
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1145
        // L_done:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1146
        mv.visitLabel(L_done);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1147
        // 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
  1148
        System.arraycopy(preForkClasses, 0, localClasses, 0, preForkClasses.length);
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1149
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1150
        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
  1151
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1152
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1153
    /**
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1154
      * Emit bytecode for the guardWithCatch idiom.
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1155
      *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1156
      * The pattern looks like (Cf. MethodHandleImpl.makeGuardWithCatch):
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1157
      * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1158
      *  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
  1159
      *    t8:L=MethodHandle.invokeBasic(a4:L,a6:L,a7:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1160
      *    t9:L=MethodHandleImpl.guardWithCatch(a1:L,a2:L,a3:L,t8:L);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1161
      *   t10:I=MethodHandle.invokeBasic(a5:L,t9:L);t10:I}
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1162
      * }</pre></blockquote>
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1163
      *
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1164
      * It is compiled into bytecode equivalent of the following code:
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1165
      * <blockquote><pre>{@code
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1166
      *  try {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1167
      *      return a1.invokeBasic(a6, a7);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1168
      *  } catch (Throwable e) {
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1169
      *      if (!a2.isInstance(e)) throw e;
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1170
      *      return a3.invokeBasic(ex, a6, a7);
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1171
      *  }}</pre></blockquote>
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1172
      */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1173
    private Name emitGuardWithCatch(int pos) {
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1174
        Name args    = lambdaForm.names[pos];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1175
        Name invoker = lambdaForm.names[pos+1];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1176
        Name result  = lambdaForm.names[pos+2];
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1177
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1178
        Label L_startBlock = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1179
        Label L_endBlock = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1180
        Label L_handler = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1181
        Label L_done = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1182
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
  1183
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 33674
diff changeset
  1184
        MethodType type = args.function.resolvedHandle().type()
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1185
                              .dropParameterTypes(0,1)
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1186
                              .changeReturnType(returnType);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1187
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1188
        mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_handler, "java/lang/Throwable");
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1189
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1190
        // Normal case
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1191
        mv.visitLabel(L_startBlock);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1192
        // load target
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1193
        emitPushArgument(invoker, 0);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1194
        emitPushArguments(args, 1); // skip 1st argument: method handle
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1195
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1196
        mv.visitLabel(L_endBlock);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1197
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1198
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1199
        // Exceptional case
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1200
        mv.visitLabel(L_handler);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1201
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1202
        // Check exception's type
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1203
        mv.visitInsn(Opcodes.DUP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1204
        // load exception class
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1205
        emitPushArgument(invoker, 1);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1206
        mv.visitInsn(Opcodes.SWAP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1207
        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
  1208
        Label L_rethrow = new Label();
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1209
        mv.visitJumpInsn(Opcodes.IFEQ, L_rethrow);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1210
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1211
        // Invoke catcher
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1212
        // load catcher
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1213
        emitPushArgument(invoker, 2);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1214
        mv.visitInsn(Opcodes.SWAP);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1215
        emitPushArguments(args, 1); // skip 1st argument: method handle
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1216
        MethodType catcherType = type.insertParameterTypes(0, Throwable.class);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1217
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", catcherType.basicType().toMethodDescriptorString(), false);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1218
        mv.visitJumpInsn(Opcodes.GOTO, L_done);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1219
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1220
        mv.visitLabel(L_rethrow);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1221
        mv.visitInsn(Opcodes.ATHROW);
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1222
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1223
        mv.visitLabel(L_done);
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1224
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1225
        return result;
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1226
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1227
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1228
    /**
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1229
     * Emit bytecode for the tryFinally idiom.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1230
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1231
     * The pattern looks like (Cf. MethodHandleImpl.makeTryFinally):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1232
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1233
     * // a0: BMH
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1234
     * // a1: target, a2: cleanup
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1235
     * // a3: box, a4: unbox
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1236
     * // a5 (and following): arguments
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1237
     * 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
  1238
     *   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
  1239
     *   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
  1240
     *   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
  1241
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1242
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1243
     * 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
  1244
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1245
     * Throwable t;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1246
     * Object r;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1247
     * try {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1248
     *     r = a1.invokeBasic(a5);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1249
     * } catch (Throwable thrown) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1250
     *     t = thrown;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1251
     *     throw t;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1252
     * } finally {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1253
     *     r = a2.invokeBasic(t, r, a5);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1254
     * }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1255
     * return r;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1256
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1257
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1258
     * 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
  1259
     * 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
  1260
     * shape if the return type is {@code void}):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1261
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1262
     * TRY:                 (--)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1263
     *                      load target                             (-- target)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1264
     *                      load args                               (-- args... target)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1265
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (depends)
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1266
     * FINALLY_NORMAL:      (-- r_2nd* r)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1267
     *                      store returned value                    (--)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1268
     *                      load cleanup                            (-- cleanup)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1269
     *                      ACONST_NULL                             (-- t cleanup)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1270
     *                      load returned value                     (-- r_2nd* r t cleanup)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1271
     *                      load args                               (-- args... r_2nd* r t cleanup)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1272
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (-- r_2nd* r)
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1273
     *                      GOTO DONE
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1274
     * CATCH:               (-- t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1275
     *                      DUP                                     (-- t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1276
     * FINALLY_EXCEPTIONAL: (-- t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1277
     *                      load cleanup                            (-- cleanup t t)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1278
     *                      SWAP                                    (-- t cleanup t)
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1279
     *                      load default for r                      (-- r_2nd* r t cleanup t)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1280
     *                      load args                               (-- args... r_2nd* r t cleanup t)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1281
     *                      INVOKEVIRTUAL MethodHandle.invokeBasic  (-- r_2nd* r t)
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1282
     *                      POP/POP2*                               (-- t)
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1283
     *                      ATHROW
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1284
     * DONE:                (-- r)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1285
     * }</pre></blockquote>
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1286
     * * = depends on whether the return type takes up 2 stack slots.
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1287
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1288
    private Name emitTryFinally(int pos) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1289
        Name args    = lambdaForm.names[pos];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1290
        Name invoker = lambdaForm.names[pos+1];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1291
        Name result  = lambdaForm.names[pos+2];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1292
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1293
        Label lFrom = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1294
        Label lTo = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1295
        Label lCatch = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1296
        Label lDone = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1297
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1298
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1299
        BasicType basicReturnType = BasicType.basicType(returnType);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1300
        boolean isNonVoid = returnType != void.class;
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1301
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1302
        MethodType type = args.function.resolvedHandle().type()
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1303
                .dropParameterTypes(0,1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1304
                .changeReturnType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1305
        MethodType cleanupType = type.insertParameterTypes(0, Throwable.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1306
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1307
            cleanupType = cleanupType.insertParameterTypes(1, returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1308
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1309
        String cleanupDesc = cleanupType.basicType().toMethodDescriptorString();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1310
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1311
        // exception handler table
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1312
        mv.visitTryCatchBlock(lFrom, lTo, lCatch, "java/lang/Throwable");
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1313
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1314
        // TRY:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1315
        mv.visitLabel(lFrom);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1316
        emitPushArgument(invoker, 0); // load target
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1317
        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
  1318
        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
  1319
        mv.visitLabel(lTo);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1320
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1321
        // FINALLY_NORMAL:
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1322
        int index = extendLocalsMap(new Class<?>[]{ returnType });
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1323
        if (isNonVoid) {
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1324
            emitStoreInsn(basicReturnType, index);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1325
        }
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1326
        emitPushArgument(invoker, 1); // load cleanup
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1327
        mv.visitInsn(Opcodes.ACONST_NULL);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1328
        if (isNonVoid) {
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1329
            emitLoadInsn(basicReturnType, index);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1330
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1331
        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
  1332
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1333
        mv.visitJumpInsn(Opcodes.GOTO, lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1334
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1335
        // CATCH:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1336
        mv.visitLabel(lCatch);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1337
        mv.visitInsn(Opcodes.DUP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1338
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1339
        // FINALLY_EXCEPTIONAL:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1340
        emitPushArgument(invoker, 1); // load cleanup
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1341
        mv.visitInsn(Opcodes.SWAP);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1342
        if (isNonVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1343
            emitZero(BasicType.basicType(returnType)); // load default for result
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1344
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1345
        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
  1346
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1347
        if (isNonVoid) {
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1348
            emitPopInsn(basicReturnType);
39342
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
        mv.visitInsn(Opcodes.ATHROW);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1351
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1352
        // DONE:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1353
        mv.visitLabel(lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1354
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1355
        return result;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1356
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1357
59075
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1358
    private void emitPopInsn(BasicType type) {
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1359
        mv.visitInsn(popInsnOpcode(type));
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1360
    }
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1361
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1362
    private static int popInsnOpcode(BasicType type) {
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1363
        switch (type) {
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1364
            case I_TYPE:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1365
            case F_TYPE:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1366
            case L_TYPE:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1367
                return Opcodes.POP;
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1368
            case J_TYPE:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1369
            case D_TYPE:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1370
                return Opcodes.POP2;
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1371
            default:
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1372
                throw new InternalError("unknown type: " + type);
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1373
        }
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1374
    }
355f4f42dda5 8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type
jvernee
parents: 58038
diff changeset
  1375
39342
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
     * Emit bytecode for the loop idiom.
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1378
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1379
     * The pattern looks like (Cf. MethodHandleImpl.loop):
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1380
     * <blockquote><pre>{@code
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1381
     * // a0: BMH
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1382
     * // a1: LoopClauses (containing an array of arrays: inits, steps, preds, finis)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1383
     * // a2: box, a3: unbox
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1384
     * // a4 (and following): arguments
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1385
     * loop=Lambda(a0:L,a1:L,a2:L,a3:L,a4:L)=>{
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1386
     *   t5:L=MethodHandle.invokeBasic(a2:L,a4:L);          // box the arguments into an Object[]
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1387
     *   t6:L=MethodHandleImpl.loop(bt:L,a1:L,t5:L);        // call the loop executor (with supplied types in bt)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1388
     *   t7:L=MethodHandle.invokeBasic(a3:L,t6:L);t7:L}     // unbox the result; return the result
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1389
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1390
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1391
     * It is compiled into bytecode equivalent to the code seen in {@link MethodHandleImpl#loop(BasicType[],
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1392
     * MethodHandleImpl.LoopClauses, Object...)}, with the difference that no arrays
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1393
     * 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
  1394
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1395
     * 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
  1396
     * 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
  1397
     * 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
  1398
     * <blockquote><pre>{@code
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1399
     * PREINIT: ALOAD_1
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1400
     *          CHECKCAST LoopClauses
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1401
     *          GETFIELD LoopClauses.clauses
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1402
     *          ASTORE clauseDataIndex          // place the clauses 2-dimensional array on the stack
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1403
     * INIT:    (INIT_SEQ for clause 1)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1404
     *          ...
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1405
     *          (INIT_SEQ for clause C)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1406
     * LOOP:    (LOOP_SEQ for clause 1)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1407
     *          ...
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1408
     *          (LOOP_SEQ for clause C)
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1409
     *          GOTO LOOP
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1410
     * DONE:    ...
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1411
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1412
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1413
     * 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
  1414
     * 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
  1415
     * <blockquote><pre>{@code
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1416
     * INIT_SEQ_x:  ALOAD clauseDataIndex
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1417
     *              ICONST_0
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1418
     *              AALOAD      // load the inits array
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1419
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1420
     *              AALOAD      // load the init handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1421
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1422
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1423
     *              store vx
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1424
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1425
     * <p>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1426
     * 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
  1427
     * 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
  1428
     * <blockquote><pre>{@code
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1429
     * LOOP_SEQ_x:  ALOAD clauseDataIndex
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1430
     *              ICONST_1
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1431
     *              AALOAD              // load the steps array
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1432
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1433
     *              AALOAD              // load the step handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1434
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1435
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1436
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1437
     *              store vx
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1438
     *              ALOAD clauseDataIndex
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1439
     *              ICONST_2
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1440
     *              AALOAD              // load the preds array
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1441
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1442
     *              AALOAD              // load the pred handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1443
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1444
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1445
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1446
     *              IFNE LOOP_SEQ_x+1   // predicate returned false -> jump to next clause
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1447
     *              ALOAD clauseDataIndex
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1448
     *              ICONST_3
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1449
     *              AALOAD              // load the finis array
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1450
     *              ICONST x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1451
     *              AALOAD              // load the fini handle for clause x
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1452
     *              load locals
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1453
     *              load args
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1454
     *              INVOKEVIRTUAL MethodHandle.invokeBasic
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1455
     *              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
  1456
     * }</pre></blockquote>
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1457
     */
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1458
    private Name emitLoop(int pos) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1459
        Name args    = lambdaForm.names[pos];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1460
        Name invoker = lambdaForm.names[pos+1];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1461
        Name result  = lambdaForm.names[pos+2];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1462
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1463
        // extract clause and loop-local state types
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1464
        // find the type info in the loop invocation
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1465
        BasicType[] loopClauseTypes = (BasicType[]) invoker.arguments[0];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1466
        Class<?>[] loopLocalStateTypes = Stream.of(loopClauseTypes).
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1467
                filter(bt -> bt != BasicType.V_TYPE).map(BasicType::basicTypeClass).toArray(Class<?>[]::new);
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1468
        Class<?>[] localTypes = new Class<?>[loopLocalStateTypes.length + 1];
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1469
        localTypes[0] = MethodHandleImpl.LoopClauses.class;
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1470
        System.arraycopy(loopLocalStateTypes, 0, localTypes, 1, loopLocalStateTypes.length);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1471
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1472
        final int clauseDataIndex = extendLocalsMap(localTypes);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1473
        final int firstLoopStateIndex = clauseDataIndex + 1;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1474
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1475
        Class<?> returnType = result.function.resolvedHandle().type().returnType();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1476
        MethodType loopType = args.function.resolvedHandle().type()
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1477
                .dropParameterTypes(0,1)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1478
                .changeReturnType(returnType);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1479
        MethodType loopHandleType = loopType.insertParameterTypes(0, loopLocalStateTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1480
        MethodType predType = loopHandleType.changeReturnType(boolean.class);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1481
        MethodType finiType = loopHandleType;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1482
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1483
        final int nClauses = loopClauseTypes.length;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1484
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1485
        // 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
  1486
        final int inits = 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1487
        final int steps = 2;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1488
        final int preds = 3;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1489
        final int finis = 4;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1490
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1491
        Label lLoop = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1492
        Label lDone = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1493
        Label lNext;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1494
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1495
        // PREINIT:
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1496
        emitPushArgument(MethodHandleImpl.LoopClauses.class, invoker.arguments[1]);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1497
        mv.visitFieldInsn(Opcodes.GETFIELD, LOOP_CLAUSES, "clauses", MHARY2);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1498
        emitAstoreInsn(clauseDataIndex);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1499
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1500
        // INIT:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1501
        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
  1502
            MethodType cInitType = loopType.changeReturnType(loopClauseTypes[c].basicTypeClass());
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1503
            emitLoopHandleInvoke(invoker, inits, c, args, false, cInitType, loopLocalStateTypes, clauseDataIndex,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1504
                    firstLoopStateIndex);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1505
            if (cInitType.returnType() != void.class) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1506
                emitStoreInsn(BasicType.basicType(cInitType.returnType()), firstLoopStateIndex + state);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1507
                ++state;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1508
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1509
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1510
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1511
        // LOOP:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1512
        mv.visitLabel(lLoop);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1513
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1514
        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
  1515
            lNext = new Label();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1516
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1517
            MethodType stepType = loopHandleType.changeReturnType(loopClauseTypes[c].basicTypeClass());
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1518
            boolean isVoid = stepType.returnType() == void.class;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1519
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1520
            // invoke loop step
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1521
            emitLoopHandleInvoke(invoker, steps, c, args, true, stepType, loopLocalStateTypes, clauseDataIndex,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1522
                    firstLoopStateIndex);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1523
            if (!isVoid) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1524
                emitStoreInsn(BasicType.basicType(stepType.returnType()), firstLoopStateIndex + state);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1525
                ++state;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1526
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1527
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1528
            // invoke loop predicate
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1529
            emitLoopHandleInvoke(invoker, preds, c, args, true, predType, loopLocalStateTypes, clauseDataIndex,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1530
                    firstLoopStateIndex);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1531
            mv.visitJumpInsn(Opcodes.IFNE, lNext);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1532
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1533
            // invoke fini
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1534
            emitLoopHandleInvoke(invoker, finis, c, args, true, finiType, loopLocalStateTypes, clauseDataIndex,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1535
                    firstLoopStateIndex);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1536
            mv.visitJumpInsn(Opcodes.GOTO, lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1537
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1538
            // 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
  1539
            mv.visitLabel(lNext);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1540
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1541
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1542
        mv.visitJumpInsn(Opcodes.GOTO, lLoop);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1543
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1544
        // DONE:
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1545
        mv.visitLabel(lDone);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1546
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1547
        return result;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1548
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1549
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1550
    private int extendLocalsMap(Class<?>[] types) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1551
        int firstSlot = localsMap.length - 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1552
        localsMap = Arrays.copyOf(localsMap, localsMap.length + types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1553
        localClasses = Arrays.copyOf(localClasses, localClasses.length + types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1554
        System.arraycopy(types, 0, localClasses, firstSlot, types.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1555
        int index = localsMap[firstSlot - 1] + 1;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1556
        int lastSlots = 0;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1557
        for (int i = 0; i < types.length; ++i) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1558
            localsMap[firstSlot + i] = index;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1559
            lastSlots = BasicType.basicType(localClasses[firstSlot + i]).basicTypeSlots();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1560
            index += lastSlots;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1561
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1562
        localsMap[localsMap.length - 1] = index - lastSlots;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1563
        return firstSlot;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1564
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1565
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1566
    private void emitLoopHandleInvoke(Name holder, int handles, int clause, Name args, boolean pushLocalState,
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1567
                                      MethodType type, Class<?>[] loopLocalStateTypes, int clauseDataSlot,
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1568
                                      int firstLoopStateSlot) {
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1569
        // load handle for clause
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1570
        emitPushClauseArray(clauseDataSlot, handles);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1571
        emitIconstInsn(clause);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1572
        mv.visitInsn(Opcodes.AALOAD);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1573
        // load loop state (preceding the other arguments)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1574
        if (pushLocalState) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1575
            for (int s = 0; s < loopLocalStateTypes.length; ++s) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1576
                emitLoadInsn(BasicType.basicType(loopLocalStateTypes[s]), firstLoopStateSlot + s);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1577
            }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1578
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1579
        // load loop args (skip 0: method handle)
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1580
        emitPushArguments(args, 1);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1581
        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
  1582
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1583
41125
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1584
    private void emitPushClauseArray(int clauseDataSlot, int which) {
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1585
        emitAloadInsn(clauseDataSlot);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1586
        emitIconstInsn(which - 1);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1587
        mv.visitInsn(Opcodes.AALOAD);
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1588
    }
88d76c8db1fa 8161211: better inlining support for loop bytecode intrinsics
mhaupt
parents: 40559
diff changeset
  1589
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1590
    private void emitZero(BasicType type) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1591
        switch (type) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1592
            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
  1593
            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
  1594
            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
  1595
            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
  1596
            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
  1597
            default: throw new InternalError("unknown type: " + type);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1598
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1599
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 38376
diff changeset
  1600
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1601
    private void emitPushArguments(Name args, int start) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
  1602
        MethodType type = args.function.methodType();
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1603
        for (int i = start; i < args.arguments.length; i++) {
40272
6af4511ee5a4 8163878: Remove unnecessary bridge methods, allocations in java.lang.invoke
redestad
parents: 40258
diff changeset
  1604
            emitPushArgument(type.parameterType(i), args.arguments[i]);
23038
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1605
        }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1606
    }
bb8b3b23af3a 8027827: Improve performance of catchException combinator
vlivanov
parents: 22297
diff changeset
  1607
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1608
    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
  1609
        Object arg = name.arguments[paramIndex];
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1610
        Class<?> ptype = name.function.methodType().parameterType(paramIndex);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1611
        emitPushArgument(ptype, arg);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1612
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1613
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1614
    private void emitPushArgument(Class<?> ptype, Object arg) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1615
        BasicType bptype = basicType(ptype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1616
        if (arg instanceof Name) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1617
            Name n = (Name) arg;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1618
            emitLoadInsn(n.type, n.index());
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1619
            emitImplicitConversion(n.type, ptype, n);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1620
        } 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
  1621
            emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1622
        } else {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1623
            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
  1624
                emitConst(arg);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1625
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1626
                mv.visitLdcInsn(constantPlaceholder(arg));
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1627
                emitImplicitConversion(L_TYPE, ptype, arg);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1628
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1629
        }
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
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1632
    /**
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1633
     * Store the name to its local, if necessary.
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1634
     */
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1635
    private void emitStoreResult(Name name) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1636
        if (name != null && name.type != V_TYPE) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1637
            // non-void: actually assign
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1638
            emitStoreInsn(name.type, name.index());
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1639
        }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1640
    }
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1641
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1642
    /**
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1643
     * 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
  1644
     */
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1645
    private void emitReturn(Name onStack) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1646
        // return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1647
        Class<?> rclass = invokerType.returnType();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1648
        BasicType rtype = lambdaForm.returnType();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1649
        assert(rtype == basicType(rclass));  // must agree
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1650
        if (rtype == V_TYPE) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1651
            // void
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1652
            mv.visitInsn(Opcodes.RETURN);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1653
            // 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
  1654
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1655
            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
  1656
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1657
            // 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
  1658
            if (rn != onStack) {
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1659
                emitLoadInsn(rtype, lambdaForm.result);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1660
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1661
26464
65b37da18e06 8037209: Improvements and cleanups to bytecode assembly for lambda forms
vlivanov
parents: 25991
diff changeset
  1662
            emitImplicitConversion(rtype, rclass, rn);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1663
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1664
            // generate actual return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1665
            emitReturnInsn(rtype);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1666
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1667
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1668
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
     * 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
  1671
     */
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1672
    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
  1673
        // Here's how.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1674
        // -   indicates forbidden
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1675
        // <-> indicates implicit
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1676
        //      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
  1677
        // from boolean    <->        -        -        -        -        -        -        -
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1678
        //      byte        -       <->       i2s      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1679
        //      short       -       i2b       <->      i2c      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1680
        //      char        -       i2b       i2s      <->      <->      i2l      i2f      i2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1681
        //      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
  1682
        //      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
  1683
        //      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
  1684
        //      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
  1685
        if (from == to) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1686
            // 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
  1687
            return;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1688
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1689
        if (from.isSubwordOrInt()) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1690
            // 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
  1691
            emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1692
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1693
            // 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
  1694
            if (to.isSubwordOrInt()) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1695
                // cast to {byte,short,char,int}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1696
                emitX2I(from);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1697
                if (to.bitWidth() < 32) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1698
                    // 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
  1699
                    emitI2X(to);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1700
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1701
            } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1702
                // 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
  1703
                boolean error = false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1704
                switch (from) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1705
                case LONG:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1706
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1707
                    case FLOAT:   mv.visitInsn(Opcodes.L2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1708
                    case DOUBLE:  mv.visitInsn(Opcodes.L2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1709
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1710
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1711
                    break;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1712
                case FLOAT:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1713
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1714
                    case LONG :   mv.visitInsn(Opcodes.F2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1715
                    case DOUBLE:  mv.visitInsn(Opcodes.F2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1716
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1717
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1718
                    break;
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1719
                case DOUBLE:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1720
                    switch (to) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1721
                    case LONG :   mv.visitInsn(Opcodes.D2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1722
                    case FLOAT:   mv.visitInsn(Opcodes.D2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1723
                    default:      error = true;               break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1724
                    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1725
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1726
                default:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1727
                    error = true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1728
                    break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1729
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1730
                if (error) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1731
                    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
  1732
                }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1733
            }
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
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1736
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1737
    private void emitI2X(Wrapper type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1738
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1739
        case BYTE:    mv.visitInsn(Opcodes.I2B);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1740
        case SHORT:   mv.visitInsn(Opcodes.I2S);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1741
        case CHAR:    mv.visitInsn(Opcodes.I2C);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1742
        case INT:     /* naught */                break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1743
        case LONG:    mv.visitInsn(Opcodes.I2L);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1744
        case FLOAT:   mv.visitInsn(Opcodes.I2F);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1745
        case DOUBLE:  mv.visitInsn(Opcodes.I2D);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1746
        case BOOLEAN:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1747
            // For compatibility with ValueConversions and explicitCastArguments:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1748
            mv.visitInsn(Opcodes.ICONST_1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1749
            mv.visitInsn(Opcodes.IAND);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1750
            break;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1751
        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
  1752
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1753
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1754
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1755
    private void emitX2I(Wrapper type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1756
        switch (type) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1757
        case LONG:    mv.visitInsn(Opcodes.L2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1758
        case FLOAT:   mv.visitInsn(Opcodes.F2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1759
        case DOUBLE:  mv.visitInsn(Opcodes.D2I);  break;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1760
        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
  1761
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1762
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1763
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1764
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1765
     * 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
  1766
     */
34429
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1767
    static MemberName generateLambdaFormInterpreterEntryPoint(MethodType mt) {
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1768
        assert(isValidSignature(basicTypeSignature(mt)));
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1769
        String name = "interpret_"+basicTypeChar(mt.returnType());
8893c723602e 8072844: Use more efficient LambdaForm type representation
mhaupt
parents: 34384
diff changeset
  1770
        MethodType type = mt;  // includes leading argument
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1771
        type = type.changeParameterType(0, MethodHandle.class);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1772
        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
  1773
        return g.loadMethod(g.generateLambdaFormInterpreterEntryPointBytes());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1774
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1775
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1776
    private byte[] generateLambdaFormInterpreterEntryPointBytes() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1777
        classFilePrologue();
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
  1778
        methodPrologue();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1779
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1780
        // Suppress this method in backtraces displayed to the user.
53435
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
  1781
        mv.visitAnnotation(HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1782
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1783
        // Don't inline the interpreter entry.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1784
        mv.visitAnnotation(DONTINLINE_SIG, true);
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1785
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1786
        // create parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1787
        emitIconstInsn(invokerType.parameterCount());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1788
        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
  1789
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1790
        // fill parameter array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1791
        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
  1792
            Class<?> ptype = invokerType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1793
            mv.visitInsn(Opcodes.DUP);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1794
            emitIconstInsn(i);
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1795
            emitLoadInsn(basicType(ptype), i);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1796
            // box if primitive type
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1797
            if (ptype.isPrimitive()) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1798
                emitBoxing(Wrapper.forPrimitiveType(ptype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1799
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1800
            mv.visitInsn(Opcodes.AASTORE);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1801
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1802
        // invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1803
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1804
        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
  1805
        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
  1806
        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
  1807
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1808
        // maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1809
        Class<?> rtype = invokerType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1810
        if (rtype.isPrimitive() && rtype != void.class) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1811
            emitUnboxing(Wrapper.forPrimitiveType(rtype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1812
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1813
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1814
        // return statement
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1815
        emitReturnInsn(basicType(rtype));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1816
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
  1817
        methodEpilogue();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1818
        bogusMethod(invokerType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1819
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1820
        final byte[] classFile = cw.toByteArray();
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
  1821
        maybeDump(classFile);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1822
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1823
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1824
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1825
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1826
     * Generate bytecode for a NamedFunction invoker.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1827
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1828
    static MemberName generateNamedFunctionInvoker(MethodTypeForm typeForm) {
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1829
        MethodType invokerType = NamedFunction.INVOKER_METHOD_TYPE;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1830
        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
  1831
        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
  1832
        return g.loadMethod(g.generateNamedFunctionInvokerImpl(typeForm));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1833
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1834
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1835
    private byte[] generateNamedFunctionInvokerImpl(MethodTypeForm typeForm) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1836
        MethodType dstType = typeForm.erasedType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1837
        classFilePrologue();
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
  1838
        methodPrologue();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1839
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1840
        // Suppress this method in backtraces displayed to the user.
53435
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 52220
diff changeset
  1841
        mv.visitAnnotation(HIDDEN_SIG, true);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1842
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1843
        // Force inlining of this invoker method.
36016
e1580bae8eb5 8144931: Assert class signatures are correct and refer to valid classes
srastogi
parents: 35256
diff changeset
  1844
        mv.visitAnnotation(FORCEINLINE_SIG, true);
14852
d513c47448f4 8005345: JSR 292: JDK performance tweaks
twisti
parents: 14595
diff changeset
  1845
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1846
        // Load receiver
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1847
        emitAloadInsn(0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1848
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1849
        // Load arguments from array
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1850
        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
  1851
            emitAloadInsn(1);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1852
            emitIconstInsn(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1853
            mv.visitInsn(Opcodes.AALOAD);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1854
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1855
            // Maybe unbox
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1856
            Class<?> dptype = dstType.parameterType(i);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1857
            if (dptype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1858
                Wrapper dstWrapper = Wrapper.forBasicType(dptype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1859
                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
  1860
                emitUnboxing(srcWrapper);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1861
                emitPrimCast(srcWrapper, dstWrapper);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1862
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1863
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1864
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1865
        // Invoke
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1866
        String targetDesc = dstType.basicType().toMethodDescriptorString();
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22287
diff changeset
  1867
        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
  1868
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1869
        // Box primitive types
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1870
        Class<?> rtype = dstType.returnType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1871
        if (rtype != void.class && rtype.isPrimitive()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1872
            Wrapper srcWrapper = Wrapper.forBasicType(rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1873
            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
  1874
            // boolean casts not allowed
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1875
            emitPrimCast(srcWrapper, dstWrapper);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1876
            emitBoxing(dstWrapper);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1877
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1878
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1879
        // 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
  1880
        if (rtype == void.class) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1881
            mv.visitInsn(Opcodes.ACONST_NULL);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1882
        }
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents: 23918
diff changeset
  1883
        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
  1884
40211
8b78c9b31244 8163369: Enable generating DMH classes at link time
redestad
parents: 39483
diff changeset
  1885
        methodEpilogue();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1886
        bogusMethod(dstType);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1887
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1888
        final byte[] classFile = cw.toByteArray();
44591
b9bf065070fe 8178387: Reduce memory churn when creating java.lang.invoke entities
redestad
parents: 44590
diff changeset
  1889
        maybeDump(classFile);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1890
        return classFile;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1891
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1892
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1893
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1894
     * 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
  1895
     * for debugging purposes.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1896
     */
49206
e991d0be4cc9 8199453: Enable link-time generation of constructor forms
redestad
parents: 49116
diff changeset
  1897
    private void bogusMethod(Object os) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1898
        if (DUMP_CLASS_FILES) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1899
            mv = cw.visitMethod(Opcodes.ACC_STATIC, "dummy", "()V", null, null);
49206
e991d0be4cc9 8199453: Enable link-time generation of constructor forms
redestad
parents: 49116
diff changeset
  1900
            mv.visitLdcInsn(os.toString());
e991d0be4cc9 8199453: Enable link-time generation of constructor forms
redestad
parents: 49116
diff changeset
  1901
            mv.visitInsn(Opcodes.POP);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1902
            mv.visitInsn(Opcodes.RETURN);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1903
            mv.visitMaxs(0, 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1904
            mv.visitEnd();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1905
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1906
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
diff changeset
  1907
}