jdk/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java
author psandoz
Tue, 11 Jul 2017 10:04:34 -0700
changeset 45970 ce7b5822f211
parent 40810 b88d5910ea1e
child 46041 d8e68e273172
permissions -rw-r--r--
8184119: Incorrect return processing for the LF editor of MethodHandles.permuteArguments Reviewed-by: jrose, redestad, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     1
/*
45970
ce7b5822f211 8184119: Incorrect return processing for the LF editor of MethodHandles.permuteArguments
psandoz
parents: 40810
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     4
 *
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    10
 *
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    15
 * accompanied this code).
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    16
 *
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    20
 *
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    23
 * questions.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    24
 */
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    25
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    26
package java.lang.invoke;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    27
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    28
import sun.invoke.util.Wrapper;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    29
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
    30
import java.lang.ref.SoftReference;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    31
import java.util.Arrays;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    32
import java.util.Collections;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    33
import java.util.concurrent.ConcurrentHashMap;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    34
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    35
import static java.lang.invoke.LambdaForm.*;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    36
import static java.lang.invoke.LambdaForm.BasicType.*;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    37
import static java.lang.invoke.MethodHandleImpl.Intrinsic;
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
    38
import static java.lang.invoke.MethodHandleImpl.NF_loop;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    39
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    40
/** Transforms on LFs.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    41
 *  A lambda-form editor can derive new LFs from its base LF.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    42
 *  The editor can cache derived LFs, which simplifies the reuse of their underlying bytecodes.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    43
 *  To support this caching, a LF has an optional pointer to its editor.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    44
 */
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    45
class LambdaFormEditor {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    46
    final LambdaForm lambdaForm;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    47
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    48
    private LambdaFormEditor(LambdaForm lambdaForm) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    49
        this.lambdaForm = lambdaForm;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    50
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    51
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    52
    // Factory method.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    53
    static LambdaFormEditor lambdaFormEditor(LambdaForm lambdaForm) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    54
        // TO DO:  Consider placing intern logic here, to cut down on duplication.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    55
        // lambdaForm = findPreexistingEquivalent(lambdaForm)
29020
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 27803
diff changeset
    56
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 27803
diff changeset
    57
        // Always use uncustomized version for editing.
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 27803
diff changeset
    58
        // It helps caching and customized LambdaForms reuse transformCache field to keep a link to uncustomized version.
9f6d43586ccb 8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents: 27803
diff changeset
    59
        return new LambdaFormEditor(lambdaForm.uncustomize());
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    60
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    61
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    62
    /** A description of a cached transform, possibly associated with the result of the transform.
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    63
     *  The logical content is a sequence of byte values, starting with a kind value.
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    64
     *  The sequence is unterminated, ending with an indefinite number of zero bytes.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    65
     *  Sequences that are simple (short enough and with small enough values) pack into a 64-bit long.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    66
     */
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
    67
    private static final class Transform extends SoftReference<LambdaForm> {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    68
        final long packedBytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    69
        final byte[] fullBytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    70
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    71
        // maybe add more for guard with test, catch exception, pointwise type conversions
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    72
        private static final byte
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    73
                BIND_ARG = 1,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    74
                ADD_ARG = 2,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    75
                DUP_ARG = 3,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    76
                SPREAD_ARGS = 4,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    77
                FILTER_ARG = 5,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    78
                FILTER_RETURN = 6,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    79
                FILTER_RETURN_TO_ZERO = 7,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    80
                COLLECT_ARGS = 8,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    81
                COLLECT_ARGS_TO_VOID = 9,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    82
                COLLECT_ARGS_TO_ARRAY = 10,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    83
                FOLD_ARGS = 11,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    84
                FOLD_ARGS_TO_VOID = 12,
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
    85
                PERMUTE_ARGS = 13,
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
    86
                LOCAL_TYPES = 14,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
    87
                FOLD_SELECT_ARGS = 15,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
    88
                FOLD_SELECT_ARGS_TO_VOID = 16;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    89
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    90
        private static final boolean STRESS_TEST = false; // turn on to disable most packing
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    91
        private static final int
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    92
                PACKED_BYTE_SIZE = (STRESS_TEST ? 2 : 4),
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    93
                PACKED_BYTE_MASK = (1 << PACKED_BYTE_SIZE) - 1,
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    94
                PACKED_BYTE_MAX_LENGTH = (STRESS_TEST ? 3 : 64 / PACKED_BYTE_SIZE);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    95
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    96
        private static long packedBytes(byte[] bytes) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    97
            if (bytes.length > PACKED_BYTE_MAX_LENGTH)  return 0;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    98
            long pb = 0;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
    99
            int bitset = 0;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   100
            for (int i = 0; i < bytes.length; i++) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   101
                int b = bytes[i] & 0xFF;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   102
                bitset |= b;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   103
                pb |= (long)b << (i * PACKED_BYTE_SIZE);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   104
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   105
            if (!inRange(bitset))
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   106
                return 0;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   107
            return pb;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   108
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   109
        private static long packedBytes(int b0, int b1) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   110
            assert(inRange(b0 | b1));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   111
            return (  (b0 << 0*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   112
                    | (b1 << 1*PACKED_BYTE_SIZE));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   113
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   114
        private static long packedBytes(int b0, int b1, int b2) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   115
            assert(inRange(b0 | b1 | b2));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   116
            return (  (b0 << 0*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   117
                    | (b1 << 1*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   118
                    | (b2 << 2*PACKED_BYTE_SIZE));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   119
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   120
        private static long packedBytes(int b0, int b1, int b2, int b3) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   121
            assert(inRange(b0 | b1 | b2 | b3));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   122
            return (  (b0 << 0*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   123
                    | (b1 << 1*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   124
                    | (b2 << 2*PACKED_BYTE_SIZE)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   125
                    | (b3 << 3*PACKED_BYTE_SIZE));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   126
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   127
        private static boolean inRange(int bitset) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   128
            assert((bitset & 0xFF) == bitset);  // incoming values must fit in *unsigned* byte
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   129
            return ((bitset & ~PACKED_BYTE_MASK) == 0);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   130
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   131
        private static byte[] fullBytes(int... byteValues) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   132
            byte[] bytes = new byte[byteValues.length];
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   133
            int i = 0;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   134
            for (int bv : byteValues) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   135
                bytes[i++] = bval(bv);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   136
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   137
            assert(packedBytes(bytes) == 0);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   138
            return bytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   139
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   140
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   141
        private Transform(long packedBytes, byte[] fullBytes, LambdaForm result) {
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   142
            super(result);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   143
            this.packedBytes = packedBytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   144
            this.fullBytes = fullBytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   145
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   146
        private Transform(long packedBytes) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   147
            this(packedBytes, null, null);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   148
            assert(packedBytes != 0);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   149
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   150
        private Transform(byte[] fullBytes) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   151
            this(0, fullBytes, null);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   152
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   153
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   154
        private static byte bval(int b) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   155
            assert((b & 0xFF) == b);  // incoming value must fit in *unsigned* byte
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   156
            return (byte)b;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   157
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   158
        static Transform of(byte k, int b1) {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   159
            byte b0 = bval(k);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   160
            if (inRange(b0 | b1))
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   161
                return new Transform(packedBytes(b0, b1));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   162
            else
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   163
                return new Transform(fullBytes(b0, b1));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   164
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   165
        static Transform of(byte b0, int b1, int b2) {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   166
            if (inRange(b0 | b1 | b2))
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   167
                return new Transform(packedBytes(b0, b1, b2));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   168
            else
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   169
                return new Transform(fullBytes(b0, b1, b2));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   170
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   171
        static Transform of(byte b0, int b1, int b2, int b3) {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   172
            if (inRange(b0 | b1 | b2 | b3))
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   173
                return new Transform(packedBytes(b0, b1, b2, b3));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   174
            else
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   175
                return new Transform(fullBytes(b0, b1, b2, b3));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   176
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   177
        private static final byte[] NO_BYTES = {};
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   178
        static Transform of(byte kind, int... b123) {
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   179
            return ofBothArrays(kind, b123, NO_BYTES);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   180
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   181
        static Transform of(byte kind, int b1, byte[] b234) {
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   182
            return ofBothArrays(kind, new int[]{ b1 }, b234);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   183
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   184
        static Transform of(byte kind, int b1, int b2, byte[] b345) {
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   185
            return ofBothArrays(kind, new int[]{ b1, b2 }, b345);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   186
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   187
        private static Transform ofBothArrays(byte kind, int[] b123, byte[] b456) {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   188
            byte[] fullBytes = new byte[1 + b123.length + b456.length];
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   189
            int i = 0;
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   190
            fullBytes[i++] = bval(kind);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   191
            for (int bv : b123) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   192
                fullBytes[i++] = bval(bv);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   193
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   194
            for (byte bv : b456) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   195
                fullBytes[i++] = bv;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   196
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   197
            long packedBytes = packedBytes(fullBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   198
            if (packedBytes != 0)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   199
                return new Transform(packedBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   200
            else
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   201
                return new Transform(fullBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   202
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   203
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   204
        Transform withResult(LambdaForm result) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   205
            return new Transform(this.packedBytes, this.fullBytes, result);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   206
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   207
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   208
        @Override
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   209
        public boolean equals(Object obj) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   210
            return obj instanceof Transform && equals((Transform)obj);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   211
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   212
        public boolean equals(Transform that) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   213
            return this.packedBytes == that.packedBytes && Arrays.equals(this.fullBytes, that.fullBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   214
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   215
        @Override
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   216
        public int hashCode() {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   217
            if (packedBytes != 0) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   218
                assert(fullBytes == null);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   219
                return Long.hashCode(packedBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   220
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   221
            return Arrays.hashCode(fullBytes);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   222
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   223
        @Override
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   224
        public String toString() {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   225
            StringBuilder buf = new StringBuilder();
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   226
            long bits = packedBytes;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   227
            if (bits != 0) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   228
                buf.append("(");
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   229
                while (bits != 0) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   230
                    buf.append(bits & PACKED_BYTE_MASK);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   231
                    bits >>>= PACKED_BYTE_SIZE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   232
                    if (bits != 0)  buf.append(",");
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   233
                }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   234
                buf.append(")");
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   235
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   236
            if (fullBytes != null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   237
                buf.append("unpacked");
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   238
                buf.append(Arrays.toString(fullBytes));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   239
            }
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   240
            LambdaForm result = get();
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   241
            if (result != null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   242
                buf.append(" result=");
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   243
                buf.append(result);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   244
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   245
            return buf.toString();
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   246
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   247
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   248
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   249
    /** Find a previously cached transform equivalent to the given one, and return its result. */
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   250
    private LambdaForm getInCache(Transform key) {
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   251
        assert(key.get() == null);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   252
        // The transformCache is one of null, Transform, Transform[], or ConcurrentHashMap.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   253
        Object c = lambdaForm.transformCache;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   254
        Transform k = null;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   255
        if (c instanceof ConcurrentHashMap) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   256
            @SuppressWarnings("unchecked")
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   257
            ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   258
            k = m.get(key);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   259
        } else if (c == null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   260
            return null;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   261
        } else if (c instanceof Transform) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   262
            // one-element cache avoids overhead of an array
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   263
            Transform t = (Transform)c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   264
            if (t.equals(key))  k = t;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   265
        } else {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   266
            Transform[] ta = (Transform[])c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   267
            for (int i = 0; i < ta.length; i++) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   268
                Transform t = ta[i];
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   269
                if (t == null)  break;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   270
                if (t.equals(key)) { k = t; break; }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   271
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   272
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   273
        assert(k == null || key.equals(k));
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   274
        return (k != null) ? k.get() : null;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   275
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   276
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   277
    /** Arbitrary but reasonable limits on Transform[] size for cache. */
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   278
    private static final int MIN_CACHE_ARRAY_SIZE = 4, MAX_CACHE_ARRAY_SIZE = 16;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   279
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   280
    /** Cache a transform with its result, and return that result.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   281
     *  But if an equivalent transform has already been cached, return its result instead.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   282
     */
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   283
    private LambdaForm putInCache(Transform key, LambdaForm form) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   284
        key = key.withResult(form);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   285
        for (int pass = 0; ; pass++) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   286
            Object c = lambdaForm.transformCache;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   287
            if (c instanceof ConcurrentHashMap) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   288
                @SuppressWarnings("unchecked")
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   289
                ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   290
                Transform k = m.putIfAbsent(key, key);
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   291
                if (k == null) return form;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   292
                LambdaForm result = k.get();
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   293
                if (result != null) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   294
                    return result;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   295
                } else {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   296
                    if (m.replace(key, k, key)) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   297
                        return form;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   298
                    } else {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   299
                        continue;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   300
                    }
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   301
                }
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   302
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   303
            assert(pass == 0);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   304
            synchronized (lambdaForm) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   305
                c = lambdaForm.transformCache;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   306
                if (c instanceof ConcurrentHashMap)
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   307
                    continue;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   308
                if (c == null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   309
                    lambdaForm.transformCache = key;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   310
                    return form;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   311
                }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   312
                Transform[] ta;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   313
                if (c instanceof Transform) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   314
                    Transform k = (Transform)c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   315
                    if (k.equals(key)) {
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   316
                        LambdaForm result = k.get();
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   317
                        if (result == null) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   318
                            lambdaForm.transformCache = key;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   319
                            return form;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   320
                        } else {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   321
                            return result;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   322
                        }
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   323
                    } else if (k.get() == null) { // overwrite stale entry
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   324
                        lambdaForm.transformCache = key;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   325
                        return form;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   326
                    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   327
                    // expand one-element cache to small array
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   328
                    ta = new Transform[MIN_CACHE_ARRAY_SIZE];
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   329
                    ta[0] = k;
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   330
                    lambdaForm.transformCache = ta;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   331
                } else {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   332
                    // it is already expanded
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   333
                    ta = (Transform[])c;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   334
                }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   335
                int len = ta.length;
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   336
                int stale = -1;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   337
                int i;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   338
                for (i = 0; i < len; i++) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   339
                    Transform k = ta[i];
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   340
                    if (k == null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   341
                        break;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   342
                    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   343
                    if (k.equals(key)) {
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   344
                        LambdaForm result = k.get();
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   345
                        if (result == null) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   346
                            ta[i] = key;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   347
                            return form;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   348
                        } else {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   349
                            return result;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   350
                        }
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   351
                    } else if (stale < 0 && k.get() == null) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   352
                        stale = i; // remember 1st stale entry index
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   353
                    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   354
                }
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   355
                if (i < len || stale >= 0) {
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   356
                    // just fall through to cache update
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   357
                } else if (len < MAX_CACHE_ARRAY_SIZE) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   358
                    len = Math.min(len * 2, MAX_CACHE_ARRAY_SIZE);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   359
                    ta = Arrays.copyOf(ta, len);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   360
                    lambdaForm.transformCache = ta;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   361
                } else {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   362
                    ConcurrentHashMap<Transform, Transform> m = new ConcurrentHashMap<>(MAX_CACHE_ARRAY_SIZE * 2);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   363
                    for (Transform k : ta) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   364
                        m.put(k, k);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   365
                    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   366
                    lambdaForm.transformCache = m;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   367
                    // The second iteration will update for this query, concurrently.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   368
                    continue;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   369
                }
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   370
                int idx = (stale >= 0) ? stale : i;
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26482
diff changeset
   371
                ta[idx] = key;
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   372
                return form;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   373
            }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   374
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   375
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   376
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   377
    private LambdaFormBuffer buffer() {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   378
        return new LambdaFormBuffer(lambdaForm);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   379
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   380
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   381
    /// Editing methods for method handles.  These need to have fast paths.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   382
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   383
    private BoundMethodHandle.SpeciesData oldSpeciesData() {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   384
        return BoundMethodHandle.speciesData(lambdaForm);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   385
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   386
    private BoundMethodHandle.SpeciesData newSpeciesData(BasicType type) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   387
        return oldSpeciesData().extendWith(type);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   388
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   389
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   390
    BoundMethodHandle bindArgumentL(BoundMethodHandle mh, int pos, Object value) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   391
        assert(mh.speciesData() == oldSpeciesData());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   392
        BasicType bt = L_TYPE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   393
        MethodType type2 = bindArgumentType(mh, pos, bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   394
        LambdaForm form2 = bindArgumentForm(1+pos);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   395
        return mh.copyWithExtendL(type2, form2, value);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   396
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   397
    BoundMethodHandle bindArgumentI(BoundMethodHandle mh, int pos, int value) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   398
        assert(mh.speciesData() == oldSpeciesData());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   399
        BasicType bt = I_TYPE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   400
        MethodType type2 = bindArgumentType(mh, pos, bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   401
        LambdaForm form2 = bindArgumentForm(1+pos);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   402
        return mh.copyWithExtendI(type2, form2, value);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   403
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   404
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   405
    BoundMethodHandle bindArgumentJ(BoundMethodHandle mh, int pos, long value) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   406
        assert(mh.speciesData() == oldSpeciesData());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   407
        BasicType bt = J_TYPE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   408
        MethodType type2 = bindArgumentType(mh, pos, bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   409
        LambdaForm form2 = bindArgumentForm(1+pos);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   410
        return mh.copyWithExtendJ(type2, form2, value);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   411
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   412
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   413
    BoundMethodHandle bindArgumentF(BoundMethodHandle mh, int pos, float value) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   414
        assert(mh.speciesData() == oldSpeciesData());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   415
        BasicType bt = F_TYPE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   416
        MethodType type2 = bindArgumentType(mh, pos, bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   417
        LambdaForm form2 = bindArgumentForm(1+pos);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   418
        return mh.copyWithExtendF(type2, form2, value);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   419
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   420
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   421
    BoundMethodHandle bindArgumentD(BoundMethodHandle mh, int pos, double value) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   422
        assert(mh.speciesData() == oldSpeciesData());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   423
        BasicType bt = D_TYPE;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   424
        MethodType type2 = bindArgumentType(mh, pos, bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   425
        LambdaForm form2 = bindArgumentForm(1+pos);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   426
        return mh.copyWithExtendD(type2, form2, value);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   427
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   428
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   429
    private MethodType bindArgumentType(BoundMethodHandle mh, int pos, BasicType bt) {
29221
e946919cf487 8073644: Assertion in LambdaFormEditor.bindArgumentType is too strict
vlivanov
parents: 29020
diff changeset
   430
        assert(mh.form.uncustomize() == lambdaForm);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   431
        assert(mh.form.names[1+pos].type == bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   432
        assert(BasicType.basicType(mh.type().parameterType(pos)) == bt);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   433
        return mh.type().dropParameterTypes(pos, pos+1);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   434
    }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   435
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   436
    /// Editing methods for lambda forms.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   437
    // Each editing method can (potentially) cache the edited LF so that it can be reused later.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   438
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   439
    LambdaForm bindArgumentForm(int pos) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   440
        Transform key = Transform.of(Transform.BIND_ARG, pos);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   441
        LambdaForm form = getInCache(key);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   442
        if (form != null) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   443
            assert(form.parameterConstraint(0) == newSpeciesData(lambdaForm.parameterType(pos)));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   444
            return form;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   445
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   446
        LambdaFormBuffer buf = buffer();
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   447
        buf.startEdit();
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   448
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   449
        BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   450
        BoundMethodHandle.SpeciesData newData = newSpeciesData(lambdaForm.parameterType(pos));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   451
        Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   452
        Name newBaseAddress;
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   453
        NamedFunction getter = newData.getterFunction(oldData.fieldCount());
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   454
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   455
        if (pos != 0) {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   456
            // The newly created LF will run with a different BMH.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   457
            // Switch over any pre-existing BMH field references to the new BMH class.
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   458
            buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   459
            newBaseAddress = oldBaseAddress.withConstraint(newData);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   460
            buf.renameParameter(0, newBaseAddress);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   461
            buf.replaceParameterByNewExpression(pos, new Name(getter, newBaseAddress));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   462
        } else {
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   463
            // cannot bind the MH arg itself, unless oldData is empty
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   464
            assert(oldData == BoundMethodHandle.SpeciesData.EMPTY);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   465
            newBaseAddress = new Name(L_TYPE).withConstraint(newData);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   466
            buf.replaceParameterByNewExpression(0, new Name(getter, newBaseAddress));
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   467
            buf.insertParameter(0, newBaseAddress);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   468
        }
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   469
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   470
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   471
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   472
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   473
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   474
    LambdaForm addArgumentForm(int pos, BasicType type) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   475
        Transform key = Transform.of(Transform.ADD_ARG, pos, type.ordinal());
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   476
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   477
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   478
            assert(form.arity == lambdaForm.arity+1);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   479
            assert(form.parameterType(pos) == type);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   480
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   481
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   482
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   483
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   484
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   485
        buf.insertParameter(pos, new Name(type));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   486
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   487
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   488
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   489
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   490
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   491
    LambdaForm dupArgumentForm(int srcPos, int dstPos) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   492
        Transform key = Transform.of(Transform.DUP_ARG, srcPos, dstPos);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   493
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   494
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   495
            assert(form.arity == lambdaForm.arity-1);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   496
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   497
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   498
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   499
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   500
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   501
        assert(lambdaForm.parameter(srcPos).constraint == null);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   502
        assert(lambdaForm.parameter(dstPos).constraint == null);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   503
        buf.replaceParameterByCopy(dstPos, srcPos);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   504
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   505
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   506
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   507
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   508
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   509
    LambdaForm spreadArgumentsForm(int pos, Class<?> arrayType, int arrayLength) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   510
        Class<?> elementType = arrayType.getComponentType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   511
        Class<?> erasedArrayType = arrayType;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   512
        if (!elementType.isPrimitive())
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   513
            erasedArrayType = Object[].class;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   514
        BasicType bt = basicType(elementType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   515
        int elementTypeKey = bt.ordinal();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   516
        if (bt.basicTypeClass() != elementType) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   517
            if (elementType.isPrimitive()) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   518
                elementTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   519
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   520
        }
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   521
        Transform key = Transform.of(Transform.SPREAD_ARGS, pos, elementTypeKey, arrayLength);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   522
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   523
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   524
            assert(form.arity == lambdaForm.arity - arrayLength + 1);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   525
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   526
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   527
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   528
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   529
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   530
        assert(pos <= MethodType.MAX_JVM_ARITY);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   531
        assert(pos + arrayLength <= lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   532
        assert(pos > 0);  // cannot spread the MH arg itself
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   533
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   534
        Name spreadParam = new Name(L_TYPE);
33841
1d1d1ea6a5f2 8142334: Improve lazy initialization of java.lang.invoke
redestad
parents: 29221
diff changeset
   535
        Name checkSpread = new Name(MethodHandleImpl.NF_checkSpreadArgument, spreadParam, arrayLength);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   536
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   537
        // insert the new expressions
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   538
        int exprPos = lambdaForm.arity();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   539
        buf.insertExpression(exprPos++, checkSpread);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   540
        // adjust the arguments
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   541
        MethodHandle aload = MethodHandles.arrayElementGetter(erasedArrayType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   542
        for (int i = 0; i < arrayLength; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   543
            Name loadArgument = new Name(aload, spreadParam, i);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   544
            buf.insertExpression(exprPos + i, loadArgument);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   545
            buf.replaceParameterByCopy(pos + i, exprPos + i);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   546
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   547
        buf.insertParameter(pos, spreadParam);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   548
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   549
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   550
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   551
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   552
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   553
    LambdaForm collectArgumentsForm(int pos, MethodType collectorType) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   554
        int collectorArity = collectorType.parameterCount();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   555
        boolean dropResult = (collectorType.returnType() == void.class);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   556
        if (collectorArity == 1 && !dropResult) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   557
            return filterArgumentForm(pos, basicType(collectorType.parameterType(0)));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   558
        }
34384
439c06c76808 8143131: Remove unused code from java.lang.invoke
redestad
parents: 33841
diff changeset
   559
        byte[] newTypes = BasicType.basicTypesOrd(collectorType.parameterArray());
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   560
        byte kind = (dropResult
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   561
                ? Transform.COLLECT_ARGS_TO_VOID
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   562
                : Transform.COLLECT_ARGS);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   563
        if (dropResult && collectorArity == 0)  pos = 1;  // pure side effect
34384
439c06c76808 8143131: Remove unused code from java.lang.invoke
redestad
parents: 33841
diff changeset
   564
        Transform key = Transform.of(kind, pos, collectorArity, newTypes);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   565
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   566
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   567
            assert(form.arity == lambdaForm.arity - (dropResult ? 0 : 1) + collectorArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   568
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   569
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   570
        form = makeArgumentCombinationForm(pos, collectorType, false, dropResult);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   571
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   572
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   573
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   574
    LambdaForm collectArgumentArrayForm(int pos, MethodHandle arrayCollector) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   575
        MethodType collectorType = arrayCollector.type();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   576
        int collectorArity = collectorType.parameterCount();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   577
        assert(arrayCollector.intrinsicName() == Intrinsic.NEW_ARRAY);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   578
        Class<?> arrayType = collectorType.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   579
        Class<?> elementType = arrayType.getComponentType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   580
        BasicType argType = basicType(elementType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   581
        int argTypeKey = argType.ordinal();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   582
        if (argType.basicTypeClass() != elementType) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   583
            // return null if it requires more metadata (like String[].class)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   584
            if (!elementType.isPrimitive())
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   585
                return null;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   586
            argTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   587
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   588
        assert(collectorType.parameterList().equals(Collections.nCopies(collectorArity, elementType)));
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   589
        byte kind = Transform.COLLECT_ARGS_TO_ARRAY;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   590
        Transform key = Transform.of(kind, pos, collectorArity, argTypeKey);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   591
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   592
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   593
            assert(form.arity == lambdaForm.arity - 1 + collectorArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   594
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   595
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   596
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   597
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   598
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   599
        assert(pos + 1 <= lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   600
        assert(pos > 0);  // cannot filter the MH arg itself
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   601
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   602
        Name[] newParams = new Name[collectorArity];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   603
        for (int i = 0; i < collectorArity; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   604
            newParams[i] = new Name(pos + i, argType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   605
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   606
        Name callCombiner = new Name(arrayCollector, (Object[]) /*...*/ newParams);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   607
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   608
        // insert the new expression
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   609
        int exprPos = lambdaForm.arity();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   610
        buf.insertExpression(exprPos, callCombiner);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   611
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   612
        // insert new arguments
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   613
        int argPos = pos + 1;  // skip result parameter
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   614
        for (Name newParam : newParams) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   615
            buf.insertParameter(argPos++, newParam);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   616
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   617
        assert(buf.lastIndexOf(callCombiner) == exprPos+newParams.length);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   618
        buf.replaceParameterByCopy(pos, exprPos+newParams.length);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   619
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   620
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   621
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   622
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   623
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   624
    LambdaForm filterArgumentForm(int pos, BasicType newType) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   625
        Transform key = Transform.of(Transform.FILTER_ARG, pos, newType.ordinal());
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   626
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   627
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   628
            assert(form.arity == lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   629
            assert(form.parameterType(pos) == newType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   630
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   631
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   632
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   633
        BasicType oldType = lambdaForm.parameterType(pos);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   634
        MethodType filterType = MethodType.methodType(oldType.basicTypeClass(),
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   635
                newType.basicTypeClass());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   636
        form = makeArgumentCombinationForm(pos, filterType, false, false);
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   637
        return putInCache(key, form);
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   638
    }
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   639
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   640
    private LambdaForm makeArgumentCombinationForm(int pos,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   641
                                                   MethodType combinerType,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   642
                                                   boolean keepArguments, boolean dropResult) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   643
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   644
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   645
        int combinerArity = combinerType.parameterCount();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   646
        int resultArity = (dropResult ? 0 : 1);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   647
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   648
        assert(pos <= MethodType.MAX_JVM_ARITY);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   649
        assert(pos + resultArity + (keepArguments ? combinerArity : 0) <= lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   650
        assert(pos > 0);  // cannot filter the MH arg itself
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   651
        assert(combinerType == combinerType.basicType());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   652
        assert(combinerType.returnType() != void.class || dropResult);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   653
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   654
        BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   655
        BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   656
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   657
        // The newly created LF will run with a different BMH.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   658
        // Switch over any pre-existing BMH field references to the new BMH class.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   659
        Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   660
        buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   661
        Name newBaseAddress = oldBaseAddress.withConstraint(newData);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   662
        buf.renameParameter(0, newBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   663
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   664
        Name getCombiner = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   665
        Object[] combinerArgs = new Object[1 + combinerArity];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   666
        combinerArgs[0] = getCombiner;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   667
        Name[] newParams;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   668
        if (keepArguments) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   669
            newParams = new Name[0];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   670
            System.arraycopy(lambdaForm.names, pos + resultArity,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   671
                             combinerArgs, 1, combinerArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   672
        } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   673
            newParams = new Name[combinerArity];
34384
439c06c76808 8143131: Remove unused code from java.lang.invoke
redestad
parents: 33841
diff changeset
   674
            for (int i = 0; i < newParams.length; i++) {
439c06c76808 8143131: Remove unused code from java.lang.invoke
redestad
parents: 33841
diff changeset
   675
                newParams[i] = new Name(pos + i, basicType(combinerType.parameterType(i)));
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   676
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   677
            System.arraycopy(newParams, 0,
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   678
                             combinerArgs, 1, combinerArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   679
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   680
        Name callCombiner = new Name(combinerType, combinerArgs);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   681
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   682
        // insert the two new expressions
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   683
        int exprPos = lambdaForm.arity();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   684
        buf.insertExpression(exprPos+0, getCombiner);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   685
        buf.insertExpression(exprPos+1, callCombiner);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   686
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   687
        // insert new arguments, if needed
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   688
        int argPos = pos + resultArity;  // skip result parameter
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   689
        for (Name newParam : newParams) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   690
            buf.insertParameter(argPos++, newParam);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   691
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   692
        assert(buf.lastIndexOf(callCombiner) == exprPos+1+newParams.length);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   693
        if (!dropResult) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   694
            buf.replaceParameterByCopy(pos, exprPos+1+newParams.length);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   695
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   696
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   697
        return buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   698
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   699
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   700
    private LambdaForm makeArgumentCombinationForm(int pos,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   701
                                                   MethodType combinerType,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   702
                                                   int[] argPositions,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   703
                                                   boolean keepArguments,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   704
                                                   boolean dropResult) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   705
        LambdaFormBuffer buf = buffer();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   706
        buf.startEdit();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   707
        int combinerArity = combinerType.parameterCount();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   708
        assert(combinerArity == argPositions.length);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   709
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   710
        int resultArity = (dropResult ? 0 : 1);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   711
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   712
        assert(pos <= lambdaForm.arity);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   713
        assert(pos > 0);  // cannot filter the MH arg itself
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   714
        assert(combinerType == combinerType.basicType());
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   715
        assert(combinerType.returnType() != void.class || dropResult);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   716
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   717
        BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   718
        BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   719
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   720
        // The newly created LF will run with a different BMH.
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   721
        // Switch over any pre-existing BMH field references to the new BMH class.
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   722
        Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   723
        buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   724
        Name newBaseAddress = oldBaseAddress.withConstraint(newData);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   725
        buf.renameParameter(0, newBaseAddress);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   726
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   727
        Name getCombiner = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   728
        Object[] combinerArgs = new Object[1 + combinerArity];
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   729
        combinerArgs[0] = getCombiner;
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   730
        Name[] newParams;
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   731
        if (keepArguments) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   732
            newParams = new Name[0];
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   733
            for (int i = 0; i < combinerArity; i++) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   734
                combinerArgs[i + 1] = lambdaForm.parameter(1 + argPositions[i]);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   735
                assert (basicType(combinerType.parameterType(i)) == lambdaForm.parameterType(1 + argPositions[i]));
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   736
            }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   737
        } else {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   738
            newParams = new Name[combinerArity];
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   739
            for (int i = 0; i < newParams.length; i++) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   740
                newParams[i] = lambdaForm.parameter(1 + argPositions[i]);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   741
                assert (basicType(combinerType.parameterType(i)) == lambdaForm.parameterType(1 + argPositions[i]));
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   742
            }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   743
            System.arraycopy(newParams, 0,
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   744
                             combinerArgs, 1, combinerArity);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   745
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   746
        Name callCombiner = new Name(combinerType, combinerArgs);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   747
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   748
        // insert the two new expressions
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   749
        int exprPos = lambdaForm.arity();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   750
        buf.insertExpression(exprPos+0, getCombiner);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   751
        buf.insertExpression(exprPos+1, callCombiner);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   752
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   753
        // insert new arguments, if needed
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   754
        int argPos = pos + resultArity;  // skip result parameter
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   755
        for (Name newParam : newParams) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   756
            buf.insertParameter(argPos++, newParam);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   757
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   758
        assert(buf.lastIndexOf(callCombiner) == exprPos+1+newParams.length);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   759
        if (!dropResult) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   760
            buf.replaceParameterByCopy(pos, exprPos+1+newParams.length);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   761
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   762
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   763
        return buf.endEdit();
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   764
    }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   765
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   766
    LambdaForm filterReturnForm(BasicType newType, boolean constantZero) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   767
        byte kind = (constantZero ? Transform.FILTER_RETURN_TO_ZERO : Transform.FILTER_RETURN);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   768
        Transform key = Transform.of(kind, newType.ordinal());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   769
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   770
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   771
            assert(form.arity == lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   772
            assert(form.returnType() == newType);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   773
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   774
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   775
        LambdaFormBuffer buf = buffer();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   776
        buf.startEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   777
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   778
        int insPos = lambdaForm.names.length;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   779
        Name callFilter;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   780
        if (constantZero) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   781
            // Synthesize a constant zero value for the given type.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   782
            if (newType == V_TYPE)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   783
                callFilter = null;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   784
            else
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   785
                callFilter = new Name(constantZero(newType));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   786
        } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   787
            BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   788
            BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   789
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   790
            // The newly created LF will run with a different BMH.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   791
            // Switch over any pre-existing BMH field references to the new BMH class.
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   792
            Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   793
            buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   794
            Name newBaseAddress = oldBaseAddress.withConstraint(newData);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   795
            buf.renameParameter(0, newBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   796
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   797
            Name getFilter = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   798
            buf.insertExpression(insPos++, getFilter);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   799
            BasicType oldType = lambdaForm.returnType();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   800
            if (oldType == V_TYPE) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   801
                MethodType filterType = MethodType.methodType(newType.basicTypeClass());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   802
                callFilter = new Name(filterType, getFilter);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   803
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   804
                MethodType filterType = MethodType.methodType(newType.basicTypeClass(), oldType.basicTypeClass());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   805
                callFilter = new Name(filterType, getFilter, lambdaForm.names[lambdaForm.result]);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   806
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   807
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   808
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   809
        if (callFilter != null)
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   810
            buf.insertExpression(insPos++, callFilter);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   811
        buf.setResult(callFilter);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   812
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   813
        form = buf.endEdit();
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   814
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   815
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   816
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   817
    LambdaForm foldArgumentsForm(int foldPos, boolean dropResult, MethodType combinerType) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   818
        int combinerArity = combinerType.parameterCount();
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   819
        byte kind = (dropResult ? Transform.FOLD_ARGS_TO_VOID : Transform.FOLD_ARGS);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   820
        Transform key = Transform.of(kind, foldPos, combinerArity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   821
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   822
        if (form != null) {
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   823
            assert(form.arity == lambdaForm.arity - (kind == Transform.FOLD_ARGS ? 1 : 0));
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   824
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   825
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   826
        form = makeArgumentCombinationForm(foldPos, combinerType, true, dropResult);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   827
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   828
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   829
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   830
    LambdaForm foldArgumentsForm(int foldPos, boolean dropResult, MethodType combinerType, int ... argPositions) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   831
        byte kind = (dropResult ? Transform.FOLD_SELECT_ARGS_TO_VOID
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   832
                                : Transform.FOLD_SELECT_ARGS);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   833
        int[] keyArgs = Arrays.copyOf(argPositions, argPositions.length + 1);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   834
        keyArgs[argPositions.length] = foldPos;
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   835
        Transform key = Transform.of(kind, keyArgs);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   836
        LambdaForm form = getInCache(key);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   837
        if (form != null) {
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   838
            assert(form.arity == lambdaForm.arity - (kind == Transform.FOLD_SELECT_ARGS ? 1 : 0));
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   839
            return form;
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   840
        }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   841
        form = makeArgumentCombinationForm(foldPos, combinerType, argPositions, true, dropResult);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   842
        return putInCache(key, form);
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   843
    }
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 40256
diff changeset
   844
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   845
    LambdaForm permuteArgumentsForm(int skip, int[] reorder) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   846
        assert(skip == 1);  // skip only the leading MH argument, names[0]
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   847
        int length = lambdaForm.names.length;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   848
        int outArgs = reorder.length;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   849
        int inTypes = 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   850
        boolean nullPerm = true;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   851
        for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   852
            int inArg = reorder[i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   853
            if (inArg != i)  nullPerm = false;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   854
            inTypes = Math.max(inTypes, inArg+1);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   855
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   856
        assert(skip + reorder.length == lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   857
        if (nullPerm)  return lambdaForm;  // do not bother to cache
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   858
        Transform key = Transform.of(Transform.PERMUTE_ARGS, reorder);
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   859
        LambdaForm form = getInCache(key);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   860
        if (form != null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   861
            assert(form.arity == skip+inTypes) : form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   862
            return form;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   863
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   864
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   865
        BasicType[] types = new BasicType[inTypes];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   866
        for (int i = 0; i < outArgs; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   867
            int inArg = reorder[i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   868
            types[inArg] = lambdaForm.names[skip + i].type;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   869
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   870
        assert (skip + outArgs == lambdaForm.arity);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   871
        assert (permutedTypesMatch(reorder, types, lambdaForm.names, skip));
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   872
        int pos = 0;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   873
        while (pos < outArgs && reorder[pos] == pos) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   874
            pos += 1;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   875
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   876
        Name[] names2 = new Name[length - outArgs + inTypes];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   877
        System.arraycopy(lambdaForm.names, 0, names2, 0, skip + pos);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   878
        int bodyLength = length - lambdaForm.arity;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   879
        System.arraycopy(lambdaForm.names, skip + outArgs, names2, skip + inTypes, bodyLength);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   880
        int arity2 = names2.length - bodyLength;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   881
        int result2 = lambdaForm.result;
45970
ce7b5822f211 8184119: Incorrect return processing for the LF editor of MethodHandles.permuteArguments
psandoz
parents: 40810
diff changeset
   882
        if (result2 >= skip) {
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   883
            if (result2 < skip + outArgs) {
45970
ce7b5822f211 8184119: Incorrect return processing for the LF editor of MethodHandles.permuteArguments
psandoz
parents: 40810
diff changeset
   884
                result2 = reorder[result2 - skip] + skip;
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   885
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   886
                result2 = result2 - outArgs + inTypes;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   887
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   888
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   889
        for (int j = pos; j < outArgs; j++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   890
            Name n = lambdaForm.names[skip + j];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   891
            int i = reorder[j];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   892
            Name n2 = names2[skip + i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   893
            if (n2 == null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   894
                names2[skip + i] = n2 = new Name(types[i]);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   895
            } else {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   896
                assert (n2.type == types[i]);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   897
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   898
            for (int k = arity2; k < names2.length; k++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   899
                names2[k] = names2[k].replaceName(n, n2);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   900
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   901
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   902
        for (int i = skip + pos; i < arity2; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   903
            if (names2[i] == null) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   904
                names2[i] = argument(i, types[i - skip]);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   905
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   906
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   907
        for (int j = lambdaForm.arity; j < lambdaForm.names.length; j++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   908
            int i = j - lambdaForm.arity + arity2;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   909
            Name n = lambdaForm.names[j];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   910
            Name n2 = names2[i];
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   911
            if (n != n2) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   912
                for (int k = i + 1; k < names2.length; k++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   913
                    names2[k] = names2[k].replaceName(n, n2);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   914
                }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   915
            }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   916
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   917
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   918
        form = new LambdaForm(lambdaForm.debugName, arity2, names2, result2);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   919
        return putInCache(key, form);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   920
    }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   921
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   922
    LambdaForm noteLoopLocalTypesForm(int pos, BasicType[] localTypes) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   923
        assert(lambdaForm.isLoop(pos));
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   924
        int[] desc = BasicType.basicTypeOrds(localTypes);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   925
        desc = Arrays.copyOf(desc, desc.length + 1);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   926
        desc[desc.length - 1] = pos;
40256
c5e03eaf7ba2 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
redestad
parents: 39342
diff changeset
   927
        Transform key = Transform.of(Transform.LOCAL_TYPES, desc);
39342
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   928
        LambdaForm form = getInCache(key);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   929
        if (form != null) {
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   930
            return form;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   931
        }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   932
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   933
        // replace the null entry in the MHImpl.loop invocation with localTypes
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   934
        Name invokeLoop = lambdaForm.names[pos + 1];
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   935
        assert(invokeLoop.function == NF_loop);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   936
        Object[] args = Arrays.copyOf(invokeLoop.arguments, invokeLoop.arguments.length);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   937
        assert(args[0] == null);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   938
        args[0] = localTypes;
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   939
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   940
        LambdaFormBuffer buf = buffer();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   941
        buf.startEdit();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   942
        buf.changeName(pos + 1, new Name(NF_loop, args));
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   943
        form = buf.endEdit();
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   944
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   945
        return putInCache(key, form);
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   946
    }
f66a89ed6fca 8143211: provide bytecode intrinsics for loop and try/finally executors
mhaupt
parents: 34384
diff changeset
   947
26482
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   948
    static boolean permutedTypesMatch(int[] reorder, BasicType[] types, Name[] names, int skip) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   949
        for (int i = 0; i < reorder.length; i++) {
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   950
            assert (names[skip + i].isParam());
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   951
            assert (names[skip + i].type == types[reorder[i]]);
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   952
        }
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   953
        return true;
cea1ab1c3ac7 8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents: 26481
diff changeset
   954
    }
26481
c5b74a88a3c0 8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff changeset
   955
}