jaroslav@1646
|
1 |
/*
|
jaroslav@1646
|
2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@1646
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1646
|
4 |
*
|
jaroslav@1646
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1646
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1646
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1646
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1646
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1646
|
10 |
*
|
jaroslav@1646
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1646
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1646
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1646
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1646
|
15 |
* accompanied this code).
|
jaroslav@1646
|
16 |
*
|
jaroslav@1646
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1646
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1646
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1646
|
20 |
*
|
jaroslav@1646
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1646
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1646
|
23 |
* questions.
|
jaroslav@1646
|
24 |
*/
|
jaroslav@1646
|
25 |
|
jaroslav@1646
|
26 |
package java.lang.invoke;
|
jaroslav@1646
|
27 |
|
jaroslav@1646
|
28 |
import java.lang.annotation.*;
|
jaroslav@1646
|
29 |
import java.lang.reflect.Method;
|
jaroslav@1646
|
30 |
import java.util.Map;
|
jaroslav@1646
|
31 |
import java.util.List;
|
jaroslav@1646
|
32 |
import java.util.Arrays;
|
jaroslav@1646
|
33 |
import java.util.ArrayList;
|
jaroslav@1646
|
34 |
import java.util.HashMap;
|
jaroslav@1646
|
35 |
import java.util.concurrent.ConcurrentHashMap;
|
jaroslav@1646
|
36 |
import sun.invoke.util.Wrapper;
|
jaroslav@1646
|
37 |
import static java.lang.invoke.MethodHandleStatics.*;
|
jaroslav@1646
|
38 |
import static java.lang.invoke.MethodHandleNatives.Constants.*;
|
jaroslav@1646
|
39 |
import java.lang.reflect.Field;
|
jaroslav@1646
|
40 |
import java.util.Objects;
|
jaroslav@1646
|
41 |
|
jaroslav@1646
|
42 |
/**
|
jaroslav@1646
|
43 |
* The symbolic, non-executable form of a method handle's invocation semantics.
|
jaroslav@1646
|
44 |
* It consists of a series of names.
|
jaroslav@1646
|
45 |
* The first N (N=arity) names are parameters,
|
jaroslav@1646
|
46 |
* while any remaining names are temporary values.
|
jaroslav@1646
|
47 |
* Each temporary specifies the application of a function to some arguments.
|
jaroslav@1646
|
48 |
* The functions are method handles, while the arguments are mixes of
|
jaroslav@1646
|
49 |
* constant values and local names.
|
jaroslav@1646
|
50 |
* The result of the lambda is defined as one of the names, often the last one.
|
jaroslav@1646
|
51 |
* <p>
|
jaroslav@1646
|
52 |
* Here is an approximate grammar:
|
jaroslav@1646
|
53 |
* <blockquote><pre>{@code
|
jaroslav@1646
|
54 |
* LambdaForm = "(" ArgName* ")=>{" TempName* Result "}"
|
jaroslav@1646
|
55 |
* ArgName = "a" N ":" T
|
jaroslav@1646
|
56 |
* TempName = "t" N ":" T "=" Function "(" Argument* ");"
|
jaroslav@1646
|
57 |
* Function = ConstantValue
|
jaroslav@1646
|
58 |
* Argument = NameRef | ConstantValue
|
jaroslav@1646
|
59 |
* Result = NameRef | "void"
|
jaroslav@1646
|
60 |
* NameRef = "a" N | "t" N
|
jaroslav@1646
|
61 |
* N = (any whole number)
|
jaroslav@1646
|
62 |
* T = "L" | "I" | "J" | "F" | "D" | "V"
|
jaroslav@1646
|
63 |
* }</pre></blockquote>
|
jaroslav@1646
|
64 |
* Names are numbered consecutively from left to right starting at zero.
|
jaroslav@1646
|
65 |
* (The letters are merely a taste of syntax sugar.)
|
jaroslav@1646
|
66 |
* Thus, the first temporary (if any) is always numbered N (where N=arity).
|
jaroslav@1646
|
67 |
* Every occurrence of a name reference in an argument list must refer to
|
jaroslav@1646
|
68 |
* a name previously defined within the same lambda.
|
jaroslav@1646
|
69 |
* A lambda has a void result if and only if its result index is -1.
|
jaroslav@1646
|
70 |
* If a temporary has the type "V", it cannot be the subject of a NameRef,
|
jaroslav@1646
|
71 |
* even though possesses a number.
|
jaroslav@1646
|
72 |
* Note that all reference types are erased to "L", which stands for {@code Object}.
|
jaroslav@1646
|
73 |
* All subword types (boolean, byte, short, char) are erased to "I" which is {@code int}.
|
jaroslav@1646
|
74 |
* The other types stand for the usual primitive types.
|
jaroslav@1646
|
75 |
* <p>
|
jaroslav@1646
|
76 |
* Function invocation closely follows the static rules of the Java verifier.
|
jaroslav@1646
|
77 |
* Arguments and return values must exactly match when their "Name" types are
|
jaroslav@1646
|
78 |
* considered.
|
jaroslav@1646
|
79 |
* Conversions are allowed only if they do not change the erased type.
|
jaroslav@1646
|
80 |
* <ul>
|
jaroslav@1646
|
81 |
* <li>L = Object: casts are used freely to convert into and out of reference types
|
jaroslav@1646
|
82 |
* <li>I = int: subword types are forcibly narrowed when passed as arguments (see {@code explicitCastArguments})
|
jaroslav@1646
|
83 |
* <li>J = long: no implicit conversions
|
jaroslav@1646
|
84 |
* <li>F = float: no implicit conversions
|
jaroslav@1646
|
85 |
* <li>D = double: no implicit conversions
|
jaroslav@1646
|
86 |
* <li>V = void: a function result may be void if and only if its Name is of type "V"
|
jaroslav@1646
|
87 |
* </ul>
|
jaroslav@1646
|
88 |
* Although implicit conversions are not allowed, explicit ones can easily be
|
jaroslav@1646
|
89 |
* encoded by using temporary expressions which call type-transformed identity functions.
|
jaroslav@1646
|
90 |
* <p>
|
jaroslav@1646
|
91 |
* Examples:
|
jaroslav@1646
|
92 |
* <blockquote><pre>{@code
|
jaroslav@1646
|
93 |
* (a0:J)=>{ a0 }
|
jaroslav@1646
|
94 |
* == identity(long)
|
jaroslav@1646
|
95 |
* (a0:I)=>{ t1:V = System.out#println(a0); void }
|
jaroslav@1646
|
96 |
* == System.out#println(int)
|
jaroslav@1646
|
97 |
* (a0:L)=>{ t1:V = System.out#println(a0); a0 }
|
jaroslav@1646
|
98 |
* == identity, with printing side-effect
|
jaroslav@1646
|
99 |
* (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
|
jaroslav@1646
|
100 |
* t3:L = BoundMethodHandle#target(a0);
|
jaroslav@1646
|
101 |
* t4:L = MethodHandle#invoke(t3, t2, a1); t4 }
|
jaroslav@1646
|
102 |
* == general invoker for unary insertArgument combination
|
jaroslav@1646
|
103 |
* (a0:L, a1:L)=>{ t2:L = FilterMethodHandle#filter(a0);
|
jaroslav@1646
|
104 |
* t3:L = MethodHandle#invoke(t2, a1);
|
jaroslav@1646
|
105 |
* t4:L = FilterMethodHandle#target(a0);
|
jaroslav@1646
|
106 |
* t5:L = MethodHandle#invoke(t4, t3); t5 }
|
jaroslav@1646
|
107 |
* == general invoker for unary filterArgument combination
|
jaroslav@1646
|
108 |
* (a0:L, a1:L)=>{ ...(same as previous example)...
|
jaroslav@1646
|
109 |
* t5:L = MethodHandle#invoke(t4, t3, a1); t5 }
|
jaroslav@1646
|
110 |
* == general invoker for unary/unary foldArgument combination
|
jaroslav@1646
|
111 |
* (a0:L, a1:I)=>{ t2:I = identity(long).asType((int)->long)(a1); t2 }
|
jaroslav@1646
|
112 |
* == invoker for identity method handle which performs i2l
|
jaroslav@1646
|
113 |
* (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
|
jaroslav@1646
|
114 |
* t3:L = Class#cast(t2,a1); t3 }
|
jaroslav@1646
|
115 |
* == invoker for identity method handle which performs cast
|
jaroslav@1646
|
116 |
* }</pre></blockquote>
|
jaroslav@1646
|
117 |
* <p>
|
jaroslav@1646
|
118 |
* @author John Rose, JSR 292 EG
|
jaroslav@1646
|
119 |
*/
|
jaroslav@1646
|
120 |
class LambdaForm {
|
jaroslav@1646
|
121 |
final int arity;
|
jaroslav@1646
|
122 |
final int result;
|
jaroslav@1646
|
123 |
@Stable final Name[] names;
|
jaroslav@1646
|
124 |
final String debugName;
|
jaroslav@1646
|
125 |
MemberName vmentry; // low-level behavior, or null if not yet prepared
|
jaroslav@1646
|
126 |
private boolean isCompiled;
|
jaroslav@1646
|
127 |
|
jaroslav@1646
|
128 |
// Caches for common structural transforms:
|
jaroslav@1646
|
129 |
LambdaForm[] bindCache;
|
jaroslav@1646
|
130 |
|
jaroslav@1646
|
131 |
public static final int VOID_RESULT = -1, LAST_RESULT = -2;
|
jaroslav@1646
|
132 |
|
jaroslav@1646
|
133 |
LambdaForm(String debugName,
|
jaroslav@1646
|
134 |
int arity, Name[] names, int result) {
|
jaroslav@1646
|
135 |
assert(namesOK(arity, names));
|
jaroslav@1646
|
136 |
this.arity = arity;
|
jaroslav@1646
|
137 |
this.result = fixResult(result, names);
|
jaroslav@1646
|
138 |
this.names = names.clone();
|
jaroslav@1646
|
139 |
this.debugName = debugName;
|
jaroslav@1646
|
140 |
normalize();
|
jaroslav@1646
|
141 |
}
|
jaroslav@1646
|
142 |
|
jaroslav@1646
|
143 |
LambdaForm(String debugName,
|
jaroslav@1646
|
144 |
int arity, Name[] names) {
|
jaroslav@1646
|
145 |
this(debugName,
|
jaroslav@1646
|
146 |
arity, names, LAST_RESULT);
|
jaroslav@1646
|
147 |
}
|
jaroslav@1646
|
148 |
|
jaroslav@1646
|
149 |
LambdaForm(String debugName,
|
jaroslav@1646
|
150 |
Name[] formals, Name[] temps, Name result) {
|
jaroslav@1646
|
151 |
this(debugName,
|
jaroslav@1646
|
152 |
formals.length, buildNames(formals, temps, result), LAST_RESULT);
|
jaroslav@1646
|
153 |
}
|
jaroslav@1646
|
154 |
|
jaroslav@1646
|
155 |
private static Name[] buildNames(Name[] formals, Name[] temps, Name result) {
|
jaroslav@1646
|
156 |
int arity = formals.length;
|
jaroslav@1646
|
157 |
int length = arity + temps.length + (result == null ? 0 : 1);
|
jaroslav@1646
|
158 |
Name[] names = Arrays.copyOf(formals, length);
|
jaroslav@1646
|
159 |
System.arraycopy(temps, 0, names, arity, temps.length);
|
jaroslav@1646
|
160 |
if (result != null)
|
jaroslav@1646
|
161 |
names[length - 1] = result;
|
jaroslav@1646
|
162 |
return names;
|
jaroslav@1646
|
163 |
}
|
jaroslav@1646
|
164 |
|
jaroslav@1646
|
165 |
private LambdaForm(String sig) {
|
jaroslav@1646
|
166 |
// Make a blank lambda form, which returns a constant zero or null.
|
jaroslav@1646
|
167 |
// It is used as a template for managing the invocation of similar forms that are non-empty.
|
jaroslav@1646
|
168 |
// Called only from getPreparedForm.
|
jaroslav@1646
|
169 |
assert(isValidSignature(sig));
|
jaroslav@1646
|
170 |
this.arity = signatureArity(sig);
|
jaroslav@1646
|
171 |
this.result = (signatureReturn(sig) == 'V' ? -1 : arity);
|
jaroslav@1646
|
172 |
this.names = buildEmptyNames(arity, sig);
|
jaroslav@1646
|
173 |
this.debugName = "LF.zero";
|
jaroslav@1646
|
174 |
assert(nameRefsAreLegal());
|
jaroslav@1646
|
175 |
assert(isEmpty());
|
jaroslav@1646
|
176 |
assert(sig.equals(basicTypeSignature()));
|
jaroslav@1646
|
177 |
}
|
jaroslav@1646
|
178 |
|
jaroslav@1646
|
179 |
private static Name[] buildEmptyNames(int arity, String basicTypeSignature) {
|
jaroslav@1646
|
180 |
assert(isValidSignature(basicTypeSignature));
|
jaroslav@1646
|
181 |
int resultPos = arity + 1; // skip '_'
|
jaroslav@1646
|
182 |
if (arity < 0 || basicTypeSignature.length() != resultPos+1)
|
jaroslav@1646
|
183 |
throw new IllegalArgumentException("bad arity for "+basicTypeSignature);
|
jaroslav@1646
|
184 |
int numRes = (basicTypeSignature.charAt(resultPos) == 'V' ? 0 : 1);
|
jaroslav@1646
|
185 |
Name[] names = arguments(numRes, basicTypeSignature.substring(0, arity));
|
jaroslav@1646
|
186 |
for (int i = 0; i < numRes; i++) {
|
jaroslav@1646
|
187 |
names[arity + i] = constantZero(arity + i, basicTypeSignature.charAt(resultPos + i));
|
jaroslav@1646
|
188 |
}
|
jaroslav@1646
|
189 |
return names;
|
jaroslav@1646
|
190 |
}
|
jaroslav@1646
|
191 |
|
jaroslav@1646
|
192 |
private static int fixResult(int result, Name[] names) {
|
jaroslav@1646
|
193 |
if (result >= 0) {
|
jaroslav@1646
|
194 |
if (names[result].type == 'V')
|
jaroslav@1646
|
195 |
return -1;
|
jaroslav@1646
|
196 |
} else if (result == LAST_RESULT) {
|
jaroslav@1646
|
197 |
return names.length - 1;
|
jaroslav@1646
|
198 |
}
|
jaroslav@1646
|
199 |
return result;
|
jaroslav@1646
|
200 |
}
|
jaroslav@1646
|
201 |
|
jaroslav@1646
|
202 |
private static boolean namesOK(int arity, Name[] names) {
|
jaroslav@1646
|
203 |
for (int i = 0; i < names.length; i++) {
|
jaroslav@1646
|
204 |
Name n = names[i];
|
jaroslav@1646
|
205 |
assert(n != null) : "n is null";
|
jaroslav@1646
|
206 |
if (i < arity)
|
jaroslav@1646
|
207 |
assert( n.isParam()) : n + " is not param at " + i;
|
jaroslav@1646
|
208 |
else
|
jaroslav@1646
|
209 |
assert(!n.isParam()) : n + " is param at " + i;
|
jaroslav@1646
|
210 |
}
|
jaroslav@1646
|
211 |
return true;
|
jaroslav@1646
|
212 |
}
|
jaroslav@1646
|
213 |
|
jaroslav@1646
|
214 |
/** Renumber and/or replace params so that they are interned and canonically numbered. */
|
jaroslav@1646
|
215 |
private void normalize() {
|
jaroslav@1646
|
216 |
Name[] oldNames = null;
|
jaroslav@1646
|
217 |
int changesStart = 0;
|
jaroslav@1646
|
218 |
for (int i = 0; i < names.length; i++) {
|
jaroslav@1646
|
219 |
Name n = names[i];
|
jaroslav@1646
|
220 |
if (!n.initIndex(i)) {
|
jaroslav@1646
|
221 |
if (oldNames == null) {
|
jaroslav@1646
|
222 |
oldNames = names.clone();
|
jaroslav@1646
|
223 |
changesStart = i;
|
jaroslav@1646
|
224 |
}
|
jaroslav@1646
|
225 |
names[i] = n.cloneWithIndex(i);
|
jaroslav@1646
|
226 |
}
|
jaroslav@1646
|
227 |
}
|
jaroslav@1646
|
228 |
if (oldNames != null) {
|
jaroslav@1646
|
229 |
int startFixing = arity;
|
jaroslav@1646
|
230 |
if (startFixing <= changesStart)
|
jaroslav@1646
|
231 |
startFixing = changesStart+1;
|
jaroslav@1646
|
232 |
for (int i = startFixing; i < names.length; i++) {
|
jaroslav@1646
|
233 |
Name fixed = names[i].replaceNames(oldNames, names, changesStart, i);
|
jaroslav@1646
|
234 |
names[i] = fixed.newIndex(i);
|
jaroslav@1646
|
235 |
}
|
jaroslav@1646
|
236 |
}
|
jaroslav@1646
|
237 |
assert(nameRefsAreLegal());
|
jaroslav@1646
|
238 |
int maxInterned = Math.min(arity, INTERNED_ARGUMENT_LIMIT);
|
jaroslav@1646
|
239 |
boolean needIntern = false;
|
jaroslav@1646
|
240 |
for (int i = 0; i < maxInterned; i++) {
|
jaroslav@1646
|
241 |
Name n = names[i], n2 = internArgument(n);
|
jaroslav@1646
|
242 |
if (n != n2) {
|
jaroslav@1646
|
243 |
names[i] = n2;
|
jaroslav@1646
|
244 |
needIntern = true;
|
jaroslav@1646
|
245 |
}
|
jaroslav@1646
|
246 |
}
|
jaroslav@1646
|
247 |
if (needIntern) {
|
jaroslav@1646
|
248 |
for (int i = arity; i < names.length; i++) {
|
jaroslav@1646
|
249 |
names[i].internArguments();
|
jaroslav@1646
|
250 |
}
|
jaroslav@1646
|
251 |
assert(nameRefsAreLegal());
|
jaroslav@1646
|
252 |
}
|
jaroslav@1646
|
253 |
}
|
jaroslav@1646
|
254 |
|
jaroslav@1646
|
255 |
/**
|
jaroslav@1646
|
256 |
* Check that all embedded Name references are localizable to this lambda,
|
jaroslav@1646
|
257 |
* and are properly ordered after their corresponding definitions.
|
jaroslav@1646
|
258 |
* <p>
|
jaroslav@1646
|
259 |
* Note that a Name can be local to multiple lambdas, as long as
|
jaroslav@1646
|
260 |
* it possesses the same index in each use site.
|
jaroslav@1646
|
261 |
* This allows Name references to be freely reused to construct
|
jaroslav@1646
|
262 |
* fresh lambdas, without confusion.
|
jaroslav@1646
|
263 |
*/
|
jaroslav@1646
|
264 |
private boolean nameRefsAreLegal() {
|
jaroslav@1646
|
265 |
assert(arity >= 0 && arity <= names.length);
|
jaroslav@1646
|
266 |
assert(result >= -1 && result < names.length);
|
jaroslav@1646
|
267 |
// Do all names possess an index consistent with their local definition order?
|
jaroslav@1646
|
268 |
for (int i = 0; i < arity; i++) {
|
jaroslav@1646
|
269 |
Name n = names[i];
|
jaroslav@1646
|
270 |
assert(n.index() == i) : Arrays.asList(n.index(), i);
|
jaroslav@1646
|
271 |
assert(n.isParam());
|
jaroslav@1646
|
272 |
}
|
jaroslav@1646
|
273 |
// Also, do all local name references
|
jaroslav@1646
|
274 |
for (int i = arity; i < names.length; i++) {
|
jaroslav@1646
|
275 |
Name n = names[i];
|
jaroslav@1646
|
276 |
assert(n.index() == i);
|
jaroslav@1646
|
277 |
for (Object arg : n.arguments) {
|
jaroslav@1646
|
278 |
if (arg instanceof Name) {
|
jaroslav@1646
|
279 |
Name n2 = (Name) arg;
|
jaroslav@1646
|
280 |
int i2 = n2.index;
|
jaroslav@1646
|
281 |
assert(0 <= i2 && i2 < names.length) : n.debugString() + ": 0 <= i2 && i2 < names.length: 0 <= " + i2 + " < " + names.length;
|
jaroslav@1646
|
282 |
assert(names[i2] == n2) : Arrays.asList("-1-", i, "-2-", n.debugString(), "-3-", i2, "-4-", n2.debugString(), "-5-", names[i2].debugString(), "-6-", this);
|
jaroslav@1646
|
283 |
assert(i2 < i); // ref must come after def!
|
jaroslav@1646
|
284 |
}
|
jaroslav@1646
|
285 |
}
|
jaroslav@1646
|
286 |
}
|
jaroslav@1646
|
287 |
return true;
|
jaroslav@1646
|
288 |
}
|
jaroslav@1646
|
289 |
|
jaroslav@1646
|
290 |
/** Invoke this form on the given arguments. */
|
jaroslav@1646
|
291 |
// final Object invoke(Object... args) throws Throwable {
|
jaroslav@1646
|
292 |
// // NYI: fit this into the fast path?
|
jaroslav@1646
|
293 |
// return interpretWithArguments(args);
|
jaroslav@1646
|
294 |
// }
|
jaroslav@1646
|
295 |
|
jaroslav@1646
|
296 |
/** Report the return type. */
|
jaroslav@1646
|
297 |
char returnType() {
|
jaroslav@1646
|
298 |
if (result < 0) return 'V';
|
jaroslav@1646
|
299 |
Name n = names[result];
|
jaroslav@1646
|
300 |
return n.type;
|
jaroslav@1646
|
301 |
}
|
jaroslav@1646
|
302 |
|
jaroslav@1646
|
303 |
/** Report the N-th argument type. */
|
jaroslav@1646
|
304 |
char parameterType(int n) {
|
jaroslav@1646
|
305 |
assert(n < arity);
|
jaroslav@1646
|
306 |
return names[n].type;
|
jaroslav@1646
|
307 |
}
|
jaroslav@1646
|
308 |
|
jaroslav@1646
|
309 |
/** Report the arity. */
|
jaroslav@1646
|
310 |
int arity() {
|
jaroslav@1646
|
311 |
return arity;
|
jaroslav@1646
|
312 |
}
|
jaroslav@1646
|
313 |
|
jaroslav@1646
|
314 |
/** Return the method type corresponding to my basic type signature. */
|
jaroslav@1646
|
315 |
MethodType methodType() {
|
jaroslav@1646
|
316 |
return signatureType(basicTypeSignature());
|
jaroslav@1646
|
317 |
}
|
jaroslav@1646
|
318 |
/** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
|
jaroslav@1646
|
319 |
final String basicTypeSignature() {
|
jaroslav@1646
|
320 |
StringBuilder buf = new StringBuilder(arity() + 3);
|
jaroslav@1646
|
321 |
for (int i = 0, a = arity(); i < a; i++)
|
jaroslav@1646
|
322 |
buf.append(parameterType(i));
|
jaroslav@1646
|
323 |
return buf.append('_').append(returnType()).toString();
|
jaroslav@1646
|
324 |
}
|
jaroslav@1646
|
325 |
static int signatureArity(String sig) {
|
jaroslav@1646
|
326 |
assert(isValidSignature(sig));
|
jaroslav@1646
|
327 |
return sig.indexOf('_');
|
jaroslav@1646
|
328 |
}
|
jaroslav@1646
|
329 |
static char signatureReturn(String sig) {
|
jaroslav@1646
|
330 |
return sig.charAt(signatureArity(sig)+1);
|
jaroslav@1646
|
331 |
}
|
jaroslav@1646
|
332 |
static boolean isValidSignature(String sig) {
|
jaroslav@1646
|
333 |
int arity = sig.indexOf('_');
|
jaroslav@1646
|
334 |
if (arity < 0) return false; // must be of the form *_*
|
jaroslav@1646
|
335 |
int siglen = sig.length();
|
jaroslav@1646
|
336 |
if (siglen != arity + 2) return false; // *_X
|
jaroslav@1646
|
337 |
for (int i = 0; i < siglen; i++) {
|
jaroslav@1646
|
338 |
if (i == arity) continue; // skip '_'
|
jaroslav@1646
|
339 |
char c = sig.charAt(i);
|
jaroslav@1646
|
340 |
if (c == 'V')
|
jaroslav@1646
|
341 |
return (i == siglen - 1 && arity == siglen - 2);
|
jaroslav@1646
|
342 |
if (ALL_TYPES.indexOf(c) < 0) return false; // must be [LIJFD]
|
jaroslav@1646
|
343 |
}
|
jaroslav@1646
|
344 |
return true; // [LIJFD]*_[LIJFDV]
|
jaroslav@1646
|
345 |
}
|
jaroslav@1646
|
346 |
static Class<?> typeClass(char t) {
|
jaroslav@1646
|
347 |
switch (t) {
|
jaroslav@1646
|
348 |
case 'I': return int.class;
|
jaroslav@1646
|
349 |
case 'J': return long.class;
|
jaroslav@1646
|
350 |
case 'F': return float.class;
|
jaroslav@1646
|
351 |
case 'D': return double.class;
|
jaroslav@1646
|
352 |
case 'L': return Object.class;
|
jaroslav@1646
|
353 |
case 'V': return void.class;
|
jaroslav@1646
|
354 |
default: assert false;
|
jaroslav@1646
|
355 |
}
|
jaroslav@1646
|
356 |
return null;
|
jaroslav@1646
|
357 |
}
|
jaroslav@1646
|
358 |
static MethodType signatureType(String sig) {
|
jaroslav@1646
|
359 |
Class<?>[] ptypes = new Class<?>[signatureArity(sig)];
|
jaroslav@1646
|
360 |
for (int i = 0; i < ptypes.length; i++)
|
jaroslav@1646
|
361 |
ptypes[i] = typeClass(sig.charAt(i));
|
jaroslav@1646
|
362 |
Class<?> rtype = typeClass(signatureReturn(sig));
|
jaroslav@1646
|
363 |
return MethodType.methodType(rtype, ptypes);
|
jaroslav@1646
|
364 |
}
|
jaroslav@1646
|
365 |
|
jaroslav@1646
|
366 |
/*
|
jaroslav@1646
|
367 |
* Code generation issues:
|
jaroslav@1646
|
368 |
*
|
jaroslav@1646
|
369 |
* Compiled LFs should be reusable in general.
|
jaroslav@1646
|
370 |
* The biggest issue is how to decide when to pull a name into
|
jaroslav@1646
|
371 |
* the bytecode, versus loading a reified form from the MH data.
|
jaroslav@1646
|
372 |
*
|
jaroslav@1646
|
373 |
* For example, an asType wrapper may require execution of a cast
|
jaroslav@1646
|
374 |
* after a call to a MH. The target type of the cast can be placed
|
jaroslav@1646
|
375 |
* as a constant in the LF itself. This will force the cast type
|
jaroslav@1646
|
376 |
* to be compiled into the bytecodes and native code for the MH.
|
jaroslav@1646
|
377 |
* Or, the target type of the cast can be erased in the LF, and
|
jaroslav@1646
|
378 |
* loaded from the MH data. (Later on, if the MH as a whole is
|
jaroslav@1646
|
379 |
* inlined, the data will flow into the inlined instance of the LF,
|
jaroslav@1646
|
380 |
* as a constant, and the end result will be an optimal cast.)
|
jaroslav@1646
|
381 |
*
|
jaroslav@1646
|
382 |
* This erasure of cast types can be done with any use of
|
jaroslav@1646
|
383 |
* reference types. It can also be done with whole method
|
jaroslav@1646
|
384 |
* handles. Erasing a method handle might leave behind
|
jaroslav@1646
|
385 |
* LF code that executes correctly for any MH of a given
|
jaroslav@1646
|
386 |
* type, and load the required MH from the enclosing MH's data.
|
jaroslav@1646
|
387 |
* Or, the erasure might even erase the expected MT.
|
jaroslav@1646
|
388 |
*
|
jaroslav@1646
|
389 |
* Also, for direct MHs, the MemberName of the target
|
jaroslav@1646
|
390 |
* could be erased, and loaded from the containing direct MH.
|
jaroslav@1646
|
391 |
* As a simple case, a LF for all int-valued non-static
|
jaroslav@1646
|
392 |
* field getters would perform a cast on its input argument
|
jaroslav@1646
|
393 |
* (to non-constant base type derived from the MemberName)
|
jaroslav@1646
|
394 |
* and load an integer value from the input object
|
jaroslav@1646
|
395 |
* (at a non-constant offset also derived from the MemberName).
|
jaroslav@1646
|
396 |
* Such MN-erased LFs would be inlinable back to optimized
|
jaroslav@1646
|
397 |
* code, whenever a constant enclosing DMH is available
|
jaroslav@1646
|
398 |
* to supply a constant MN from its data.
|
jaroslav@1646
|
399 |
*
|
jaroslav@1646
|
400 |
* The main problem here is to keep LFs reasonably generic,
|
jaroslav@1646
|
401 |
* while ensuring that hot spots will inline good instances.
|
jaroslav@1646
|
402 |
* "Reasonably generic" means that we don't end up with
|
jaroslav@1646
|
403 |
* repeated versions of bytecode or machine code that do
|
jaroslav@1646
|
404 |
* not differ in their optimized form. Repeated versions
|
jaroslav@1646
|
405 |
* of machine would have the undesirable overheads of
|
jaroslav@1646
|
406 |
* (a) redundant compilation work and (b) extra I$ pressure.
|
jaroslav@1646
|
407 |
* To control repeated versions, we need to be ready to
|
jaroslav@1646
|
408 |
* erase details from LFs and move them into MH data,
|
jaroslav@1646
|
409 |
* whevener those details are not relevant to significant
|
jaroslav@1646
|
410 |
* optimization. "Significant" means optimization of
|
jaroslav@1646
|
411 |
* code that is actually hot.
|
jaroslav@1646
|
412 |
*
|
jaroslav@1646
|
413 |
* Achieving this may require dynamic splitting of MHs, by replacing
|
jaroslav@1646
|
414 |
* a generic LF with a more specialized one, on the same MH,
|
jaroslav@1646
|
415 |
* if (a) the MH is frequently executed and (b) the MH cannot
|
jaroslav@1646
|
416 |
* be inlined into a containing caller, such as an invokedynamic.
|
jaroslav@1646
|
417 |
*
|
jaroslav@1646
|
418 |
* Compiled LFs that are no longer used should be GC-able.
|
jaroslav@1646
|
419 |
* If they contain non-BCP references, they should be properly
|
jaroslav@1646
|
420 |
* interlinked with the class loader(s) that their embedded types
|
jaroslav@1646
|
421 |
* depend on. This probably means that reusable compiled LFs
|
jaroslav@1646
|
422 |
* will be tabulated (indexed) on relevant class loaders,
|
jaroslav@1646
|
423 |
* or else that the tables that cache them will have weak links.
|
jaroslav@1646
|
424 |
*/
|
jaroslav@1646
|
425 |
|
jaroslav@1646
|
426 |
/**
|
jaroslav@1646
|
427 |
* Make this LF directly executable, as part of a MethodHandle.
|
jaroslav@1646
|
428 |
* Invariant: Every MH which is invoked must prepare its LF
|
jaroslav@1646
|
429 |
* before invocation.
|
jaroslav@1646
|
430 |
* (In principle, the JVM could do this very lazily,
|
jaroslav@1646
|
431 |
* as a sort of pre-invocation linkage step.)
|
jaroslav@1646
|
432 |
*/
|
jaroslav@1646
|
433 |
public void prepare() {
|
jaroslav@1646
|
434 |
if (COMPILE_THRESHOLD == 0) {
|
jaroslav@1646
|
435 |
compileToBytecode();
|
jaroslav@1646
|
436 |
}
|
jaroslav@1646
|
437 |
if (this.vmentry != null) {
|
jaroslav@1646
|
438 |
// already prepared (e.g., a primitive DMH invoker form)
|
jaroslav@1646
|
439 |
return;
|
jaroslav@1646
|
440 |
}
|
jaroslav@1646
|
441 |
LambdaForm prep = getPreparedForm(basicTypeSignature());
|
jaroslav@1646
|
442 |
this.vmentry = prep.vmentry;
|
jaroslav@1646
|
443 |
// TO DO: Maybe add invokeGeneric, invokeWithArguments
|
jaroslav@1646
|
444 |
}
|
jaroslav@1646
|
445 |
|
jaroslav@1646
|
446 |
/** Generate optimizable bytecode for this form. */
|
jaroslav@1646
|
447 |
MemberName compileToBytecode() {
|
jaroslav@1646
|
448 |
MethodType invokerType = methodType();
|
jaroslav@1646
|
449 |
assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
|
jaroslav@1646
|
450 |
if (vmentry != null && isCompiled) {
|
jaroslav@1646
|
451 |
return vmentry; // already compiled somehow
|
jaroslav@1646
|
452 |
}
|
jaroslav@1646
|
453 |
try {
|
jaroslav@1646
|
454 |
vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
|
jaroslav@1646
|
455 |
if (TRACE_INTERPRETER)
|
jaroslav@1646
|
456 |
traceInterpreter("compileToBytecode", this);
|
jaroslav@1646
|
457 |
isCompiled = true;
|
jaroslav@1646
|
458 |
return vmentry;
|
jaroslav@1646
|
459 |
} catch (Error | Exception ex) {
|
jaroslav@1646
|
460 |
throw newInternalError("compileToBytecode", ex);
|
jaroslav@1646
|
461 |
}
|
jaroslav@1646
|
462 |
}
|
jaroslav@1646
|
463 |
|
jaroslav@1646
|
464 |
private static final ConcurrentHashMap<String,LambdaForm> PREPARED_FORMS;
|
jaroslav@1646
|
465 |
static {
|
jaroslav@1646
|
466 |
int capacity = 512; // expect many distinct signatures over time
|
jaroslav@1646
|
467 |
float loadFactor = 0.75f; // normal default
|
jaroslav@1646
|
468 |
int writers = 1;
|
jaroslav@1646
|
469 |
PREPARED_FORMS = new ConcurrentHashMap<>(capacity, loadFactor, writers);
|
jaroslav@1646
|
470 |
}
|
jaroslav@1646
|
471 |
|
jaroslav@1646
|
472 |
private static Map<String,LambdaForm> computeInitialPreparedForms() {
|
jaroslav@1646
|
473 |
// Find all predefined invokers and associate them with canonical empty lambda forms.
|
jaroslav@1646
|
474 |
HashMap<String,LambdaForm> forms = new HashMap<>();
|
jaroslav@1646
|
475 |
for (MemberName m : MemberName.getFactory().getMethods(LambdaForm.class, false, null, null, null)) {
|
jaroslav@1646
|
476 |
if (!m.isStatic() || !m.isPackage()) continue;
|
jaroslav@1646
|
477 |
MethodType mt = m.getMethodType();
|
jaroslav@1646
|
478 |
if (mt.parameterCount() > 0 &&
|
jaroslav@1646
|
479 |
mt.parameterType(0) == MethodHandle.class &&
|
jaroslav@1646
|
480 |
m.getName().startsWith("interpret_")) {
|
jaroslav@1646
|
481 |
String sig = basicTypeSignature(mt);
|
jaroslav@1646
|
482 |
assert(m.getName().equals("interpret" + sig.substring(sig.indexOf('_'))));
|
jaroslav@1646
|
483 |
LambdaForm form = new LambdaForm(sig);
|
jaroslav@1646
|
484 |
form.vmentry = m;
|
jaroslav@1646
|
485 |
mt.form().setCachedLambdaForm(MethodTypeForm.LF_COUNTER, form);
|
jaroslav@1646
|
486 |
// FIXME: get rid of PREPARED_FORMS; use MethodTypeForm cache only
|
jaroslav@1646
|
487 |
forms.put(sig, form);
|
jaroslav@1646
|
488 |
}
|
jaroslav@1646
|
489 |
}
|
jaroslav@1646
|
490 |
//System.out.println("computeInitialPreparedForms => "+forms);
|
jaroslav@1646
|
491 |
return forms;
|
jaroslav@1646
|
492 |
}
|
jaroslav@1646
|
493 |
|
jaroslav@1646
|
494 |
// Set this false to disable use of the interpret_L methods defined in this file.
|
jaroslav@1646
|
495 |
private static final boolean USE_PREDEFINED_INTERPRET_METHODS = true;
|
jaroslav@1646
|
496 |
|
jaroslav@1646
|
497 |
// The following are predefined exact invokers. The system must build
|
jaroslav@1646
|
498 |
// a separate invoker for each distinct signature.
|
jaroslav@1646
|
499 |
static Object interpret_L(MethodHandle mh) throws Throwable {
|
jaroslav@1646
|
500 |
Object[] av = {mh};
|
jaroslav@1646
|
501 |
String sig = null;
|
jaroslav@1646
|
502 |
assert(argumentTypesMatch(sig = "L_L", av));
|
jaroslav@1646
|
503 |
Object res = mh.form.interpretWithArguments(av);
|
jaroslav@1646
|
504 |
assert(returnTypesMatch(sig, av, res));
|
jaroslav@1646
|
505 |
return res;
|
jaroslav@1646
|
506 |
}
|
jaroslav@1646
|
507 |
static Object interpret_L(MethodHandle mh, Object x1) throws Throwable {
|
jaroslav@1646
|
508 |
Object[] av = {mh, x1};
|
jaroslav@1646
|
509 |
String sig = null;
|
jaroslav@1646
|
510 |
assert(argumentTypesMatch(sig = "LL_L", av));
|
jaroslav@1646
|
511 |
Object res = mh.form.interpretWithArguments(av);
|
jaroslav@1646
|
512 |
assert(returnTypesMatch(sig, av, res));
|
jaroslav@1646
|
513 |
return res;
|
jaroslav@1646
|
514 |
}
|
jaroslav@1646
|
515 |
static Object interpret_L(MethodHandle mh, Object x1, Object x2) throws Throwable {
|
jaroslav@1646
|
516 |
Object[] av = {mh, x1, x2};
|
jaroslav@1646
|
517 |
String sig = null;
|
jaroslav@1646
|
518 |
assert(argumentTypesMatch(sig = "LLL_L", av));
|
jaroslav@1646
|
519 |
Object res = mh.form.interpretWithArguments(av);
|
jaroslav@1646
|
520 |
assert(returnTypesMatch(sig, av, res));
|
jaroslav@1646
|
521 |
return res;
|
jaroslav@1646
|
522 |
}
|
jaroslav@1646
|
523 |
private static LambdaForm getPreparedForm(String sig) {
|
jaroslav@1646
|
524 |
MethodType mtype = signatureType(sig);
|
jaroslav@1646
|
525 |
//LambdaForm prep = PREPARED_FORMS.get(sig);
|
jaroslav@1646
|
526 |
LambdaForm prep = mtype.form().cachedLambdaForm(MethodTypeForm.LF_INTERPRET);
|
jaroslav@1646
|
527 |
if (prep != null) return prep;
|
jaroslav@1646
|
528 |
assert(isValidSignature(sig));
|
jaroslav@1646
|
529 |
prep = new LambdaForm(sig);
|
jaroslav@1646
|
530 |
prep.vmentry = InvokerBytecodeGenerator.generateLambdaFormInterpreterEntryPoint(sig);
|
jaroslav@1646
|
531 |
//LambdaForm prep2 = PREPARED_FORMS.putIfAbsent(sig.intern(), prep);
|
jaroslav@1646
|
532 |
return mtype.form().setCachedLambdaForm(MethodTypeForm.LF_INTERPRET, prep);
|
jaroslav@1646
|
533 |
}
|
jaroslav@1646
|
534 |
|
jaroslav@1646
|
535 |
// The next few routines are called only from assert expressions
|
jaroslav@1646
|
536 |
// They verify that the built-in invokers process the correct raw data types.
|
jaroslav@1646
|
537 |
private static boolean argumentTypesMatch(String sig, Object[] av) {
|
jaroslav@1646
|
538 |
int arity = signatureArity(sig);
|
jaroslav@1646
|
539 |
assert(av.length == arity) : "av.length == arity: av.length=" + av.length + ", arity=" + arity;
|
jaroslav@1646
|
540 |
assert(av[0] instanceof MethodHandle) : "av[0] not instace of MethodHandle: " + av[0];
|
jaroslav@1646
|
541 |
MethodHandle mh = (MethodHandle) av[0];
|
jaroslav@1646
|
542 |
MethodType mt = mh.type();
|
jaroslav@1646
|
543 |
assert(mt.parameterCount() == arity-1);
|
jaroslav@1646
|
544 |
for (int i = 0; i < av.length; i++) {
|
jaroslav@1646
|
545 |
Class<?> pt = (i == 0 ? MethodHandle.class : mt.parameterType(i-1));
|
jaroslav@1646
|
546 |
assert(valueMatches(sig.charAt(i), pt, av[i]));
|
jaroslav@1646
|
547 |
}
|
jaroslav@1646
|
548 |
return true;
|
jaroslav@1646
|
549 |
}
|
jaroslav@1646
|
550 |
private static boolean valueMatches(char tc, Class<?> type, Object x) {
|
jaroslav@1646
|
551 |
// The following line is needed because (...)void method handles can use non-void invokers
|
jaroslav@1646
|
552 |
if (type == void.class) tc = 'V'; // can drop any kind of value
|
jaroslav@1646
|
553 |
assert tc == basicType(type) : tc + " == basicType(" + type + ")=" + basicType(type);
|
jaroslav@1646
|
554 |
switch (tc) {
|
jaroslav@1646
|
555 |
case 'I': assert checkInt(type, x) : "checkInt(" + type + "," + x +")"; break;
|
jaroslav@1646
|
556 |
case 'J': assert x instanceof Long : "instanceof Long: " + x; break;
|
jaroslav@1646
|
557 |
case 'F': assert x instanceof Float : "instanceof Float: " + x; break;
|
jaroslav@1646
|
558 |
case 'D': assert x instanceof Double : "instanceof Double: " + x; break;
|
jaroslav@1646
|
559 |
case 'L': assert checkRef(type, x) : "checkRef(" + type + "," + x + ")"; break;
|
jaroslav@1646
|
560 |
case 'V': break; // allow anything here; will be dropped
|
jaroslav@1646
|
561 |
default: assert(false);
|
jaroslav@1646
|
562 |
}
|
jaroslav@1646
|
563 |
return true;
|
jaroslav@1646
|
564 |
}
|
jaroslav@1646
|
565 |
private static boolean returnTypesMatch(String sig, Object[] av, Object res) {
|
jaroslav@1646
|
566 |
MethodHandle mh = (MethodHandle) av[0];
|
jaroslav@1646
|
567 |
return valueMatches(signatureReturn(sig), mh.type().returnType(), res);
|
jaroslav@1646
|
568 |
}
|
jaroslav@1646
|
569 |
private static boolean checkInt(Class<?> type, Object x) {
|
jaroslav@1646
|
570 |
assert(x instanceof Integer);
|
jaroslav@1646
|
571 |
if (type == int.class) return true;
|
jaroslav@1646
|
572 |
Wrapper w = Wrapper.forBasicType(type);
|
jaroslav@1646
|
573 |
assert(w.isSubwordOrInt());
|
jaroslav@1646
|
574 |
Object x1 = Wrapper.INT.wrap(w.wrap(x));
|
jaroslav@1646
|
575 |
return x.equals(x1);
|
jaroslav@1646
|
576 |
}
|
jaroslav@1646
|
577 |
private static boolean checkRef(Class<?> type, Object x) {
|
jaroslav@1646
|
578 |
assert(!type.isPrimitive());
|
jaroslav@1646
|
579 |
if (x == null) return true;
|
jaroslav@1646
|
580 |
if (type.isInterface()) return true;
|
jaroslav@1646
|
581 |
return type.isInstance(x);
|
jaroslav@1646
|
582 |
}
|
jaroslav@1646
|
583 |
|
jaroslav@1646
|
584 |
/** If the invocation count hits the threshold we spin bytecodes and call that subsequently. */
|
jaroslav@1646
|
585 |
private static final int COMPILE_THRESHOLD;
|
jaroslav@1646
|
586 |
static {
|
jaroslav@1646
|
587 |
if (MethodHandleStatics.COMPILE_THRESHOLD != null)
|
jaroslav@1646
|
588 |
COMPILE_THRESHOLD = MethodHandleStatics.COMPILE_THRESHOLD;
|
jaroslav@1646
|
589 |
else
|
jaroslav@1646
|
590 |
COMPILE_THRESHOLD = 30; // default value
|
jaroslav@1646
|
591 |
}
|
jaroslav@1646
|
592 |
private int invocationCounter = 0;
|
jaroslav@1646
|
593 |
|
jaroslav@1646
|
594 |
@Hidden
|
jaroslav@1646
|
595 |
@DontInline
|
jaroslav@1646
|
596 |
/** Interpretively invoke this form on the given arguments. */
|
jaroslav@1646
|
597 |
Object interpretWithArguments(Object... argumentValues) throws Throwable {
|
jaroslav@1646
|
598 |
if (TRACE_INTERPRETER)
|
jaroslav@1646
|
599 |
return interpretWithArgumentsTracing(argumentValues);
|
jaroslav@1646
|
600 |
checkInvocationCounter();
|
jaroslav@1646
|
601 |
assert(arityCheck(argumentValues));
|
jaroslav@1646
|
602 |
Object[] values = Arrays.copyOf(argumentValues, names.length);
|
jaroslav@1646
|
603 |
for (int i = argumentValues.length; i < values.length; i++) {
|
jaroslav@1646
|
604 |
values[i] = interpretName(names[i], values);
|
jaroslav@1646
|
605 |
}
|
jaroslav@1646
|
606 |
return (result < 0) ? null : values[result];
|
jaroslav@1646
|
607 |
}
|
jaroslav@1646
|
608 |
|
jaroslav@1646
|
609 |
@Hidden
|
jaroslav@1646
|
610 |
@DontInline
|
jaroslav@1646
|
611 |
/** Evaluate a single Name within this form, applying its function to its arguments. */
|
jaroslav@1646
|
612 |
Object interpretName(Name name, Object[] values) throws Throwable {
|
jaroslav@1646
|
613 |
if (TRACE_INTERPRETER)
|
jaroslav@1646
|
614 |
traceInterpreter("| interpretName", name.debugString(), (Object[]) null);
|
jaroslav@1646
|
615 |
Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
|
jaroslav@1646
|
616 |
for (int i = 0; i < arguments.length; i++) {
|
jaroslav@1646
|
617 |
Object a = arguments[i];
|
jaroslav@1646
|
618 |
if (a instanceof Name) {
|
jaroslav@1646
|
619 |
int i2 = ((Name)a).index();
|
jaroslav@1646
|
620 |
assert(names[i2] == a);
|
jaroslav@1646
|
621 |
a = values[i2];
|
jaroslav@1646
|
622 |
arguments[i] = a;
|
jaroslav@1646
|
623 |
}
|
jaroslav@1646
|
624 |
}
|
jaroslav@1646
|
625 |
return name.function.invokeWithArguments(arguments);
|
jaroslav@1646
|
626 |
}
|
jaroslav@1646
|
627 |
|
jaroslav@1646
|
628 |
private void checkInvocationCounter() {
|
jaroslav@1646
|
629 |
if (COMPILE_THRESHOLD != 0 &&
|
jaroslav@1646
|
630 |
invocationCounter < COMPILE_THRESHOLD) {
|
jaroslav@1646
|
631 |
invocationCounter++; // benign race
|
jaroslav@1646
|
632 |
if (invocationCounter >= COMPILE_THRESHOLD) {
|
jaroslav@1646
|
633 |
// Replace vmentry with a bytecode version of this LF.
|
jaroslav@1646
|
634 |
compileToBytecode();
|
jaroslav@1646
|
635 |
}
|
jaroslav@1646
|
636 |
}
|
jaroslav@1646
|
637 |
}
|
jaroslav@1646
|
638 |
Object interpretWithArgumentsTracing(Object... argumentValues) throws Throwable {
|
jaroslav@1646
|
639 |
traceInterpreter("[ interpretWithArguments", this, argumentValues);
|
jaroslav@1646
|
640 |
if (invocationCounter < COMPILE_THRESHOLD) {
|
jaroslav@1646
|
641 |
int ctr = invocationCounter++; // benign race
|
jaroslav@1646
|
642 |
traceInterpreter("| invocationCounter", ctr);
|
jaroslav@1646
|
643 |
if (invocationCounter >= COMPILE_THRESHOLD) {
|
jaroslav@1646
|
644 |
compileToBytecode();
|
jaroslav@1646
|
645 |
}
|
jaroslav@1646
|
646 |
}
|
jaroslav@1646
|
647 |
Object rval;
|
jaroslav@1646
|
648 |
try {
|
jaroslav@1646
|
649 |
assert(arityCheck(argumentValues));
|
jaroslav@1646
|
650 |
Object[] values = Arrays.copyOf(argumentValues, names.length);
|
jaroslav@1646
|
651 |
for (int i = argumentValues.length; i < values.length; i++) {
|
jaroslav@1646
|
652 |
values[i] = interpretName(names[i], values);
|
jaroslav@1646
|
653 |
}
|
jaroslav@1646
|
654 |
rval = (result < 0) ? null : values[result];
|
jaroslav@1646
|
655 |
} catch (Throwable ex) {
|
jaroslav@1646
|
656 |
traceInterpreter("] throw =>", ex);
|
jaroslav@1646
|
657 |
throw ex;
|
jaroslav@1646
|
658 |
}
|
jaroslav@1646
|
659 |
traceInterpreter("] return =>", rval);
|
jaroslav@1646
|
660 |
return rval;
|
jaroslav@1646
|
661 |
}
|
jaroslav@1646
|
662 |
|
jaroslav@1646
|
663 |
//** This transform is applied (statically) to every name.function. */
|
jaroslav@1646
|
664 |
/*
|
jaroslav@1646
|
665 |
private static MethodHandle eraseSubwordTypes(MethodHandle mh) {
|
jaroslav@1646
|
666 |
MethodType mt = mh.type();
|
jaroslav@1646
|
667 |
if (mt.hasPrimitives()) {
|
jaroslav@1646
|
668 |
mt = mt.changeReturnType(eraseSubwordType(mt.returnType()));
|
jaroslav@1646
|
669 |
for (int i = 0; i < mt.parameterCount(); i++) {
|
jaroslav@1646
|
670 |
mt = mt.changeParameterType(i, eraseSubwordType(mt.parameterType(i)));
|
jaroslav@1646
|
671 |
}
|
jaroslav@1646
|
672 |
mh = MethodHandles.explicitCastArguments(mh, mt);
|
jaroslav@1646
|
673 |
}
|
jaroslav@1646
|
674 |
return mh;
|
jaroslav@1646
|
675 |
}
|
jaroslav@1646
|
676 |
private static Class<?> eraseSubwordType(Class<?> type) {
|
jaroslav@1646
|
677 |
if (!type.isPrimitive()) return type;
|
jaroslav@1646
|
678 |
if (type == int.class) return type;
|
jaroslav@1646
|
679 |
Wrapper w = Wrapper.forPrimitiveType(type);
|
jaroslav@1646
|
680 |
if (w.isSubwordOrInt()) return int.class;
|
jaroslav@1646
|
681 |
return type;
|
jaroslav@1646
|
682 |
}
|
jaroslav@1646
|
683 |
*/
|
jaroslav@1646
|
684 |
|
jaroslav@1646
|
685 |
static void traceInterpreter(String event, Object obj, Object... args) {
|
jaroslav@1646
|
686 |
if (TRACE_INTERPRETER) {
|
jaroslav@1646
|
687 |
System.out.println("LFI: "+event+" "+(obj != null ? obj : "")+(args != null && args.length != 0 ? Arrays.asList(args) : ""));
|
jaroslav@1646
|
688 |
}
|
jaroslav@1646
|
689 |
}
|
jaroslav@1646
|
690 |
static void traceInterpreter(String event, Object obj) {
|
jaroslav@1646
|
691 |
traceInterpreter(event, obj, (Object[])null);
|
jaroslav@1646
|
692 |
}
|
jaroslav@1646
|
693 |
private boolean arityCheck(Object[] argumentValues) {
|
jaroslav@1646
|
694 |
assert(argumentValues.length == arity) : arity+"!="+Arrays.asList(argumentValues)+".length";
|
jaroslav@1646
|
695 |
// also check that the leading (receiver) argument is somehow bound to this LF:
|
jaroslav@1646
|
696 |
assert(argumentValues[0] instanceof MethodHandle) : "not MH: " + argumentValues[0];
|
jaroslav@1646
|
697 |
assert(((MethodHandle)argumentValues[0]).internalForm() == this);
|
jaroslav@1646
|
698 |
// note: argument #0 could also be an interface wrapper, in the future
|
jaroslav@1646
|
699 |
return true;
|
jaroslav@1646
|
700 |
}
|
jaroslav@1646
|
701 |
|
jaroslav@1646
|
702 |
private boolean isEmpty() {
|
jaroslav@1646
|
703 |
if (result < 0)
|
jaroslav@1646
|
704 |
return (names.length == arity);
|
jaroslav@1646
|
705 |
else if (result == arity && names.length == arity + 1)
|
jaroslav@1646
|
706 |
return names[arity].isConstantZero();
|
jaroslav@1646
|
707 |
else
|
jaroslav@1646
|
708 |
return false;
|
jaroslav@1646
|
709 |
}
|
jaroslav@1646
|
710 |
|
jaroslav@1646
|
711 |
public String toString() {
|
jaroslav@1646
|
712 |
StringBuilder buf = new StringBuilder(debugName+"=Lambda(");
|
jaroslav@1646
|
713 |
for (int i = 0; i < names.length; i++) {
|
jaroslav@1646
|
714 |
if (i == arity) buf.append(")=>{");
|
jaroslav@1646
|
715 |
Name n = names[i];
|
jaroslav@1646
|
716 |
if (i >= arity) buf.append("\n ");
|
jaroslav@1646
|
717 |
buf.append(n);
|
jaroslav@1646
|
718 |
if (i < arity) {
|
jaroslav@1646
|
719 |
if (i+1 < arity) buf.append(",");
|
jaroslav@1646
|
720 |
continue;
|
jaroslav@1646
|
721 |
}
|
jaroslav@1646
|
722 |
buf.append("=").append(n.exprString());
|
jaroslav@1646
|
723 |
buf.append(";");
|
jaroslav@1646
|
724 |
}
|
jaroslav@1646
|
725 |
buf.append(result < 0 ? "void" : names[result]).append("}");
|
jaroslav@1646
|
726 |
if (TRACE_INTERPRETER) {
|
jaroslav@1646
|
727 |
// Extra verbosity:
|
jaroslav@1646
|
728 |
buf.append(":").append(basicTypeSignature());
|
jaroslav@1646
|
729 |
buf.append("/").append(vmentry);
|
jaroslav@1646
|
730 |
}
|
jaroslav@1646
|
731 |
return buf.toString();
|
jaroslav@1646
|
732 |
}
|
jaroslav@1646
|
733 |
|
jaroslav@1646
|
734 |
/**
|
jaroslav@1646
|
735 |
* Apply immediate binding for a Name in this form indicated by its position relative to the form.
|
jaroslav@1646
|
736 |
* The first parameter to a LambdaForm, a0:L, always represents the form's method handle, so 0 is not
|
jaroslav@1646
|
737 |
* accepted as valid.
|
jaroslav@1646
|
738 |
*/
|
jaroslav@1646
|
739 |
LambdaForm bindImmediate(int pos, char basicType, Object value) {
|
jaroslav@1646
|
740 |
// must be an argument, and the types must match
|
jaroslav@1646
|
741 |
assert pos > 0 && pos < arity && names[pos].type == basicType && Name.typesMatch(basicType, value);
|
jaroslav@1646
|
742 |
|
jaroslav@1646
|
743 |
int arity2 = arity - 1;
|
jaroslav@1646
|
744 |
Name[] names2 = new Name[names.length - 1];
|
jaroslav@1646
|
745 |
for (int r = 0, w = 0; r < names.length; ++r, ++w) { // (r)ead from names, (w)rite to names2
|
jaroslav@1646
|
746 |
Name n = names[r];
|
jaroslav@1646
|
747 |
if (n.isParam()) {
|
jaroslav@1646
|
748 |
if (n.index == pos) {
|
jaroslav@1646
|
749 |
// do not copy over the argument that is to be replaced with a literal,
|
jaroslav@1646
|
750 |
// but adjust the write index
|
jaroslav@1646
|
751 |
--w;
|
jaroslav@1646
|
752 |
} else {
|
jaroslav@1646
|
753 |
names2[w] = new Name(w, n.type);
|
jaroslav@1646
|
754 |
}
|
jaroslav@1646
|
755 |
} else {
|
jaroslav@1646
|
756 |
Object[] arguments2 = new Object[n.arguments.length];
|
jaroslav@1646
|
757 |
for (int i = 0; i < n.arguments.length; ++i) {
|
jaroslav@1646
|
758 |
Object arg = n.arguments[i];
|
jaroslav@1646
|
759 |
if (arg instanceof Name) {
|
jaroslav@1646
|
760 |
int ni = ((Name) arg).index;
|
jaroslav@1646
|
761 |
if (ni == pos) {
|
jaroslav@1646
|
762 |
arguments2[i] = value;
|
jaroslav@1646
|
763 |
} else if (ni < pos) {
|
jaroslav@1646
|
764 |
// replacement position not yet passed
|
jaroslav@1646
|
765 |
arguments2[i] = names2[ni];
|
jaroslav@1646
|
766 |
} else {
|
jaroslav@1646
|
767 |
// replacement position passed
|
jaroslav@1646
|
768 |
arguments2[i] = names2[ni - 1];
|
jaroslav@1646
|
769 |
}
|
jaroslav@1646
|
770 |
} else {
|
jaroslav@1646
|
771 |
arguments2[i] = arg;
|
jaroslav@1646
|
772 |
}
|
jaroslav@1646
|
773 |
}
|
jaroslav@1646
|
774 |
names2[w] = new Name(n.function, arguments2);
|
jaroslav@1646
|
775 |
names2[w].initIndex(w);
|
jaroslav@1646
|
776 |
}
|
jaroslav@1646
|
777 |
}
|
jaroslav@1646
|
778 |
|
jaroslav@1646
|
779 |
int result2 = result == -1 ? -1 : result - 1;
|
jaroslav@1646
|
780 |
return new LambdaForm(debugName, arity2, names2, result2);
|
jaroslav@1646
|
781 |
}
|
jaroslav@1646
|
782 |
|
jaroslav@1646
|
783 |
LambdaForm bind(int namePos, BoundMethodHandle.SpeciesData oldData) {
|
jaroslav@1646
|
784 |
Name name = names[namePos];
|
jaroslav@1646
|
785 |
BoundMethodHandle.SpeciesData newData = oldData.extendWithType(name.type);
|
jaroslav@1646
|
786 |
return bind(name, newData.getterName(names[0], oldData.fieldCount()), oldData, newData);
|
jaroslav@1646
|
787 |
}
|
jaroslav@1646
|
788 |
LambdaForm bind(Name name, Name binding,
|
jaroslav@1646
|
789 |
BoundMethodHandle.SpeciesData oldData,
|
jaroslav@1646
|
790 |
BoundMethodHandle.SpeciesData newData) {
|
jaroslav@1646
|
791 |
int pos = name.index;
|
jaroslav@1646
|
792 |
assert(name.isParam());
|
jaroslav@1646
|
793 |
assert(!binding.isParam());
|
jaroslav@1646
|
794 |
assert(name.type == binding.type);
|
jaroslav@1646
|
795 |
assert(0 <= pos && pos < arity && names[pos] == name);
|
jaroslav@1646
|
796 |
assert(binding.function.memberDeclaringClassOrNull() == newData.clazz);
|
jaroslav@1646
|
797 |
assert(oldData.getters.length == newData.getters.length-1);
|
jaroslav@1646
|
798 |
if (bindCache != null) {
|
jaroslav@1646
|
799 |
LambdaForm form = bindCache[pos];
|
jaroslav@1646
|
800 |
if (form != null) {
|
jaroslav@1646
|
801 |
assert(form.contains(binding)) : "form << " + form + " >> does not contain binding << " + binding + " >>";
|
jaroslav@1646
|
802 |
return form;
|
jaroslav@1646
|
803 |
}
|
jaroslav@1646
|
804 |
} else {
|
jaroslav@1646
|
805 |
bindCache = new LambdaForm[arity];
|
jaroslav@1646
|
806 |
}
|
jaroslav@1646
|
807 |
assert(nameRefsAreLegal());
|
jaroslav@1646
|
808 |
int arity2 = arity-1;
|
jaroslav@1646
|
809 |
Name[] names2 = names.clone();
|
jaroslav@1646
|
810 |
names2[pos] = binding; // we might move this in a moment
|
jaroslav@1646
|
811 |
|
jaroslav@1646
|
812 |
// The newly created LF will run with a different BMH.
|
jaroslav@1646
|
813 |
// Switch over any pre-existing BMH field references to the new BMH class.
|
jaroslav@1646
|
814 |
int firstOldRef = -1;
|
jaroslav@1646
|
815 |
for (int i = 0; i < names2.length; i++) {
|
jaroslav@1646
|
816 |
Name n = names[i];
|
jaroslav@1646
|
817 |
if (n.function != null &&
|
jaroslav@1646
|
818 |
n.function.memberDeclaringClassOrNull() == oldData.clazz) {
|
jaroslav@1646
|
819 |
MethodHandle oldGetter = n.function.resolvedHandle;
|
jaroslav@1646
|
820 |
MethodHandle newGetter = null;
|
jaroslav@1646
|
821 |
for (int j = 0; j < oldData.getters.length; j++) {
|
jaroslav@1646
|
822 |
if (oldGetter == oldData.getters[j])
|
jaroslav@1646
|
823 |
newGetter = newData.getters[j];
|
jaroslav@1646
|
824 |
}
|
jaroslav@1646
|
825 |
if (newGetter != null) {
|
jaroslav@1646
|
826 |
if (firstOldRef < 0) firstOldRef = i;
|
jaroslav@1646
|
827 |
Name n2 = new Name(newGetter, n.arguments);
|
jaroslav@1646
|
828 |
names2[i] = n2;
|
jaroslav@1646
|
829 |
}
|
jaroslav@1646
|
830 |
}
|
jaroslav@1646
|
831 |
}
|
jaroslav@1646
|
832 |
|
jaroslav@1646
|
833 |
// Walk over the new list of names once, in forward order.
|
jaroslav@1646
|
834 |
// Replace references to 'name' with 'binding'.
|
jaroslav@1646
|
835 |
// Replace data structure references to the old BMH species with the new.
|
jaroslav@1646
|
836 |
// This might cause a ripple effect, but it will settle in one pass.
|
jaroslav@1646
|
837 |
assert(firstOldRef < 0 || firstOldRef > pos);
|
jaroslav@1646
|
838 |
for (int i = pos+1; i < names2.length; i++) {
|
jaroslav@1646
|
839 |
if (i <= arity2) continue;
|
jaroslav@1646
|
840 |
names2[i] = names2[i].replaceNames(names, names2, pos, i);
|
jaroslav@1646
|
841 |
}
|
jaroslav@1646
|
842 |
|
jaroslav@1646
|
843 |
// (a0, a1, name=a2, a3, a4) => (a0, a1, a3, a4, binding)
|
jaroslav@1646
|
844 |
int insPos = pos;
|
jaroslav@1646
|
845 |
for (; insPos+1 < names2.length; insPos++) {
|
jaroslav@1646
|
846 |
Name n = names2[insPos+1];
|
jaroslav@1646
|
847 |
if (n.isSiblingBindingBefore(binding)) {
|
jaroslav@1646
|
848 |
names2[insPos] = n;
|
jaroslav@1646
|
849 |
} else {
|
jaroslav@1646
|
850 |
break;
|
jaroslav@1646
|
851 |
}
|
jaroslav@1646
|
852 |
}
|
jaroslav@1646
|
853 |
names2[insPos] = binding;
|
jaroslav@1646
|
854 |
|
jaroslav@1646
|
855 |
// Since we moved some stuff, maybe update the result reference:
|
jaroslav@1646
|
856 |
int result2 = result;
|
jaroslav@1646
|
857 |
if (result2 == pos)
|
jaroslav@1646
|
858 |
result2 = insPos;
|
jaroslav@1646
|
859 |
else if (result2 > pos && result2 <= insPos)
|
jaroslav@1646
|
860 |
result2 -= 1;
|
jaroslav@1646
|
861 |
|
jaroslav@1646
|
862 |
return bindCache[pos] = new LambdaForm(debugName, arity2, names2, result2);
|
jaroslav@1646
|
863 |
}
|
jaroslav@1646
|
864 |
|
jaroslav@1646
|
865 |
boolean contains(Name name) {
|
jaroslav@1646
|
866 |
int pos = name.index();
|
jaroslav@1646
|
867 |
if (pos >= 0) {
|
jaroslav@1646
|
868 |
return pos < names.length && name.equals(names[pos]);
|
jaroslav@1646
|
869 |
}
|
jaroslav@1646
|
870 |
for (int i = arity; i < names.length; i++) {
|
jaroslav@1646
|
871 |
if (name.equals(names[i]))
|
jaroslav@1646
|
872 |
return true;
|
jaroslav@1646
|
873 |
}
|
jaroslav@1646
|
874 |
return false;
|
jaroslav@1646
|
875 |
}
|
jaroslav@1646
|
876 |
|
jaroslav@1646
|
877 |
LambdaForm addArguments(int pos, char... types) {
|
jaroslav@1646
|
878 |
assert(pos <= arity);
|
jaroslav@1646
|
879 |
int length = names.length;
|
jaroslav@1646
|
880 |
int inTypes = types.length;
|
jaroslav@1646
|
881 |
Name[] names2 = Arrays.copyOf(names, length + inTypes);
|
jaroslav@1646
|
882 |
int arity2 = arity + inTypes;
|
jaroslav@1646
|
883 |
int result2 = result;
|
jaroslav@1646
|
884 |
if (result2 >= arity)
|
jaroslav@1646
|
885 |
result2 += inTypes;
|
jaroslav@1646
|
886 |
// names array has MH in slot 0; skip it.
|
jaroslav@1646
|
887 |
int argpos = pos + 1;
|
jaroslav@1646
|
888 |
// Note: The LF constructor will rename names2[argpos...].
|
jaroslav@1646
|
889 |
// Make space for new arguments (shift temporaries).
|
jaroslav@1646
|
890 |
System.arraycopy(names, argpos, names2, argpos + inTypes, length - argpos);
|
jaroslav@1646
|
891 |
for (int i = 0; i < inTypes; i++) {
|
jaroslav@1646
|
892 |
names2[argpos + i] = new Name(types[i]);
|
jaroslav@1646
|
893 |
}
|
jaroslav@1646
|
894 |
return new LambdaForm(debugName, arity2, names2, result2);
|
jaroslav@1646
|
895 |
}
|
jaroslav@1646
|
896 |
|
jaroslav@1646
|
897 |
LambdaForm addArguments(int pos, List<Class<?>> types) {
|
jaroslav@1646
|
898 |
char[] basicTypes = new char[types.size()];
|
jaroslav@1646
|
899 |
for (int i = 0; i < basicTypes.length; i++)
|
jaroslav@1646
|
900 |
basicTypes[i] = basicType(types.get(i));
|
jaroslav@1646
|
901 |
return addArguments(pos, basicTypes);
|
jaroslav@1646
|
902 |
}
|
jaroslav@1646
|
903 |
|
jaroslav@1646
|
904 |
LambdaForm permuteArguments(int skip, int[] reorder, char[] types) {
|
jaroslav@1646
|
905 |
// Note: When inArg = reorder[outArg], outArg is fed by a copy of inArg.
|
jaroslav@1646
|
906 |
// The types are the types of the new (incoming) arguments.
|
jaroslav@1646
|
907 |
int length = names.length;
|
jaroslav@1646
|
908 |
int inTypes = types.length;
|
jaroslav@1646
|
909 |
int outArgs = reorder.length;
|
jaroslav@1646
|
910 |
assert(skip+outArgs == arity);
|
jaroslav@1646
|
911 |
assert(permutedTypesMatch(reorder, types, names, skip));
|
jaroslav@1646
|
912 |
int pos = 0;
|
jaroslav@1646
|
913 |
// skip trivial first part of reordering:
|
jaroslav@1646
|
914 |
while (pos < outArgs && reorder[pos] == pos) pos += 1;
|
jaroslav@1646
|
915 |
Name[] names2 = new Name[length - outArgs + inTypes];
|
jaroslav@1646
|
916 |
System.arraycopy(names, 0, names2, 0, skip+pos);
|
jaroslav@1646
|
917 |
// copy the body:
|
jaroslav@1646
|
918 |
int bodyLength = length - arity;
|
jaroslav@1646
|
919 |
System.arraycopy(names, skip+outArgs, names2, skip+inTypes, bodyLength);
|
jaroslav@1646
|
920 |
int arity2 = names2.length - bodyLength;
|
jaroslav@1646
|
921 |
int result2 = result;
|
jaroslav@1646
|
922 |
if (result2 >= 0) {
|
jaroslav@1646
|
923 |
if (result2 < skip+outArgs) {
|
jaroslav@1646
|
924 |
// return the corresponding inArg
|
jaroslav@1646
|
925 |
result2 = reorder[result2-skip];
|
jaroslav@1646
|
926 |
} else {
|
jaroslav@1646
|
927 |
result2 = result2 - outArgs + inTypes;
|
jaroslav@1646
|
928 |
}
|
jaroslav@1646
|
929 |
}
|
jaroslav@1646
|
930 |
// rework names in the body:
|
jaroslav@1646
|
931 |
for (int j = pos; j < outArgs; j++) {
|
jaroslav@1646
|
932 |
Name n = names[skip+j];
|
jaroslav@1646
|
933 |
int i = reorder[j];
|
jaroslav@1646
|
934 |
// replace names[skip+j] by names2[skip+i]
|
jaroslav@1646
|
935 |
Name n2 = names2[skip+i];
|
jaroslav@1646
|
936 |
if (n2 == null)
|
jaroslav@1646
|
937 |
names2[skip+i] = n2 = new Name(types[i]);
|
jaroslav@1646
|
938 |
else
|
jaroslav@1646
|
939 |
assert(n2.type == types[i]);
|
jaroslav@1646
|
940 |
for (int k = arity2; k < names2.length; k++) {
|
jaroslav@1646
|
941 |
names2[k] = names2[k].replaceName(n, n2);
|
jaroslav@1646
|
942 |
}
|
jaroslav@1646
|
943 |
}
|
jaroslav@1646
|
944 |
// some names are unused, but must be filled in
|
jaroslav@1646
|
945 |
for (int i = skip+pos; i < arity2; i++) {
|
jaroslav@1646
|
946 |
if (names2[i] == null)
|
jaroslav@1646
|
947 |
names2[i] = argument(i, types[i - skip]);
|
jaroslav@1646
|
948 |
}
|
jaroslav@1646
|
949 |
for (int j = arity; j < names.length; j++) {
|
jaroslav@1646
|
950 |
int i = j - arity + arity2;
|
jaroslav@1646
|
951 |
// replace names2[i] by names[j]
|
jaroslav@1646
|
952 |
Name n = names[j];
|
jaroslav@1646
|
953 |
Name n2 = names2[i];
|
jaroslav@1646
|
954 |
if (n != n2) {
|
jaroslav@1646
|
955 |
for (int k = i+1; k < names2.length; k++) {
|
jaroslav@1646
|
956 |
names2[k] = names2[k].replaceName(n, n2);
|
jaroslav@1646
|
957 |
}
|
jaroslav@1646
|
958 |
}
|
jaroslav@1646
|
959 |
}
|
jaroslav@1646
|
960 |
return new LambdaForm(debugName, arity2, names2, result2);
|
jaroslav@1646
|
961 |
}
|
jaroslav@1646
|
962 |
|
jaroslav@1646
|
963 |
static boolean permutedTypesMatch(int[] reorder, char[] types, Name[] names, int skip) {
|
jaroslav@1646
|
964 |
int inTypes = types.length;
|
jaroslav@1646
|
965 |
int outArgs = reorder.length;
|
jaroslav@1646
|
966 |
for (int i = 0; i < outArgs; i++) {
|
jaroslav@1646
|
967 |
assert(names[skip+i].isParam());
|
jaroslav@1646
|
968 |
assert(names[skip+i].type == types[reorder[i]]);
|
jaroslav@1646
|
969 |
}
|
jaroslav@1646
|
970 |
return true;
|
jaroslav@1646
|
971 |
}
|
jaroslav@1646
|
972 |
|
jaroslav@1646
|
973 |
static class NamedFunction {
|
jaroslav@1646
|
974 |
final MemberName member;
|
jaroslav@1646
|
975 |
@Stable MethodHandle resolvedHandle;
|
jaroslav@1646
|
976 |
@Stable MethodHandle invoker;
|
jaroslav@1646
|
977 |
|
jaroslav@1646
|
978 |
NamedFunction(MethodHandle resolvedHandle) {
|
jaroslav@1646
|
979 |
this(resolvedHandle.internalMemberName(), resolvedHandle);
|
jaroslav@1646
|
980 |
}
|
jaroslav@1646
|
981 |
NamedFunction(MemberName member, MethodHandle resolvedHandle) {
|
jaroslav@1646
|
982 |
this.member = member;
|
jaroslav@1646
|
983 |
//resolvedHandle = eraseSubwordTypes(resolvedHandle);
|
jaroslav@1646
|
984 |
this.resolvedHandle = resolvedHandle;
|
jaroslav@1646
|
985 |
}
|
jaroslav@1646
|
986 |
NamedFunction(MethodType basicInvokerType) {
|
jaroslav@1646
|
987 |
assert(basicInvokerType == basicInvokerType.basicType()) : basicInvokerType;
|
jaroslav@1646
|
988 |
if (basicInvokerType.parameterSlotCount() < MethodType.MAX_MH_INVOKER_ARITY) {
|
jaroslav@1646
|
989 |
this.resolvedHandle = basicInvokerType.invokers().basicInvoker();
|
jaroslav@1646
|
990 |
this.member = resolvedHandle.internalMemberName();
|
jaroslav@1646
|
991 |
} else {
|
jaroslav@1646
|
992 |
// necessary to pass BigArityTest
|
jaroslav@1646
|
993 |
this.member = Invokers.invokeBasicMethod(basicInvokerType);
|
jaroslav@1646
|
994 |
}
|
jaroslav@1646
|
995 |
}
|
jaroslav@1646
|
996 |
|
jaroslav@1646
|
997 |
// The next 3 constructors are used to break circular dependencies on MH.invokeStatic, etc.
|
jaroslav@1646
|
998 |
// Any LambdaForm containing such a member is not interpretable.
|
jaroslav@1646
|
999 |
// This is OK, since all such LFs are prepared with special primitive vmentry points.
|
jaroslav@1646
|
1000 |
// And even without the resolvedHandle, the name can still be compiled and optimized.
|
jaroslav@1646
|
1001 |
NamedFunction(Method method) {
|
jaroslav@1646
|
1002 |
this(new MemberName(method));
|
jaroslav@1646
|
1003 |
}
|
jaroslav@1646
|
1004 |
NamedFunction(Field field) {
|
jaroslav@1646
|
1005 |
this(new MemberName(field));
|
jaroslav@1646
|
1006 |
}
|
jaroslav@1646
|
1007 |
NamedFunction(MemberName member) {
|
jaroslav@1646
|
1008 |
this.member = member;
|
jaroslav@1646
|
1009 |
this.resolvedHandle = null;
|
jaroslav@1646
|
1010 |
}
|
jaroslav@1646
|
1011 |
|
jaroslav@1646
|
1012 |
MethodHandle resolvedHandle() {
|
jaroslav@1646
|
1013 |
if (resolvedHandle == null) resolve();
|
jaroslav@1646
|
1014 |
return resolvedHandle;
|
jaroslav@1646
|
1015 |
}
|
jaroslav@1646
|
1016 |
|
jaroslav@1646
|
1017 |
void resolve() {
|
jaroslav@1646
|
1018 |
resolvedHandle = DirectMethodHandle.make(member);
|
jaroslav@1646
|
1019 |
}
|
jaroslav@1646
|
1020 |
|
jaroslav@1646
|
1021 |
@Override
|
jaroslav@1646
|
1022 |
public boolean equals(Object other) {
|
jaroslav@1646
|
1023 |
if (this == other) return true;
|
jaroslav@1646
|
1024 |
if (other == null) return false;
|
jaroslav@1646
|
1025 |
if (!(other instanceof NamedFunction)) return false;
|
jaroslav@1646
|
1026 |
NamedFunction that = (NamedFunction) other;
|
jaroslav@1646
|
1027 |
return this.member != null && this.member.equals(that.member);
|
jaroslav@1646
|
1028 |
}
|
jaroslav@1646
|
1029 |
|
jaroslav@1646
|
1030 |
@Override
|
jaroslav@1646
|
1031 |
public int hashCode() {
|
jaroslav@1646
|
1032 |
if (member != null)
|
jaroslav@1646
|
1033 |
return member.hashCode();
|
jaroslav@1646
|
1034 |
return super.hashCode();
|
jaroslav@1646
|
1035 |
}
|
jaroslav@1646
|
1036 |
|
jaroslav@1646
|
1037 |
// Put the predefined NamedFunction invokers into the table.
|
jaroslav@1646
|
1038 |
static void initializeInvokers() {
|
jaroslav@1646
|
1039 |
for (MemberName m : MemberName.getFactory().getMethods(NamedFunction.class, false, null, null, null)) {
|
jaroslav@1646
|
1040 |
if (!m.isStatic() || !m.isPackage()) continue;
|
jaroslav@1646
|
1041 |
MethodType type = m.getMethodType();
|
jaroslav@1646
|
1042 |
if (type.equals(INVOKER_METHOD_TYPE) &&
|
jaroslav@1646
|
1043 |
m.getName().startsWith("invoke_")) {
|
jaroslav@1646
|
1044 |
String sig = m.getName().substring("invoke_".length());
|
jaroslav@1646
|
1045 |
int arity = LambdaForm.signatureArity(sig);
|
jaroslav@1646
|
1046 |
MethodType srcType = MethodType.genericMethodType(arity);
|
jaroslav@1646
|
1047 |
if (LambdaForm.signatureReturn(sig) == 'V')
|
jaroslav@1646
|
1048 |
srcType = srcType.changeReturnType(void.class);
|
jaroslav@1646
|
1049 |
MethodTypeForm typeForm = srcType.form();
|
jaroslav@1646
|
1050 |
typeForm.namedFunctionInvoker = DirectMethodHandle.make(m);
|
jaroslav@1646
|
1051 |
}
|
jaroslav@1646
|
1052 |
}
|
jaroslav@1646
|
1053 |
}
|
jaroslav@1646
|
1054 |
|
jaroslav@1646
|
1055 |
// The following are predefined NamedFunction invokers. The system must build
|
jaroslav@1646
|
1056 |
// a separate invoker for each distinct signature.
|
jaroslav@1646
|
1057 |
/** void return type invokers. */
|
jaroslav@1646
|
1058 |
@Hidden
|
jaroslav@1646
|
1059 |
static Object invoke__V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1060 |
assert(a.length == 0);
|
jaroslav@1646
|
1061 |
mh.invokeBasic();
|
jaroslav@1646
|
1062 |
return null;
|
jaroslav@1646
|
1063 |
}
|
jaroslav@1646
|
1064 |
@Hidden
|
jaroslav@1646
|
1065 |
static Object invoke_L_V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1066 |
assert(a.length == 1);
|
jaroslav@1646
|
1067 |
mh.invokeBasic(a[0]);
|
jaroslav@1646
|
1068 |
return null;
|
jaroslav@1646
|
1069 |
}
|
jaroslav@1646
|
1070 |
@Hidden
|
jaroslav@1646
|
1071 |
static Object invoke_LL_V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1072 |
assert(a.length == 2);
|
jaroslav@1646
|
1073 |
mh.invokeBasic(a[0], a[1]);
|
jaroslav@1646
|
1074 |
return null;
|
jaroslav@1646
|
1075 |
}
|
jaroslav@1646
|
1076 |
@Hidden
|
jaroslav@1646
|
1077 |
static Object invoke_LLL_V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1078 |
assert(a.length == 3);
|
jaroslav@1646
|
1079 |
mh.invokeBasic(a[0], a[1], a[2]);
|
jaroslav@1646
|
1080 |
return null;
|
jaroslav@1646
|
1081 |
}
|
jaroslav@1646
|
1082 |
@Hidden
|
jaroslav@1646
|
1083 |
static Object invoke_LLLL_V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1084 |
assert(a.length == 4);
|
jaroslav@1646
|
1085 |
mh.invokeBasic(a[0], a[1], a[2], a[3]);
|
jaroslav@1646
|
1086 |
return null;
|
jaroslav@1646
|
1087 |
}
|
jaroslav@1646
|
1088 |
@Hidden
|
jaroslav@1646
|
1089 |
static Object invoke_LLLLL_V(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1090 |
assert(a.length == 5);
|
jaroslav@1646
|
1091 |
mh.invokeBasic(a[0], a[1], a[2], a[3], a[4]);
|
jaroslav@1646
|
1092 |
return null;
|
jaroslav@1646
|
1093 |
}
|
jaroslav@1646
|
1094 |
/** Object return type invokers. */
|
jaroslav@1646
|
1095 |
@Hidden
|
jaroslav@1646
|
1096 |
static Object invoke__L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1097 |
assert(a.length == 0);
|
jaroslav@1646
|
1098 |
return mh.invokeBasic();
|
jaroslav@1646
|
1099 |
}
|
jaroslav@1646
|
1100 |
@Hidden
|
jaroslav@1646
|
1101 |
static Object invoke_L_L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1102 |
assert(a.length == 1);
|
jaroslav@1646
|
1103 |
return mh.invokeBasic(a[0]);
|
jaroslav@1646
|
1104 |
}
|
jaroslav@1646
|
1105 |
@Hidden
|
jaroslav@1646
|
1106 |
static Object invoke_LL_L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1107 |
assert(a.length == 2);
|
jaroslav@1646
|
1108 |
return mh.invokeBasic(a[0], a[1]);
|
jaroslav@1646
|
1109 |
}
|
jaroslav@1646
|
1110 |
@Hidden
|
jaroslav@1646
|
1111 |
static Object invoke_LLL_L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1112 |
assert(a.length == 3);
|
jaroslav@1646
|
1113 |
return mh.invokeBasic(a[0], a[1], a[2]);
|
jaroslav@1646
|
1114 |
}
|
jaroslav@1646
|
1115 |
@Hidden
|
jaroslav@1646
|
1116 |
static Object invoke_LLLL_L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1117 |
assert(a.length == 4);
|
jaroslav@1646
|
1118 |
return mh.invokeBasic(a[0], a[1], a[2], a[3]);
|
jaroslav@1646
|
1119 |
}
|
jaroslav@1646
|
1120 |
@Hidden
|
jaroslav@1646
|
1121 |
static Object invoke_LLLLL_L(MethodHandle mh, Object[] a) throws Throwable {
|
jaroslav@1646
|
1122 |
assert(a.length == 5);
|
jaroslav@1646
|
1123 |
return mh.invokeBasic(a[0], a[1], a[2], a[3], a[4]);
|
jaroslav@1646
|
1124 |
}
|
jaroslav@1646
|
1125 |
|
jaroslav@1646
|
1126 |
static final MethodType INVOKER_METHOD_TYPE =
|
jaroslav@1646
|
1127 |
MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
|
jaroslav@1646
|
1128 |
|
jaroslav@1646
|
1129 |
private static MethodHandle computeInvoker(MethodTypeForm typeForm) {
|
jaroslav@1646
|
1130 |
MethodHandle mh = typeForm.namedFunctionInvoker;
|
jaroslav@1646
|
1131 |
if (mh != null) return mh;
|
jaroslav@1646
|
1132 |
MemberName invoker = InvokerBytecodeGenerator.generateNamedFunctionInvoker(typeForm); // this could take a while
|
jaroslav@1646
|
1133 |
mh = DirectMethodHandle.make(invoker);
|
jaroslav@1646
|
1134 |
MethodHandle mh2 = typeForm.namedFunctionInvoker;
|
jaroslav@1646
|
1135 |
if (mh2 != null) return mh2; // benign race
|
jaroslav@1646
|
1136 |
if (!mh.type().equals(INVOKER_METHOD_TYPE))
|
jaroslav@1646
|
1137 |
throw new InternalError(mh.debugString());
|
jaroslav@1646
|
1138 |
return typeForm.namedFunctionInvoker = mh;
|
jaroslav@1646
|
1139 |
}
|
jaroslav@1646
|
1140 |
|
jaroslav@1646
|
1141 |
@Hidden
|
jaroslav@1646
|
1142 |
Object invokeWithArguments(Object... arguments) throws Throwable {
|
jaroslav@1646
|
1143 |
// If we have a cached invoker, call it right away.
|
jaroslav@1646
|
1144 |
// NOTE: The invoker always returns a reference value.
|
jaroslav@1646
|
1145 |
if (TRACE_INTERPRETER) return invokeWithArgumentsTracing(arguments);
|
jaroslav@1646
|
1146 |
assert(checkArgumentTypes(arguments, methodType()));
|
jaroslav@1646
|
1147 |
return invoker().invokeBasic(resolvedHandle(), arguments);
|
jaroslav@1646
|
1148 |
}
|
jaroslav@1646
|
1149 |
|
jaroslav@1646
|
1150 |
@Hidden
|
jaroslav@1646
|
1151 |
Object invokeWithArgumentsTracing(Object[] arguments) throws Throwable {
|
jaroslav@1646
|
1152 |
Object rval;
|
jaroslav@1646
|
1153 |
try {
|
jaroslav@1646
|
1154 |
traceInterpreter("[ call", this, arguments);
|
jaroslav@1646
|
1155 |
if (invoker == null) {
|
jaroslav@1646
|
1156 |
traceInterpreter("| getInvoker", this);
|
jaroslav@1646
|
1157 |
invoker();
|
jaroslav@1646
|
1158 |
}
|
jaroslav@1646
|
1159 |
if (resolvedHandle == null) {
|
jaroslav@1646
|
1160 |
traceInterpreter("| resolve", this);
|
jaroslav@1646
|
1161 |
resolvedHandle();
|
jaroslav@1646
|
1162 |
}
|
jaroslav@1646
|
1163 |
assert(checkArgumentTypes(arguments, methodType()));
|
jaroslav@1646
|
1164 |
rval = invoker().invokeBasic(resolvedHandle(), arguments);
|
jaroslav@1646
|
1165 |
} catch (Throwable ex) {
|
jaroslav@1646
|
1166 |
traceInterpreter("] throw =>", ex);
|
jaroslav@1646
|
1167 |
throw ex;
|
jaroslav@1646
|
1168 |
}
|
jaroslav@1646
|
1169 |
traceInterpreter("] return =>", rval);
|
jaroslav@1646
|
1170 |
return rval;
|
jaroslav@1646
|
1171 |
}
|
jaroslav@1646
|
1172 |
|
jaroslav@1646
|
1173 |
private MethodHandle invoker() {
|
jaroslav@1646
|
1174 |
if (invoker != null) return invoker;
|
jaroslav@1646
|
1175 |
// Get an invoker and cache it.
|
jaroslav@1646
|
1176 |
return invoker = computeInvoker(methodType().form());
|
jaroslav@1646
|
1177 |
}
|
jaroslav@1646
|
1178 |
|
jaroslav@1646
|
1179 |
private static boolean checkArgumentTypes(Object[] arguments, MethodType methodType) {
|
jaroslav@1646
|
1180 |
if (true) return true; // FIXME
|
jaroslav@1646
|
1181 |
MethodType dstType = methodType.form().erasedType();
|
jaroslav@1646
|
1182 |
MethodType srcType = dstType.basicType().wrap();
|
jaroslav@1646
|
1183 |
Class<?>[] ptypes = new Class<?>[arguments.length];
|
jaroslav@1646
|
1184 |
for (int i = 0; i < arguments.length; i++) {
|
jaroslav@1646
|
1185 |
Object arg = arguments[i];
|
jaroslav@1646
|
1186 |
Class<?> ptype = arg == null ? Object.class : arg.getClass();
|
jaroslav@1646
|
1187 |
// If the dest. type is a primitive we keep the
|
jaroslav@1646
|
1188 |
// argument type.
|
jaroslav@1646
|
1189 |
ptypes[i] = dstType.parameterType(i).isPrimitive() ? ptype : Object.class;
|
jaroslav@1646
|
1190 |
}
|
jaroslav@1646
|
1191 |
MethodType argType = MethodType.methodType(srcType.returnType(), ptypes).wrap();
|
jaroslav@1646
|
1192 |
assert(argType.isConvertibleTo(srcType)) : "wrong argument types: cannot convert " + argType + " to " + srcType;
|
jaroslav@1646
|
1193 |
return true;
|
jaroslav@1646
|
1194 |
}
|
jaroslav@1646
|
1195 |
|
jaroslav@1646
|
1196 |
String basicTypeSignature() {
|
jaroslav@1646
|
1197 |
//return LambdaForm.basicTypeSignature(resolvedHandle.type());
|
jaroslav@1646
|
1198 |
return LambdaForm.basicTypeSignature(methodType());
|
jaroslav@1646
|
1199 |
}
|
jaroslav@1646
|
1200 |
|
jaroslav@1646
|
1201 |
MethodType methodType() {
|
jaroslav@1646
|
1202 |
if (resolvedHandle != null)
|
jaroslav@1646
|
1203 |
return resolvedHandle.type();
|
jaroslav@1646
|
1204 |
else
|
jaroslav@1646
|
1205 |
// only for certain internal LFs during bootstrapping
|
jaroslav@1646
|
1206 |
return member.getInvocationType();
|
jaroslav@1646
|
1207 |
}
|
jaroslav@1646
|
1208 |
|
jaroslav@1646
|
1209 |
MemberName member() {
|
jaroslav@1646
|
1210 |
assert(assertMemberIsConsistent());
|
jaroslav@1646
|
1211 |
return member;
|
jaroslav@1646
|
1212 |
}
|
jaroslav@1646
|
1213 |
|
jaroslav@1646
|
1214 |
// Called only from assert.
|
jaroslav@1646
|
1215 |
private boolean assertMemberIsConsistent() {
|
jaroslav@1646
|
1216 |
if (resolvedHandle instanceof DirectMethodHandle) {
|
jaroslav@1646
|
1217 |
MemberName m = resolvedHandle.internalMemberName();
|
jaroslav@1646
|
1218 |
assert(m.equals(member));
|
jaroslav@1646
|
1219 |
}
|
jaroslav@1646
|
1220 |
return true;
|
jaroslav@1646
|
1221 |
}
|
jaroslav@1646
|
1222 |
|
jaroslav@1646
|
1223 |
Class<?> memberDeclaringClassOrNull() {
|
jaroslav@1646
|
1224 |
return (member == null) ? null : member.getDeclaringClass();
|
jaroslav@1646
|
1225 |
}
|
jaroslav@1646
|
1226 |
|
jaroslav@1646
|
1227 |
char returnType() {
|
jaroslav@1646
|
1228 |
return basicType(methodType().returnType());
|
jaroslav@1646
|
1229 |
}
|
jaroslav@1646
|
1230 |
|
jaroslav@1646
|
1231 |
char parameterType(int n) {
|
jaroslav@1646
|
1232 |
return basicType(methodType().parameterType(n));
|
jaroslav@1646
|
1233 |
}
|
jaroslav@1646
|
1234 |
|
jaroslav@1646
|
1235 |
int arity() {
|
jaroslav@1646
|
1236 |
//int siglen = member.getMethodType().parameterCount();
|
jaroslav@1646
|
1237 |
//if (!member.isStatic()) siglen += 1;
|
jaroslav@1646
|
1238 |
//return siglen;
|
jaroslav@1646
|
1239 |
return methodType().parameterCount();
|
jaroslav@1646
|
1240 |
}
|
jaroslav@1646
|
1241 |
|
jaroslav@1646
|
1242 |
public String toString() {
|
jaroslav@1646
|
1243 |
if (member == null) return String.valueOf(resolvedHandle);
|
jaroslav@1646
|
1244 |
return member.getDeclaringClass().getSimpleName()+"."+member.getName();
|
jaroslav@1646
|
1245 |
}
|
jaroslav@1646
|
1246 |
}
|
jaroslav@1646
|
1247 |
|
jaroslav@1646
|
1248 |
void resolve() {
|
jaroslav@1646
|
1249 |
for (Name n : names) n.resolve();
|
jaroslav@1646
|
1250 |
}
|
jaroslav@1646
|
1251 |
|
jaroslav@1646
|
1252 |
public static char basicType(Class<?> type) {
|
jaroslav@1646
|
1253 |
char c = Wrapper.basicTypeChar(type);
|
jaroslav@1646
|
1254 |
if ("ZBSC".indexOf(c) >= 0) c = 'I';
|
jaroslav@1646
|
1255 |
assert("LIJFDV".indexOf(c) >= 0);
|
jaroslav@1646
|
1256 |
return c;
|
jaroslav@1646
|
1257 |
}
|
jaroslav@1646
|
1258 |
public static char[] basicTypes(List<Class<?>> types) {
|
jaroslav@1646
|
1259 |
char[] btypes = new char[types.size()];
|
jaroslav@1646
|
1260 |
for (int i = 0; i < btypes.length; i++) {
|
jaroslav@1646
|
1261 |
btypes[i] = basicType(types.get(i));
|
jaroslav@1646
|
1262 |
}
|
jaroslav@1646
|
1263 |
return btypes;
|
jaroslav@1646
|
1264 |
}
|
jaroslav@1646
|
1265 |
public static String basicTypeSignature(MethodType type) {
|
jaroslav@1646
|
1266 |
char[] sig = new char[type.parameterCount() + 2];
|
jaroslav@1646
|
1267 |
int sigp = 0;
|
jaroslav@1646
|
1268 |
for (Class<?> pt : type.parameterList()) {
|
jaroslav@1646
|
1269 |
sig[sigp++] = basicType(pt);
|
jaroslav@1646
|
1270 |
}
|
jaroslav@1646
|
1271 |
sig[sigp++] = '_';
|
jaroslav@1646
|
1272 |
sig[sigp++] = basicType(type.returnType());
|
jaroslav@1646
|
1273 |
assert(sigp == sig.length);
|
jaroslav@1646
|
1274 |
return String.valueOf(sig);
|
jaroslav@1646
|
1275 |
}
|
jaroslav@1646
|
1276 |
|
jaroslav@1646
|
1277 |
static final class Name {
|
jaroslav@1646
|
1278 |
final char type;
|
jaroslav@1646
|
1279 |
private short index;
|
jaroslav@1646
|
1280 |
final NamedFunction function;
|
jaroslav@1646
|
1281 |
@Stable final Object[] arguments;
|
jaroslav@1646
|
1282 |
|
jaroslav@1646
|
1283 |
private Name(int index, char type, NamedFunction function, Object[] arguments) {
|
jaroslav@1646
|
1284 |
this.index = (short)index;
|
jaroslav@1646
|
1285 |
this.type = type;
|
jaroslav@1646
|
1286 |
this.function = function;
|
jaroslav@1646
|
1287 |
this.arguments = arguments;
|
jaroslav@1646
|
1288 |
assert(this.index == index);
|
jaroslav@1646
|
1289 |
}
|
jaroslav@1646
|
1290 |
Name(MethodHandle function, Object... arguments) {
|
jaroslav@1646
|
1291 |
this(new NamedFunction(function), arguments);
|
jaroslav@1646
|
1292 |
}
|
jaroslav@1646
|
1293 |
Name(MethodType functionType, Object... arguments) {
|
jaroslav@1646
|
1294 |
this(new NamedFunction(functionType), arguments);
|
jaroslav@1646
|
1295 |
assert(arguments[0] instanceof Name && ((Name)arguments[0]).type == 'L');
|
jaroslav@1646
|
1296 |
}
|
jaroslav@1646
|
1297 |
Name(MemberName function, Object... arguments) {
|
jaroslav@1646
|
1298 |
this(new NamedFunction(function), arguments);
|
jaroslav@1646
|
1299 |
}
|
jaroslav@1646
|
1300 |
Name(NamedFunction function, Object... arguments) {
|
jaroslav@1646
|
1301 |
this(-1, function.returnType(), function, arguments = arguments.clone());
|
jaroslav@1646
|
1302 |
assert(arguments.length == function.arity()) : "arity mismatch: arguments.length=" + arguments.length + " == function.arity()=" + function.arity() + " in " + debugString();
|
jaroslav@1646
|
1303 |
for (int i = 0; i < arguments.length; i++)
|
jaroslav@1646
|
1304 |
assert(typesMatch(function.parameterType(i), arguments[i])) : "types don't match: function.parameterType(" + i + ")=" + function.parameterType(i) + ", arguments[" + i + "]=" + arguments[i] + " in " + debugString();
|
jaroslav@1646
|
1305 |
}
|
jaroslav@1646
|
1306 |
Name(int index, char type) {
|
jaroslav@1646
|
1307 |
this(index, type, null, null);
|
jaroslav@1646
|
1308 |
}
|
jaroslav@1646
|
1309 |
Name(char type) {
|
jaroslav@1646
|
1310 |
this(-1, type);
|
jaroslav@1646
|
1311 |
}
|
jaroslav@1646
|
1312 |
|
jaroslav@1646
|
1313 |
char type() { return type; }
|
jaroslav@1646
|
1314 |
int index() { return index; }
|
jaroslav@1646
|
1315 |
boolean initIndex(int i) {
|
jaroslav@1646
|
1316 |
if (index != i) {
|
jaroslav@1646
|
1317 |
if (index != -1) return false;
|
jaroslav@1646
|
1318 |
index = (short)i;
|
jaroslav@1646
|
1319 |
}
|
jaroslav@1646
|
1320 |
return true;
|
jaroslav@1646
|
1321 |
}
|
jaroslav@1646
|
1322 |
|
jaroslav@1646
|
1323 |
|
jaroslav@1646
|
1324 |
void resolve() {
|
jaroslav@1646
|
1325 |
if (function != null)
|
jaroslav@1646
|
1326 |
function.resolve();
|
jaroslav@1646
|
1327 |
}
|
jaroslav@1646
|
1328 |
|
jaroslav@1646
|
1329 |
Name newIndex(int i) {
|
jaroslav@1646
|
1330 |
if (initIndex(i)) return this;
|
jaroslav@1646
|
1331 |
return cloneWithIndex(i);
|
jaroslav@1646
|
1332 |
}
|
jaroslav@1646
|
1333 |
Name cloneWithIndex(int i) {
|
jaroslav@1646
|
1334 |
Object[] newArguments = (arguments == null) ? null : arguments.clone();
|
jaroslav@1646
|
1335 |
return new Name(i, type, function, newArguments);
|
jaroslav@1646
|
1336 |
}
|
jaroslav@1646
|
1337 |
Name replaceName(Name oldName, Name newName) { // FIXME: use replaceNames uniformly
|
jaroslav@1646
|
1338 |
if (oldName == newName) return this;
|
jaroslav@1646
|
1339 |
@SuppressWarnings("LocalVariableHidesMemberVariable")
|
jaroslav@1646
|
1340 |
Object[] arguments = this.arguments;
|
jaroslav@1646
|
1341 |
if (arguments == null) return this;
|
jaroslav@1646
|
1342 |
boolean replaced = false;
|
jaroslav@1646
|
1343 |
for (int j = 0; j < arguments.length; j++) {
|
jaroslav@1646
|
1344 |
if (arguments[j] == oldName) {
|
jaroslav@1646
|
1345 |
if (!replaced) {
|
jaroslav@1646
|
1346 |
replaced = true;
|
jaroslav@1646
|
1347 |
arguments = arguments.clone();
|
jaroslav@1646
|
1348 |
}
|
jaroslav@1646
|
1349 |
arguments[j] = newName;
|
jaroslav@1646
|
1350 |
}
|
jaroslav@1646
|
1351 |
}
|
jaroslav@1646
|
1352 |
if (!replaced) return this;
|
jaroslav@1646
|
1353 |
return new Name(function, arguments);
|
jaroslav@1646
|
1354 |
}
|
jaroslav@1646
|
1355 |
Name replaceNames(Name[] oldNames, Name[] newNames, int start, int end) {
|
jaroslav@1646
|
1356 |
@SuppressWarnings("LocalVariableHidesMemberVariable")
|
jaroslav@1646
|
1357 |
Object[] arguments = this.arguments;
|
jaroslav@1646
|
1358 |
boolean replaced = false;
|
jaroslav@1646
|
1359 |
eachArg:
|
jaroslav@1646
|
1360 |
for (int j = 0; j < arguments.length; j++) {
|
jaroslav@1646
|
1361 |
if (arguments[j] instanceof Name) {
|
jaroslav@1646
|
1362 |
Name n = (Name) arguments[j];
|
jaroslav@1646
|
1363 |
int check = n.index;
|
jaroslav@1646
|
1364 |
// harmless check to see if the thing is already in newNames:
|
jaroslav@1646
|
1365 |
if (check >= 0 && check < newNames.length && n == newNames[check])
|
jaroslav@1646
|
1366 |
continue eachArg;
|
jaroslav@1646
|
1367 |
// n might not have the correct index: n != oldNames[n.index].
|
jaroslav@1646
|
1368 |
for (int i = start; i < end; i++) {
|
jaroslav@1646
|
1369 |
if (n == oldNames[i]) {
|
jaroslav@1646
|
1370 |
if (n == newNames[i])
|
jaroslav@1646
|
1371 |
continue eachArg;
|
jaroslav@1646
|
1372 |
if (!replaced) {
|
jaroslav@1646
|
1373 |
replaced = true;
|
jaroslav@1646
|
1374 |
arguments = arguments.clone();
|
jaroslav@1646
|
1375 |
}
|
jaroslav@1646
|
1376 |
arguments[j] = newNames[i];
|
jaroslav@1646
|
1377 |
continue eachArg;
|
jaroslav@1646
|
1378 |
}
|
jaroslav@1646
|
1379 |
}
|
jaroslav@1646
|
1380 |
}
|
jaroslav@1646
|
1381 |
}
|
jaroslav@1646
|
1382 |
if (!replaced) return this;
|
jaroslav@1646
|
1383 |
return new Name(function, arguments);
|
jaroslav@1646
|
1384 |
}
|
jaroslav@1646
|
1385 |
void internArguments() {
|
jaroslav@1646
|
1386 |
@SuppressWarnings("LocalVariableHidesMemberVariable")
|
jaroslav@1646
|
1387 |
Object[] arguments = this.arguments;
|
jaroslav@1646
|
1388 |
for (int j = 0; j < arguments.length; j++) {
|
jaroslav@1646
|
1389 |
if (arguments[j] instanceof Name) {
|
jaroslav@1646
|
1390 |
Name n = (Name) arguments[j];
|
jaroslav@1646
|
1391 |
if (n.isParam() && n.index < INTERNED_ARGUMENT_LIMIT)
|
jaroslav@1646
|
1392 |
arguments[j] = internArgument(n);
|
jaroslav@1646
|
1393 |
}
|
jaroslav@1646
|
1394 |
}
|
jaroslav@1646
|
1395 |
}
|
jaroslav@1646
|
1396 |
boolean isParam() {
|
jaroslav@1646
|
1397 |
return function == null;
|
jaroslav@1646
|
1398 |
}
|
jaroslav@1646
|
1399 |
boolean isConstantZero() {
|
jaroslav@1646
|
1400 |
return !isParam() && arguments.length == 0 && function.equals(constantZero(0, type).function);
|
jaroslav@1646
|
1401 |
}
|
jaroslav@1646
|
1402 |
|
jaroslav@1646
|
1403 |
public String toString() {
|
jaroslav@1646
|
1404 |
return (isParam()?"a":"t")+(index >= 0 ? index : System.identityHashCode(this))+":"+type;
|
jaroslav@1646
|
1405 |
}
|
jaroslav@1646
|
1406 |
public String debugString() {
|
jaroslav@1646
|
1407 |
String s = toString();
|
jaroslav@1646
|
1408 |
return (function == null) ? s : s + "=" + exprString();
|
jaroslav@1646
|
1409 |
}
|
jaroslav@1646
|
1410 |
public String exprString() {
|
jaroslav@1646
|
1411 |
if (function == null) return "null";
|
jaroslav@1646
|
1412 |
StringBuilder buf = new StringBuilder(function.toString());
|
jaroslav@1646
|
1413 |
buf.append("(");
|
jaroslav@1646
|
1414 |
String cma = "";
|
jaroslav@1646
|
1415 |
for (Object a : arguments) {
|
jaroslav@1646
|
1416 |
buf.append(cma); cma = ",";
|
jaroslav@1646
|
1417 |
if (a instanceof Name || a instanceof Integer)
|
jaroslav@1646
|
1418 |
buf.append(a);
|
jaroslav@1646
|
1419 |
else
|
jaroslav@1646
|
1420 |
buf.append("(").append(a).append(")");
|
jaroslav@1646
|
1421 |
}
|
jaroslav@1646
|
1422 |
buf.append(")");
|
jaroslav@1646
|
1423 |
return buf.toString();
|
jaroslav@1646
|
1424 |
}
|
jaroslav@1646
|
1425 |
|
jaroslav@1646
|
1426 |
private static boolean typesMatch(char parameterType, Object object) {
|
jaroslav@1646
|
1427 |
if (object instanceof Name) {
|
jaroslav@1646
|
1428 |
return ((Name)object).type == parameterType;
|
jaroslav@1646
|
1429 |
}
|
jaroslav@1646
|
1430 |
switch (parameterType) {
|
jaroslav@1646
|
1431 |
case 'I': return object instanceof Integer;
|
jaroslav@1646
|
1432 |
case 'J': return object instanceof Long;
|
jaroslav@1646
|
1433 |
case 'F': return object instanceof Float;
|
jaroslav@1646
|
1434 |
case 'D': return object instanceof Double;
|
jaroslav@1646
|
1435 |
}
|
jaroslav@1646
|
1436 |
assert(parameterType == 'L');
|
jaroslav@1646
|
1437 |
return true;
|
jaroslav@1646
|
1438 |
}
|
jaroslav@1646
|
1439 |
|
jaroslav@1646
|
1440 |
/**
|
jaroslav@1646
|
1441 |
* Does this Name precede the given binding node in some canonical order?
|
jaroslav@1646
|
1442 |
* This predicate is used to order data bindings (via insertion sort)
|
jaroslav@1646
|
1443 |
* with some stability.
|
jaroslav@1646
|
1444 |
*/
|
jaroslav@1646
|
1445 |
boolean isSiblingBindingBefore(Name binding) {
|
jaroslav@1646
|
1446 |
assert(!binding.isParam());
|
jaroslav@1646
|
1447 |
if (isParam()) return true;
|
jaroslav@1646
|
1448 |
if (function.equals(binding.function) &&
|
jaroslav@1646
|
1449 |
arguments.length == binding.arguments.length) {
|
jaroslav@1646
|
1450 |
boolean sawInt = false;
|
jaroslav@1646
|
1451 |
for (int i = 0; i < arguments.length; i++) {
|
jaroslav@1646
|
1452 |
Object a1 = arguments[i];
|
jaroslav@1646
|
1453 |
Object a2 = binding.arguments[i];
|
jaroslav@1646
|
1454 |
if (!a1.equals(a2)) {
|
jaroslav@1646
|
1455 |
if (a1 instanceof Integer && a2 instanceof Integer) {
|
jaroslav@1646
|
1456 |
if (sawInt) continue;
|
jaroslav@1646
|
1457 |
sawInt = true;
|
jaroslav@1646
|
1458 |
if ((int)a1 < (int)a2) continue; // still might be true
|
jaroslav@1646
|
1459 |
}
|
jaroslav@1646
|
1460 |
return false;
|
jaroslav@1646
|
1461 |
}
|
jaroslav@1646
|
1462 |
}
|
jaroslav@1646
|
1463 |
return sawInt;
|
jaroslav@1646
|
1464 |
}
|
jaroslav@1646
|
1465 |
return false;
|
jaroslav@1646
|
1466 |
}
|
jaroslav@1646
|
1467 |
|
jaroslav@1646
|
1468 |
public boolean equals(Name that) {
|
jaroslav@1646
|
1469 |
if (this == that) return true;
|
jaroslav@1646
|
1470 |
if (isParam())
|
jaroslav@1646
|
1471 |
// each parameter is a unique atom
|
jaroslav@1646
|
1472 |
return false; // this != that
|
jaroslav@1646
|
1473 |
return
|
jaroslav@1646
|
1474 |
//this.index == that.index &&
|
jaroslav@1646
|
1475 |
this.type == that.type &&
|
jaroslav@1646
|
1476 |
this.function.equals(that.function) &&
|
jaroslav@1646
|
1477 |
Arrays.equals(this.arguments, that.arguments);
|
jaroslav@1646
|
1478 |
}
|
jaroslav@1646
|
1479 |
@Override
|
jaroslav@1646
|
1480 |
public boolean equals(Object x) {
|
jaroslav@1646
|
1481 |
return x instanceof Name && equals((Name)x);
|
jaroslav@1646
|
1482 |
}
|
jaroslav@1646
|
1483 |
@Override
|
jaroslav@1646
|
1484 |
public int hashCode() {
|
jaroslav@1646
|
1485 |
if (isParam())
|
jaroslav@1646
|
1486 |
return index | (type << 8);
|
jaroslav@1646
|
1487 |
return function.hashCode() ^ Arrays.hashCode(arguments);
|
jaroslav@1646
|
1488 |
}
|
jaroslav@1646
|
1489 |
}
|
jaroslav@1646
|
1490 |
|
jaroslav@1646
|
1491 |
static Name argument(int which, char type) {
|
jaroslav@1646
|
1492 |
int tn = ALL_TYPES.indexOf(type);
|
jaroslav@1646
|
1493 |
if (tn < 0 || which >= INTERNED_ARGUMENT_LIMIT)
|
jaroslav@1646
|
1494 |
return new Name(which, type);
|
jaroslav@1646
|
1495 |
return INTERNED_ARGUMENTS[tn][which];
|
jaroslav@1646
|
1496 |
}
|
jaroslav@1646
|
1497 |
static Name internArgument(Name n) {
|
jaroslav@1646
|
1498 |
assert(n.isParam()) : "not param: " + n;
|
jaroslav@1646
|
1499 |
assert(n.index < INTERNED_ARGUMENT_LIMIT);
|
jaroslav@1646
|
1500 |
return argument(n.index, n.type);
|
jaroslav@1646
|
1501 |
}
|
jaroslav@1646
|
1502 |
static Name[] arguments(int extra, String types) {
|
jaroslav@1646
|
1503 |
int length = types.length();
|
jaroslav@1646
|
1504 |
Name[] names = new Name[length + extra];
|
jaroslav@1646
|
1505 |
for (int i = 0; i < length; i++)
|
jaroslav@1646
|
1506 |
names[i] = argument(i, types.charAt(i));
|
jaroslav@1646
|
1507 |
return names;
|
jaroslav@1646
|
1508 |
}
|
jaroslav@1646
|
1509 |
static Name[] arguments(int extra, char... types) {
|
jaroslav@1646
|
1510 |
int length = types.length;
|
jaroslav@1646
|
1511 |
Name[] names = new Name[length + extra];
|
jaroslav@1646
|
1512 |
for (int i = 0; i < length; i++)
|
jaroslav@1646
|
1513 |
names[i] = argument(i, types[i]);
|
jaroslav@1646
|
1514 |
return names;
|
jaroslav@1646
|
1515 |
}
|
jaroslav@1646
|
1516 |
static Name[] arguments(int extra, List<Class<?>> types) {
|
jaroslav@1646
|
1517 |
int length = types.size();
|
jaroslav@1646
|
1518 |
Name[] names = new Name[length + extra];
|
jaroslav@1646
|
1519 |
for (int i = 0; i < length; i++)
|
jaroslav@1646
|
1520 |
names[i] = argument(i, basicType(types.get(i)));
|
jaroslav@1646
|
1521 |
return names;
|
jaroslav@1646
|
1522 |
}
|
jaroslav@1646
|
1523 |
static Name[] arguments(int extra, Class<?>... types) {
|
jaroslav@1646
|
1524 |
int length = types.length;
|
jaroslav@1646
|
1525 |
Name[] names = new Name[length + extra];
|
jaroslav@1646
|
1526 |
for (int i = 0; i < length; i++)
|
jaroslav@1646
|
1527 |
names[i] = argument(i, basicType(types[i]));
|
jaroslav@1646
|
1528 |
return names;
|
jaroslav@1646
|
1529 |
}
|
jaroslav@1646
|
1530 |
static Name[] arguments(int extra, MethodType types) {
|
jaroslav@1646
|
1531 |
int length = types.parameterCount();
|
jaroslav@1646
|
1532 |
Name[] names = new Name[length + extra];
|
jaroslav@1646
|
1533 |
for (int i = 0; i < length; i++)
|
jaroslav@1646
|
1534 |
names[i] = argument(i, basicType(types.parameterType(i)));
|
jaroslav@1646
|
1535 |
return names;
|
jaroslav@1646
|
1536 |
}
|
jaroslav@1646
|
1537 |
static final String ALL_TYPES = "LIJFD"; // omit V, not an argument type
|
jaroslav@1646
|
1538 |
static final int INTERNED_ARGUMENT_LIMIT = 10;
|
jaroslav@1646
|
1539 |
private static final Name[][] INTERNED_ARGUMENTS
|
jaroslav@1646
|
1540 |
= new Name[ALL_TYPES.length()][INTERNED_ARGUMENT_LIMIT];
|
jaroslav@1646
|
1541 |
static {
|
jaroslav@1646
|
1542 |
for (int tn = 0; tn < ALL_TYPES.length(); tn++) {
|
jaroslav@1646
|
1543 |
for (int i = 0; i < INTERNED_ARGUMENTS[tn].length; i++) {
|
jaroslav@1646
|
1544 |
char type = ALL_TYPES.charAt(tn);
|
jaroslav@1646
|
1545 |
INTERNED_ARGUMENTS[tn][i] = new Name(i, type);
|
jaroslav@1646
|
1546 |
}
|
jaroslav@1646
|
1547 |
}
|
jaroslav@1646
|
1548 |
}
|
jaroslav@1646
|
1549 |
|
jaroslav@1646
|
1550 |
private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
|
jaroslav@1646
|
1551 |
|
jaroslav@1646
|
1552 |
static Name constantZero(int which, char type) {
|
jaroslav@1646
|
1553 |
return CONSTANT_ZERO[ALL_TYPES.indexOf(type)].newIndex(which);
|
jaroslav@1646
|
1554 |
}
|
jaroslav@1646
|
1555 |
private static final Name[] CONSTANT_ZERO
|
jaroslav@1646
|
1556 |
= new Name[ALL_TYPES.length()];
|
jaroslav@1646
|
1557 |
static {
|
jaroslav@1646
|
1558 |
for (int tn = 0; tn < ALL_TYPES.length(); tn++) {
|
jaroslav@1646
|
1559 |
char bt = ALL_TYPES.charAt(tn);
|
jaroslav@1646
|
1560 |
Wrapper wrap = Wrapper.forBasicType(bt);
|
jaroslav@1646
|
1561 |
MemberName zmem = new MemberName(LambdaForm.class, "zero"+bt, MethodType.methodType(wrap.primitiveType()), REF_invokeStatic);
|
jaroslav@1646
|
1562 |
try {
|
jaroslav@1646
|
1563 |
zmem = IMPL_NAMES.resolveOrFail(REF_invokeStatic, zmem, null, NoSuchMethodException.class);
|
jaroslav@1646
|
1564 |
} catch (IllegalAccessException|NoSuchMethodException ex) {
|
jaroslav@1646
|
1565 |
throw newInternalError(ex);
|
jaroslav@1646
|
1566 |
}
|
jaroslav@1646
|
1567 |
NamedFunction zcon = new NamedFunction(zmem);
|
jaroslav@1646
|
1568 |
Name n = new Name(zcon).newIndex(0);
|
jaroslav@1646
|
1569 |
assert(n.type == ALL_TYPES.charAt(tn));
|
jaroslav@1646
|
1570 |
CONSTANT_ZERO[tn] = n;
|
jaroslav@1646
|
1571 |
assert(n.isConstantZero());
|
jaroslav@1646
|
1572 |
}
|
jaroslav@1646
|
1573 |
}
|
jaroslav@1646
|
1574 |
|
jaroslav@1646
|
1575 |
// Avoid appealing to ValueConversions at bootstrap time:
|
jaroslav@1646
|
1576 |
private static int zeroI() { return 0; }
|
jaroslav@1646
|
1577 |
private static long zeroJ() { return 0; }
|
jaroslav@1646
|
1578 |
private static float zeroF() { return 0; }
|
jaroslav@1646
|
1579 |
private static double zeroD() { return 0; }
|
jaroslav@1646
|
1580 |
private static Object zeroL() { return null; }
|
jaroslav@1646
|
1581 |
|
jaroslav@1646
|
1582 |
// Put this last, so that previous static inits can run before.
|
jaroslav@1646
|
1583 |
static {
|
jaroslav@1646
|
1584 |
if (USE_PREDEFINED_INTERPRET_METHODS)
|
jaroslav@1646
|
1585 |
PREPARED_FORMS.putAll(computeInitialPreparedForms());
|
jaroslav@1646
|
1586 |
}
|
jaroslav@1646
|
1587 |
|
jaroslav@1646
|
1588 |
/**
|
jaroslav@1646
|
1589 |
* Internal marker for byte-compiled LambdaForms.
|
jaroslav@1646
|
1590 |
*/
|
jaroslav@1646
|
1591 |
/*non-public*/
|
jaroslav@1646
|
1592 |
@Target(ElementType.METHOD)
|
jaroslav@1646
|
1593 |
@Retention(RetentionPolicy.RUNTIME)
|
jaroslav@1646
|
1594 |
@interface Compiled {
|
jaroslav@1646
|
1595 |
}
|
jaroslav@1646
|
1596 |
|
jaroslav@1646
|
1597 |
/**
|
jaroslav@1646
|
1598 |
* Internal marker for LambdaForm interpreter frames.
|
jaroslav@1646
|
1599 |
*/
|
jaroslav@1646
|
1600 |
/*non-public*/
|
jaroslav@1646
|
1601 |
@Target(ElementType.METHOD)
|
jaroslav@1646
|
1602 |
@Retention(RetentionPolicy.RUNTIME)
|
jaroslav@1646
|
1603 |
@interface Hidden {
|
jaroslav@1646
|
1604 |
}
|
jaroslav@1646
|
1605 |
|
jaroslav@1646
|
1606 |
|
jaroslav@1646
|
1607 |
/*
|
jaroslav@1646
|
1608 |
// Smoke-test for the invokers used in this file.
|
jaroslav@1646
|
1609 |
static void testMethodHandleLinkers() throws Throwable {
|
jaroslav@1646
|
1610 |
MemberName.Factory lookup = MemberName.getFactory();
|
jaroslav@1646
|
1611 |
MemberName asList_MN = new MemberName(Arrays.class, "asList",
|
jaroslav@1646
|
1612 |
MethodType.methodType(List.class, Object[].class),
|
jaroslav@1646
|
1613 |
REF_invokeStatic);
|
jaroslav@1646
|
1614 |
//MethodHandleNatives.resolve(asList_MN, null);
|
jaroslav@1646
|
1615 |
asList_MN = lookup.resolveOrFail(asList_MN, REF_invokeStatic, null, NoSuchMethodException.class);
|
jaroslav@1646
|
1616 |
System.out.println("about to call "+asList_MN);
|
jaroslav@1646
|
1617 |
Object[] abc = { "a", "bc" };
|
jaroslav@1646
|
1618 |
List<?> lst = (List<?>) MethodHandle.linkToStatic(abc, asList_MN);
|
jaroslav@1646
|
1619 |
System.out.println("lst="+lst);
|
jaroslav@1646
|
1620 |
MemberName toString_MN = new MemberName(Object.class.getMethod("toString"));
|
jaroslav@1646
|
1621 |
String s1 = (String) MethodHandle.linkToVirtual(lst, toString_MN);
|
jaroslav@1646
|
1622 |
toString_MN = new MemberName(Object.class.getMethod("toString"), true);
|
jaroslav@1646
|
1623 |
String s2 = (String) MethodHandle.linkToSpecial(lst, toString_MN);
|
jaroslav@1646
|
1624 |
System.out.println("[s1,s2,lst]="+Arrays.asList(s1, s2, lst.toString()));
|
jaroslav@1646
|
1625 |
MemberName toArray_MN = new MemberName(List.class.getMethod("toArray"));
|
jaroslav@1646
|
1626 |
Object[] arr = (Object[]) MethodHandle.linkToInterface(lst, toArray_MN);
|
jaroslav@1646
|
1627 |
System.out.println("toArray="+Arrays.toString(arr));
|
jaroslav@1646
|
1628 |
}
|
jaroslav@1646
|
1629 |
static { try { testMethodHandleLinkers(); } catch (Throwable ex) { throw new RuntimeException(ex); } }
|
jaroslav@1646
|
1630 |
// Requires these definitions in MethodHandle:
|
jaroslav@1646
|
1631 |
static final native Object linkToStatic(Object x1, MemberName mn) throws Throwable;
|
jaroslav@1646
|
1632 |
static final native Object linkToVirtual(Object x1, MemberName mn) throws Throwable;
|
jaroslav@1646
|
1633 |
static final native Object linkToSpecial(Object x1, MemberName mn) throws Throwable;
|
jaroslav@1646
|
1634 |
static final native Object linkToInterface(Object x1, MemberName mn) throws Throwable;
|
jaroslav@1646
|
1635 |
*/
|
jaroslav@1646
|
1636 |
|
jaroslav@1646
|
1637 |
static { NamedFunction.initializeInvokers(); }
|
jaroslav@1646
|
1638 |
|
jaroslav@1646
|
1639 |
// The following hack is necessary in order to suppress TRACE_INTERPRETER
|
jaroslav@1646
|
1640 |
// during execution of the static initializes of this class.
|
jaroslav@1646
|
1641 |
// Turning on TRACE_INTERPRETER too early will cause
|
jaroslav@1646
|
1642 |
// stack overflows and other misbehavior during attempts to trace events
|
jaroslav@1646
|
1643 |
// that occur during LambdaForm.<clinit>.
|
jaroslav@1646
|
1644 |
// Therefore, do not move this line higher in this file, and do not remove.
|
jaroslav@1646
|
1645 |
private static final boolean TRACE_INTERPRETER = MethodHandleStatics.TRACE_INTERPRETER;
|
jaroslav@1646
|
1646 |
}
|