author | shade |
Tue, 17 May 2016 22:28:00 +0300 | |
changeset 38372 | 017d7578731c |
parent 34384 | 439c06c76808 |
child 39342 | f66a89ed6fca |
permissions | -rw-r--r-- |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
1 |
/* |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
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 |
|
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
28 |
import java.lang.ref.SoftReference; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
29 |
import java.util.Arrays; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
30 |
import static java.lang.invoke.LambdaForm.*; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
31 |
import static java.lang.invoke.LambdaForm.BasicType.*; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
32 |
import static java.lang.invoke.MethodHandleImpl.Intrinsic; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
33 |
import java.util.Collections; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
34 |
import java.util.concurrent.ConcurrentHashMap; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
35 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
36 |
import sun.invoke.util.Wrapper; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
37 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
38 |
/** Transforms on LFs. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
39 |
* 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
|
40 |
* 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
|
41 |
* 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
|
42 |
*/ |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
43 |
class LambdaFormEditor { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
44 |
final LambdaForm lambdaForm; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
45 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
46 |
private LambdaFormEditor(LambdaForm lambdaForm) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
47 |
this.lambdaForm = lambdaForm; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
48 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
49 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
50 |
// Factory method. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
51 |
static LambdaFormEditor lambdaFormEditor(LambdaForm lambdaForm) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
52 |
// 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
|
53 |
// lambdaForm = findPreexistingEquivalent(lambdaForm) |
29020
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27803
diff
changeset
|
54 |
|
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27803
diff
changeset
|
55 |
// Always use uncustomized version for editing. |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27803
diff
changeset
|
56 |
// 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
|
57 |
return new LambdaFormEditor(lambdaForm.uncustomize()); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
58 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
59 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
60 |
/** A description of a cached transform, possibly associated with the result of the transform. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
61 |
* The logical content is a sequence of byte values, starting with a Kind.ordinal value. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
62 |
* 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
|
63 |
* 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
|
64 |
*/ |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
65 |
private static final class Transform extends SoftReference<LambdaForm> { |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
66 |
final long packedBytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
67 |
final byte[] fullBytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
68 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
69 |
private enum Kind { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
70 |
NO_KIND, // necessary because ordinal must be greater than zero |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
71 |
BIND_ARG, ADD_ARG, DUP_ARG, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
72 |
SPREAD_ARGS, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
73 |
FILTER_ARG, FILTER_RETURN, FILTER_RETURN_TO_ZERO, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
74 |
COLLECT_ARGS, COLLECT_ARGS_TO_VOID, COLLECT_ARGS_TO_ARRAY, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
75 |
FOLD_ARGS, FOLD_ARGS_TO_VOID, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
76 |
PERMUTE_ARGS |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
77 |
//maybe add more for guard with test, catch exception, pointwise type conversions |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
78 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
79 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
80 |
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
|
81 |
private static final int |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
82 |
PACKED_BYTE_SIZE = (STRESS_TEST ? 2 : 4), |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
83 |
PACKED_BYTE_MASK = (1 << PACKED_BYTE_SIZE) - 1, |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
84 |
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
|
85 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
86 |
private static long packedBytes(byte[] bytes) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
87 |
if (bytes.length > PACKED_BYTE_MAX_LENGTH) return 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
88 |
long pb = 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
89 |
int bitset = 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
90 |
for (int i = 0; i < bytes.length; i++) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
91 |
int b = bytes[i] & 0xFF; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
92 |
bitset |= b; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
93 |
pb |= (long)b << (i * PACKED_BYTE_SIZE); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
94 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
95 |
if (!inRange(bitset)) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
96 |
return 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
97 |
return pb; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
98 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
99 |
private static long packedBytes(int b0, int b1) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
100 |
assert(inRange(b0 | b1)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
101 |
return ( (b0 << 0*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
102 |
| (b1 << 1*PACKED_BYTE_SIZE)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
103 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
104 |
private static long packedBytes(int b0, int b1, int b2) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
105 |
assert(inRange(b0 | b1 | b2)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
106 |
return ( (b0 << 0*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
107 |
| (b1 << 1*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
108 |
| (b2 << 2*PACKED_BYTE_SIZE)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
109 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
110 |
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
|
111 |
assert(inRange(b0 | b1 | b2 | b3)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
112 |
return ( (b0 << 0*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
113 |
| (b1 << 1*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
114 |
| (b2 << 2*PACKED_BYTE_SIZE) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
115 |
| (b3 << 3*PACKED_BYTE_SIZE)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
116 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
117 |
private static boolean inRange(int bitset) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
118 |
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
|
119 |
return ((bitset & ~PACKED_BYTE_MASK) == 0); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
120 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
121 |
private static byte[] fullBytes(int... byteValues) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
122 |
byte[] bytes = new byte[byteValues.length]; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
123 |
int i = 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
124 |
for (int bv : byteValues) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
125 |
bytes[i++] = bval(bv); |
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 |
assert(packedBytes(bytes) == 0); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
128 |
return bytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
129 |
} |
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 byte byteAt(int i) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
132 |
long pb = packedBytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
133 |
if (pb == 0) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
134 |
if (i >= fullBytes.length) return 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
135 |
return fullBytes[i]; |
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(fullBytes == null); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
138 |
if (i > PACKED_BYTE_MAX_LENGTH) return 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
139 |
int pos = (i * PACKED_BYTE_SIZE); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
140 |
return (byte)((pb >>> pos) & PACKED_BYTE_MASK); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
141 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
142 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
143 |
Kind kind() { return Kind.values()[byteAt(0)]; } |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
144 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
145 |
private Transform(long packedBytes, byte[] fullBytes, LambdaForm result) { |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
146 |
super(result); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
147 |
this.packedBytes = packedBytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
148 |
this.fullBytes = fullBytes; |
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(long packedBytes) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
151 |
this(packedBytes, null, null); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
152 |
assert(packedBytes != 0); |
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 Transform(byte[] fullBytes) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
155 |
this(0, fullBytes, null); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
156 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
157 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
158 |
private static byte bval(int b) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
159 |
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
|
160 |
return (byte)b; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
161 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
162 |
private static byte bval(Kind k) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
163 |
return bval(k.ordinal()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
164 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
165 |
static Transform of(Kind k, int b1) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
166 |
byte b0 = bval(k); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
167 |
if (inRange(b0 | b1)) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
168 |
return new Transform(packedBytes(b0, b1)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
169 |
else |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
170 |
return new Transform(fullBytes(b0, b1)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
171 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
172 |
static Transform of(Kind k, int b1, int b2) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
173 |
byte b0 = (byte) k.ordinal(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
174 |
if (inRange(b0 | b1 | b2)) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
175 |
return new Transform(packedBytes(b0, b1, b2)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
176 |
else |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
177 |
return new Transform(fullBytes(b0, b1, b2)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
178 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
179 |
static Transform of(Kind k, int b1, int b2, int b3) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
180 |
byte b0 = (byte) k.ordinal(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
181 |
if (inRange(b0 | b1 | b2 | b3)) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
182 |
return new Transform(packedBytes(b0, b1, b2, b3)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
183 |
else |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
184 |
return new Transform(fullBytes(b0, b1, b2, b3)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
185 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
186 |
private static final byte[] NO_BYTES = {}; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
187 |
static Transform of(Kind k, int... b123) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
188 |
return ofBothArrays(k, b123, NO_BYTES); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
189 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
190 |
static Transform of(Kind k, int b1, byte[] b234) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
191 |
return ofBothArrays(k, new int[]{ b1 }, b234); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
192 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
193 |
static Transform of(Kind k, int b1, int b2, byte[] b345) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
194 |
return ofBothArrays(k, new int[]{ b1, b2 }, b345); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
195 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
196 |
private static Transform ofBothArrays(Kind k, int[] b123, byte[] b456) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
197 |
byte[] fullBytes = new byte[1 + b123.length + b456.length]; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
198 |
int i = 0; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
199 |
fullBytes[i++] = bval(k); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
200 |
for (int bv : b123) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
201 |
fullBytes[i++] = bval(bv); |
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 |
for (byte bv : b456) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
204 |
fullBytes[i++] = bv; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
205 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
206 |
long packedBytes = packedBytes(fullBytes); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
207 |
if (packedBytes != 0) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
208 |
return new Transform(packedBytes); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
209 |
else |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
210 |
return new Transform(fullBytes); |
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 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
213 |
Transform withResult(LambdaForm result) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
214 |
return new Transform(this.packedBytes, this.fullBytes, result); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
215 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
216 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
217 |
@Override |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
218 |
public boolean equals(Object obj) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
219 |
return obj instanceof Transform && equals((Transform)obj); |
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 |
public boolean equals(Transform that) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
222 |
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
|
223 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
224 |
@Override |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
225 |
public int hashCode() { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
226 |
if (packedBytes != 0) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
227 |
assert(fullBytes == null); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
228 |
return Long.hashCode(packedBytes); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
229 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
230 |
return Arrays.hashCode(fullBytes); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
231 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
232 |
@Override |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
233 |
public String toString() { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
234 |
StringBuilder buf = new StringBuilder(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
235 |
long bits = packedBytes; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
236 |
if (bits != 0) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
237 |
buf.append("("); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
238 |
while (bits != 0) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
239 |
buf.append(bits & PACKED_BYTE_MASK); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
240 |
bits >>>= PACKED_BYTE_SIZE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
241 |
if (bits != 0) buf.append(","); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
242 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
243 |
buf.append(")"); |
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 |
if (fullBytes != null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
246 |
buf.append("unpacked"); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
247 |
buf.append(Arrays.toString(fullBytes)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
248 |
} |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
249 |
LambdaForm result = get(); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
250 |
if (result != null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
251 |
buf.append(" result="); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
252 |
buf.append(result); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
253 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
254 |
return buf.toString(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
255 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
256 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
257 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
258 |
/** 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
|
259 |
private LambdaForm getInCache(Transform key) { |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
260 |
assert(key.get() == null); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
261 |
// The transformCache is one of null, Transform, Transform[], or ConcurrentHashMap. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
262 |
Object c = lambdaForm.transformCache; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
263 |
Transform k = null; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
264 |
if (c instanceof ConcurrentHashMap) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
265 |
@SuppressWarnings("unchecked") |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
266 |
ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
267 |
k = m.get(key); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
268 |
} else if (c == null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
269 |
return null; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
270 |
} else if (c instanceof Transform) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
271 |
// one-element cache avoids overhead of an array |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
272 |
Transform t = (Transform)c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
273 |
if (t.equals(key)) k = t; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
274 |
} else { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
275 |
Transform[] ta = (Transform[])c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
276 |
for (int i = 0; i < ta.length; i++) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
277 |
Transform t = ta[i]; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
278 |
if (t == null) break; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
279 |
if (t.equals(key)) { k = t; break; } |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
280 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
281 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
282 |
assert(k == null || key.equals(k)); |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
283 |
return (k != null) ? k.get() : null; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
284 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
285 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
286 |
/** Arbitrary but reasonable limits on Transform[] size for cache. */ |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
287 |
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
|
288 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
289 |
/** Cache a transform with its result, and return that result. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
290 |
* 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
|
291 |
*/ |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
292 |
private LambdaForm putInCache(Transform key, LambdaForm form) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
293 |
key = key.withResult(form); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
294 |
for (int pass = 0; ; pass++) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
295 |
Object c = lambdaForm.transformCache; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
296 |
if (c instanceof ConcurrentHashMap) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
297 |
@SuppressWarnings("unchecked") |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
298 |
ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
299 |
Transform k = m.putIfAbsent(key, key); |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
300 |
if (k == null) return form; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
301 |
LambdaForm result = k.get(); |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
302 |
if (result != null) { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
303 |
return result; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
304 |
} else { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
305 |
if (m.replace(key, k, key)) { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
306 |
return form; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
307 |
} else { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
308 |
continue; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
309 |
} |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
310 |
} |
26481
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 |
assert(pass == 0); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
313 |
synchronized (lambdaForm) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
314 |
c = lambdaForm.transformCache; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
315 |
if (c instanceof ConcurrentHashMap) |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
316 |
continue; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
317 |
if (c == null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
318 |
lambdaForm.transformCache = key; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
319 |
return form; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
320 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
321 |
Transform[] ta; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
322 |
if (c instanceof Transform) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
323 |
Transform k = (Transform)c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
324 |
if (k.equals(key)) { |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
325 |
LambdaForm result = k.get(); |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
326 |
if (result == null) { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
327 |
lambdaForm.transformCache = key; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
328 |
return form; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
329 |
} else { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
330 |
return result; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
331 |
} |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
332 |
} else if (k.get() == null) { // overwrite stale entry |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
333 |
lambdaForm.transformCache = key; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
334 |
return form; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
335 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
336 |
// expand one-element cache to small array |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
337 |
ta = new Transform[MIN_CACHE_ARRAY_SIZE]; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
338 |
ta[0] = k; |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
339 |
lambdaForm.transformCache = ta; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
340 |
} else { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
341 |
// it is already expanded |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
342 |
ta = (Transform[])c; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
343 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
344 |
int len = ta.length; |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
345 |
int stale = -1; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
346 |
int i; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
347 |
for (i = 0; i < len; i++) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
348 |
Transform k = ta[i]; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
349 |
if (k == null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
350 |
break; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
351 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
352 |
if (k.equals(key)) { |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
353 |
LambdaForm result = k.get(); |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
354 |
if (result == null) { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
355 |
ta[i] = key; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
356 |
return form; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
357 |
} else { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
358 |
return result; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
359 |
} |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
360 |
} else if (stale < 0 && k.get() == null) { |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
361 |
stale = i; // remember 1st stale entry index |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
362 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
363 |
} |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
364 |
if (i < len || stale >= 0) { |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
365 |
// just fall through to cache update |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
366 |
} else if (len < MAX_CACHE_ARRAY_SIZE) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
367 |
len = Math.min(len * 2, MAX_CACHE_ARRAY_SIZE); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
368 |
ta = Arrays.copyOf(ta, len); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
369 |
lambdaForm.transformCache = ta; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
370 |
} else { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
371 |
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
|
372 |
for (Transform k : ta) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
373 |
m.put(k, k); |
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 |
lambdaForm.transformCache = m; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
376 |
// The second iteration will update for this query, concurrently. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
377 |
continue; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
378 |
} |
27803
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
379 |
int idx = (stale >= 0) ? stale : i; |
d04ca9d519ce
8057020: LambdaForm caches should support eviction
vlivanov
parents:
26482
diff
changeset
|
380 |
ta[idx] = key; |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
381 |
return form; |
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 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
384 |
} |
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 LambdaFormBuffer buffer() { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
387 |
return new LambdaFormBuffer(lambdaForm); |
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 |
/// 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
|
391 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
392 |
private BoundMethodHandle.SpeciesData oldSpeciesData() { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
393 |
return BoundMethodHandle.speciesData(lambdaForm); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
394 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
395 |
private BoundMethodHandle.SpeciesData newSpeciesData(BasicType type) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
396 |
return oldSpeciesData().extendWith(type); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
397 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
398 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
399 |
BoundMethodHandle bindArgumentL(BoundMethodHandle mh, int pos, Object value) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
400 |
assert(mh.speciesData() == oldSpeciesData()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
401 |
BasicType bt = L_TYPE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
402 |
MethodType type2 = bindArgumentType(mh, pos, bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
403 |
LambdaForm form2 = bindArgumentForm(1+pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
404 |
return mh.copyWithExtendL(type2, form2, value); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
405 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
406 |
BoundMethodHandle bindArgumentI(BoundMethodHandle mh, int pos, int value) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
407 |
assert(mh.speciesData() == oldSpeciesData()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
408 |
BasicType bt = I_TYPE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
409 |
MethodType type2 = bindArgumentType(mh, pos, bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
410 |
LambdaForm form2 = bindArgumentForm(1+pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
411 |
return mh.copyWithExtendI(type2, form2, value); |
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 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
414 |
BoundMethodHandle bindArgumentJ(BoundMethodHandle mh, int pos, long value) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
415 |
assert(mh.speciesData() == oldSpeciesData()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
416 |
BasicType bt = J_TYPE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
417 |
MethodType type2 = bindArgumentType(mh, pos, bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
418 |
LambdaForm form2 = bindArgumentForm(1+pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
419 |
return mh.copyWithExtendJ(type2, form2, value); |
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 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
422 |
BoundMethodHandle bindArgumentF(BoundMethodHandle mh, int pos, float value) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
423 |
assert(mh.speciesData() == oldSpeciesData()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
424 |
BasicType bt = F_TYPE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
425 |
MethodType type2 = bindArgumentType(mh, pos, bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
426 |
LambdaForm form2 = bindArgumentForm(1+pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
427 |
return mh.copyWithExtendF(type2, form2, value); |
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 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
430 |
BoundMethodHandle bindArgumentD(BoundMethodHandle mh, int pos, double value) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
431 |
assert(mh.speciesData() == oldSpeciesData()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
432 |
BasicType bt = D_TYPE; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
433 |
MethodType type2 = bindArgumentType(mh, pos, bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
434 |
LambdaForm form2 = bindArgumentForm(1+pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
435 |
return mh.copyWithExtendD(type2, form2, value); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
436 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
437 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
438 |
private MethodType bindArgumentType(BoundMethodHandle mh, int pos, BasicType bt) { |
29221
e946919cf487
8073644: Assertion in LambdaFormEditor.bindArgumentType is too strict
vlivanov
parents:
29020
diff
changeset
|
439 |
assert(mh.form.uncustomize() == lambdaForm); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
440 |
assert(mh.form.names[1+pos].type == bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
441 |
assert(BasicType.basicType(mh.type().parameterType(pos)) == bt); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
442 |
return mh.type().dropParameterTypes(pos, pos+1); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
443 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
444 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
445 |
/// Editing methods for lambda forms. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
446 |
// 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
|
447 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
448 |
LambdaForm bindArgumentForm(int pos) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
449 |
Transform key = Transform.of(Transform.Kind.BIND_ARG, pos); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
450 |
LambdaForm form = getInCache(key); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
451 |
if (form != null) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
452 |
assert(form.parameterConstraint(0) == newSpeciesData(lambdaForm.parameterType(pos))); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
453 |
return form; |
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 |
LambdaFormBuffer buf = buffer(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
456 |
buf.startEdit(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
457 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
458 |
BoundMethodHandle.SpeciesData oldData = oldSpeciesData(); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
459 |
BoundMethodHandle.SpeciesData newData = newSpeciesData(lambdaForm.parameterType(pos)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
460 |
Name oldBaseAddress = lambdaForm.parameter(0); // BMH holding the values |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
461 |
Name newBaseAddress; |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
462 |
NamedFunction getter = newData.getterFunction(oldData.fieldCount()); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
463 |
|
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
464 |
if (pos != 0) { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
465 |
// The newly created LF will run with a different BMH. |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
466 |
// 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
|
467 |
buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
468 |
newBaseAddress = oldBaseAddress.withConstraint(newData); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
469 |
buf.renameParameter(0, newBaseAddress); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
470 |
buf.replaceParameterByNewExpression(pos, new Name(getter, newBaseAddress)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
471 |
} else { |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
472 |
// cannot bind the MH arg itself, unless oldData is empty |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
473 |
assert(oldData == BoundMethodHandle.SpeciesData.EMPTY); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
474 |
newBaseAddress = new Name(L_TYPE).withConstraint(newData); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
475 |
buf.replaceParameterByNewExpression(0, new Name(getter, newBaseAddress)); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
476 |
buf.insertParameter(0, newBaseAddress); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
477 |
} |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
478 |
|
26482
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
479 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
480 |
return putInCache(key, 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 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
483 |
LambdaForm addArgumentForm(int pos, BasicType type) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
484 |
Transform key = Transform.of(Transform.Kind.ADD_ARG, pos, type.ordinal()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
485 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
486 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
487 |
assert(form.arity == lambdaForm.arity+1); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
488 |
assert(form.parameterType(pos) == type); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
489 |
return form; |
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 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
492 |
buf.startEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
493 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
494 |
buf.insertParameter(pos, new Name(type)); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
495 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
496 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
497 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
498 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
499 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
500 |
LambdaForm dupArgumentForm(int srcPos, int dstPos) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
501 |
Transform key = Transform.of(Transform.Kind.DUP_ARG, srcPos, dstPos); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
502 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
503 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
504 |
assert(form.arity == lambdaForm.arity-1); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
505 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
506 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
507 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
508 |
buf.startEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
509 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
510 |
assert(lambdaForm.parameter(srcPos).constraint == null); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
511 |
assert(lambdaForm.parameter(dstPos).constraint == null); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
512 |
buf.replaceParameterByCopy(dstPos, srcPos); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
513 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
514 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
515 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
516 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
517 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
518 |
LambdaForm spreadArgumentsForm(int pos, Class<?> arrayType, int arrayLength) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
519 |
Class<?> elementType = arrayType.getComponentType(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
520 |
Class<?> erasedArrayType = arrayType; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
521 |
if (!elementType.isPrimitive()) |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
522 |
erasedArrayType = Object[].class; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
523 |
BasicType bt = basicType(elementType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
524 |
int elementTypeKey = bt.ordinal(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
525 |
if (bt.basicTypeClass() != elementType) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
526 |
if (elementType.isPrimitive()) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
527 |
elementTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
528 |
} |
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 |
Transform key = Transform.of(Transform.Kind.SPREAD_ARGS, pos, elementTypeKey, arrayLength); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
531 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
532 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
533 |
assert(form.arity == lambdaForm.arity - arrayLength + 1); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
534 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
535 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
536 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
537 |
buf.startEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
538 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
539 |
assert(pos <= MethodType.MAX_JVM_ARITY); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
540 |
assert(pos + arrayLength <= lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
541 |
assert(pos > 0); // cannot spread the MH arg itself |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
542 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
543 |
Name spreadParam = new Name(L_TYPE); |
33841
1d1d1ea6a5f2
8142334: Improve lazy initialization of java.lang.invoke
redestad
parents:
29221
diff
changeset
|
544 |
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
|
545 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
546 |
// insert the new expressions |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
547 |
int exprPos = lambdaForm.arity(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
548 |
buf.insertExpression(exprPos++, checkSpread); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
549 |
// adjust the arguments |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
550 |
MethodHandle aload = MethodHandles.arrayElementGetter(erasedArrayType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
551 |
for (int i = 0; i < arrayLength; i++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
552 |
Name loadArgument = new Name(aload, spreadParam, i); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
553 |
buf.insertExpression(exprPos + i, loadArgument); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
554 |
buf.replaceParameterByCopy(pos + i, exprPos + i); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
555 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
556 |
buf.insertParameter(pos, spreadParam); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
557 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
558 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
559 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
560 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
561 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
562 |
LambdaForm collectArgumentsForm(int pos, MethodType collectorType) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
563 |
int collectorArity = collectorType.parameterCount(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
564 |
boolean dropResult = (collectorType.returnType() == void.class); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
565 |
if (collectorArity == 1 && !dropResult) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
566 |
return filterArgumentForm(pos, basicType(collectorType.parameterType(0))); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
567 |
} |
34384
439c06c76808
8143131: Remove unused code from java.lang.invoke
redestad
parents:
33841
diff
changeset
|
568 |
byte[] newTypes = BasicType.basicTypesOrd(collectorType.parameterArray()); |
26482
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
569 |
Transform.Kind kind = (dropResult |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
570 |
? Transform.Kind.COLLECT_ARGS_TO_VOID |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
571 |
: Transform.Kind.COLLECT_ARGS); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
572 |
if (dropResult && collectorArity == 0) pos = 1; // pure side effect |
34384
439c06c76808
8143131: Remove unused code from java.lang.invoke
redestad
parents:
33841
diff
changeset
|
573 |
Transform key = Transform.of(kind, pos, collectorArity, newTypes); |
26482
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
574 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
575 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
576 |
assert(form.arity == lambdaForm.arity - (dropResult ? 0 : 1) + collectorArity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
577 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
578 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
579 |
form = makeArgumentCombinationForm(pos, collectorType, false, dropResult); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
580 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
581 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
582 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
583 |
LambdaForm collectArgumentArrayForm(int pos, MethodHandle arrayCollector) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
584 |
MethodType collectorType = arrayCollector.type(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
585 |
int collectorArity = collectorType.parameterCount(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
586 |
assert(arrayCollector.intrinsicName() == Intrinsic.NEW_ARRAY); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
587 |
Class<?> arrayType = collectorType.returnType(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
588 |
Class<?> elementType = arrayType.getComponentType(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
589 |
BasicType argType = basicType(elementType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
590 |
int argTypeKey = argType.ordinal(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
591 |
if (argType.basicTypeClass() != elementType) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
592 |
// 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
|
593 |
if (!elementType.isPrimitive()) |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
594 |
return null; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
595 |
argTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
596 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
597 |
assert(collectorType.parameterList().equals(Collections.nCopies(collectorArity, elementType))); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
598 |
Transform.Kind kind = Transform.Kind.COLLECT_ARGS_TO_ARRAY; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
599 |
Transform key = Transform.of(kind, pos, collectorArity, argTypeKey); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
600 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
601 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
602 |
assert(form.arity == lambdaForm.arity - 1 + collectorArity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
603 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
604 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
605 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
606 |
buf.startEdit(); |
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 |
assert(pos + 1 <= lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
609 |
assert(pos > 0); // cannot filter the MH arg itself |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
610 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
611 |
Name[] newParams = new Name[collectorArity]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
612 |
for (int i = 0; i < collectorArity; i++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
613 |
newParams[i] = new Name(pos + i, argType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
614 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
615 |
Name callCombiner = new Name(arrayCollector, (Object[]) /*...*/ newParams); |
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 |
// insert the new expression |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
618 |
int exprPos = lambdaForm.arity(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
619 |
buf.insertExpression(exprPos, callCombiner); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
620 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
621 |
// insert new arguments |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
622 |
int argPos = pos + 1; // skip result parameter |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
623 |
for (Name newParam : newParams) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
624 |
buf.insertParameter(argPos++, newParam); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
625 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
626 |
assert(buf.lastIndexOf(callCombiner) == exprPos+newParams.length); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
627 |
buf.replaceParameterByCopy(pos, exprPos+newParams.length); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
628 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
629 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
630 |
return putInCache(key, 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 |
LambdaForm filterArgumentForm(int pos, BasicType newType) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
634 |
Transform key = Transform.of(Transform.Kind.FILTER_ARG, pos, newType.ordinal()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
635 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
636 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
637 |
assert(form.arity == lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
638 |
assert(form.parameterType(pos) == newType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
639 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
640 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
641 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
642 |
BasicType oldType = lambdaForm.parameterType(pos); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
643 |
MethodType filterType = MethodType.methodType(oldType.basicTypeClass(), |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
644 |
newType.basicTypeClass()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
645 |
form = makeArgumentCombinationForm(pos, filterType, false, false); |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
646 |
return putInCache(key, form); |
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
647 |
} |
26482
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
648 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
649 |
private LambdaForm makeArgumentCombinationForm(int pos, |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
650 |
MethodType combinerType, |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
651 |
boolean keepArguments, boolean dropResult) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
652 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
653 |
buf.startEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
654 |
int combinerArity = combinerType.parameterCount(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
655 |
int resultArity = (dropResult ? 0 : 1); |
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 |
assert(pos <= MethodType.MAX_JVM_ARITY); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
658 |
assert(pos + resultArity + (keepArguments ? combinerArity : 0) <= lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
659 |
assert(pos > 0); // cannot filter the MH arg itself |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
660 |
assert(combinerType == combinerType.basicType()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
661 |
assert(combinerType.returnType() != void.class || dropResult); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
662 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
663 |
BoundMethodHandle.SpeciesData oldData = oldSpeciesData(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
664 |
BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
665 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
666 |
// 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
|
667 |
// 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
|
668 |
Name oldBaseAddress = lambdaForm.parameter(0); // BMH holding the values |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
669 |
buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
670 |
Name newBaseAddress = oldBaseAddress.withConstraint(newData); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
671 |
buf.renameParameter(0, newBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
672 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
673 |
Name getCombiner = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
674 |
Object[] combinerArgs = new Object[1 + combinerArity]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
675 |
combinerArgs[0] = getCombiner; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
676 |
Name[] newParams; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
677 |
if (keepArguments) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
678 |
newParams = new Name[0]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
679 |
System.arraycopy(lambdaForm.names, pos + resultArity, |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
680 |
combinerArgs, 1, combinerArity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
681 |
} else { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
682 |
newParams = new Name[combinerArity]; |
34384
439c06c76808
8143131: Remove unused code from java.lang.invoke
redestad
parents:
33841
diff
changeset
|
683 |
for (int i = 0; i < newParams.length; i++) { |
439c06c76808
8143131: Remove unused code from java.lang.invoke
redestad
parents:
33841
diff
changeset
|
684 |
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
|
685 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
686 |
System.arraycopy(newParams, 0, |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
687 |
combinerArgs, 1, combinerArity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
688 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
689 |
Name callCombiner = new Name(combinerType, combinerArgs); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
690 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
691 |
// insert the two new expressions |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
692 |
int exprPos = lambdaForm.arity(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
693 |
buf.insertExpression(exprPos+0, getCombiner); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
694 |
buf.insertExpression(exprPos+1, callCombiner); |
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 |
// insert new arguments, if needed |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
697 |
int argPos = pos + resultArity; // skip result parameter |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
698 |
for (Name newParam : newParams) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
699 |
buf.insertParameter(argPos++, newParam); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
700 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
701 |
assert(buf.lastIndexOf(callCombiner) == exprPos+1+newParams.length); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
702 |
if (!dropResult) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
703 |
buf.replaceParameterByCopy(pos, exprPos+1+newParams.length); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
704 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
705 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
706 |
return buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
707 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
708 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
709 |
LambdaForm filterReturnForm(BasicType newType, boolean constantZero) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
710 |
Transform.Kind kind = (constantZero ? Transform.Kind.FILTER_RETURN_TO_ZERO : Transform.Kind.FILTER_RETURN); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
711 |
Transform key = Transform.of(kind, newType.ordinal()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
712 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
713 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
714 |
assert(form.arity == lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
715 |
assert(form.returnType() == newType); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
716 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
717 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
718 |
LambdaFormBuffer buf = buffer(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
719 |
buf.startEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
720 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
721 |
int insPos = lambdaForm.names.length; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
722 |
Name callFilter; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
723 |
if (constantZero) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
724 |
// Synthesize a constant zero value for the given type. |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
725 |
if (newType == V_TYPE) |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
726 |
callFilter = null; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
727 |
else |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
728 |
callFilter = new Name(constantZero(newType)); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
729 |
} else { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
730 |
BoundMethodHandle.SpeciesData oldData = oldSpeciesData(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
731 |
BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
732 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
733 |
// 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
|
734 |
// 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
|
735 |
Name oldBaseAddress = lambdaForm.parameter(0); // BMH holding the values |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
736 |
buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
737 |
Name newBaseAddress = oldBaseAddress.withConstraint(newData); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
738 |
buf.renameParameter(0, newBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
739 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
740 |
Name getFilter = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
741 |
buf.insertExpression(insPos++, getFilter); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
742 |
BasicType oldType = lambdaForm.returnType(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
743 |
if (oldType == V_TYPE) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
744 |
MethodType filterType = MethodType.methodType(newType.basicTypeClass()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
745 |
callFilter = new Name(filterType, getFilter); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
746 |
} else { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
747 |
MethodType filterType = MethodType.methodType(newType.basicTypeClass(), oldType.basicTypeClass()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
748 |
callFilter = new Name(filterType, getFilter, lambdaForm.names[lambdaForm.result]); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
749 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
750 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
751 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
752 |
if (callFilter != null) |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
753 |
buf.insertExpression(insPos++, callFilter); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
754 |
buf.setResult(callFilter); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
755 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
756 |
form = buf.endEdit(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
757 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
758 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
759 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
760 |
LambdaForm foldArgumentsForm(int foldPos, boolean dropResult, MethodType combinerType) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
761 |
int combinerArity = combinerType.parameterCount(); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
762 |
Transform.Kind kind = (dropResult ? Transform.Kind.FOLD_ARGS_TO_VOID : Transform.Kind.FOLD_ARGS); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
763 |
Transform key = Transform.of(kind, foldPos, combinerArity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
764 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
765 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
766 |
assert(form.arity == lambdaForm.arity - (kind == Transform.Kind.FOLD_ARGS ? 1 : 0)); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
767 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
768 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
769 |
form = makeArgumentCombinationForm(foldPos, combinerType, true, dropResult); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
770 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
771 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
772 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
773 |
LambdaForm permuteArgumentsForm(int skip, int[] reorder) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
774 |
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
|
775 |
int length = lambdaForm.names.length; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
776 |
int outArgs = reorder.length; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
777 |
int inTypes = 0; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
778 |
boolean nullPerm = true; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
779 |
for (int i = 0; i < reorder.length; i++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
780 |
int inArg = reorder[i]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
781 |
if (inArg != i) nullPerm = false; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
782 |
inTypes = Math.max(inTypes, inArg+1); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
783 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
784 |
assert(skip + reorder.length == lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
785 |
if (nullPerm) return lambdaForm; // do not bother to cache |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
786 |
Transform key = Transform.of(Transform.Kind.PERMUTE_ARGS, reorder); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
787 |
LambdaForm form = getInCache(key); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
788 |
if (form != null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
789 |
assert(form.arity == skip+inTypes) : form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
790 |
return form; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
791 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
792 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
793 |
BasicType[] types = new BasicType[inTypes]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
794 |
for (int i = 0; i < outArgs; i++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
795 |
int inArg = reorder[i]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
796 |
types[inArg] = lambdaForm.names[skip + i].type; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
797 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
798 |
assert (skip + outArgs == lambdaForm.arity); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
799 |
assert (permutedTypesMatch(reorder, types, lambdaForm.names, skip)); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
800 |
int pos = 0; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
801 |
while (pos < outArgs && reorder[pos] == pos) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
802 |
pos += 1; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
803 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
804 |
Name[] names2 = new Name[length - outArgs + inTypes]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
805 |
System.arraycopy(lambdaForm.names, 0, names2, 0, skip + pos); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
806 |
int bodyLength = length - lambdaForm.arity; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
807 |
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
|
808 |
int arity2 = names2.length - bodyLength; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
809 |
int result2 = lambdaForm.result; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
810 |
if (result2 >= 0) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
811 |
if (result2 < skip + outArgs) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
812 |
result2 = reorder[result2 - skip]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
813 |
} else { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
814 |
result2 = result2 - outArgs + inTypes; |
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 |
for (int j = pos; j < outArgs; j++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
818 |
Name n = lambdaForm.names[skip + j]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
819 |
int i = reorder[j]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
820 |
Name n2 = names2[skip + i]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
821 |
if (n2 == null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
822 |
names2[skip + i] = n2 = new Name(types[i]); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
823 |
} else { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
824 |
assert (n2.type == types[i]); |
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 |
for (int k = arity2; k < names2.length; k++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
827 |
names2[k] = names2[k].replaceName(n, n2); |
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 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
830 |
for (int i = skip + pos; i < arity2; i++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
831 |
if (names2[i] == null) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
832 |
names2[i] = argument(i, types[i - skip]); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
833 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
834 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
835 |
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
|
836 |
int i = j - lambdaForm.arity + arity2; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
837 |
Name n = lambdaForm.names[j]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
838 |
Name n2 = names2[i]; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
839 |
if (n != n2) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
840 |
for (int k = i + 1; k < names2.length; k++) { |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
841 |
names2[k] = names2[k].replaceName(n, n2); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
842 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
843 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
844 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
845 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
846 |
form = new LambdaForm(lambdaForm.debugName, arity2, names2, result2); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
847 |
return putInCache(key, form); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
848 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
849 |
|
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
850 |
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
|
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 |
assert (names[skip + i].isParam()); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
853 |
assert (names[skip + i].type == types[reorder[i]]); |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
854 |
} |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
855 |
return true; |
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26481
diff
changeset
|
856 |
} |
26481
c5b74a88a3c0
8057042: LambdaFormEditor: derive new LFs from a base LF
vlivanov
parents:
diff
changeset
|
857 |
} |