1 /*
2 * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.invoke;
27
28 import java.lang.classfile.TypeKind;
29 import jdk.internal.perf.PerfCounter;
30 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
31 import jdk.internal.vm.annotation.DontInline;
32 import jdk.internal.vm.annotation.Hidden;
33 import jdk.internal.vm.annotation.Stable;
34 import sun.invoke.util.Wrapper;
35
36 import java.lang.annotation.ElementType;
37 import java.lang.annotation.Retention;
38 import java.lang.annotation.RetentionPolicy;
39 import java.lang.annotation.Target;
40 import java.lang.reflect.Method;
41 import java.util.Arrays;
42 import java.util.HashMap;
43
44 import static java.lang.invoke.LambdaForm.BasicType.*;
45 import static java.lang.invoke.MethodHandleNatives.Constants.*;
46 import static java.lang.invoke.MethodHandleStatics.*;
47
48 /**
49 * The symbolic, non-executable form of a method handle's invocation semantics.
50 * It consists of a series of names.
51 * The first N (N=arity) names are parameters,
52 * while any remaining names are temporary values.
53 * Each temporary specifies the application of a function to some arguments.
54 * The functions are method handles, while the arguments are mixes of
55 * constant values and local names.
56 * The result of the lambda is defined as one of the names, often the last one.
57 * <p>
58 * Here is an approximate grammar:
59 * <blockquote><pre>{@code
60 * LambdaForm = "(" ArgName* ")=>{" TempName* Result "}"
61 * ArgName = "a" N ":" T
62 * TempName = "t" N ":" T "=" Function "(" Argument* ");"
63 * Function = ConstantValue
64 * Argument = NameRef | ConstantValue
65 * Result = NameRef | "void"
66 * NameRef = "a" N | "t" N
67 * N = (any whole number)
68 * T = "L" | "I" | "J" | "F" | "D" | "V"
69 * }</pre></blockquote>
70 * Names are numbered consecutively from left to right starting at zero.
71 * (The letters are merely a taste of syntax sugar.)
72 * Thus, the first temporary (if any) is always numbered N (where N=arity).
73 * Every occurrence of a name reference in an argument list must refer to
74 * a name previously defined within the same lambda.
75 * A lambda has a void result if and only if its result index is -1.
76 * If a temporary has the type "V", it cannot be the subject of a NameRef,
77 * even though possesses a number.
78 * Note that all reference types are erased to "L", which stands for {@code Object}.
79 * All subword types (boolean, byte, short, char) are erased to "I" which is {@code int}.
80 * The other types stand for the usual primitive types.
81 * <p>
82 * Function invocation closely follows the static rules of the Java verifier.
83 * Arguments and return values must exactly match when their "Name" types are
84 * considered.
85 * Conversions are allowed only if they do not change the erased type.
86 * <ul>
87 * <li>L = Object: casts are used freely to convert into and out of reference types
88 * <li>I = int: subword types are forcibly narrowed when passed as arguments (see {@code explicitCastArguments})
89 * <li>J = long: no implicit conversions
90 * <li>F = float: no implicit conversions
91 * <li>D = double: no implicit conversions
92 * <li>V = void: a function result may be void if and only if its Name is of type "V"
93 * </ul>
94 * Although implicit conversions are not allowed, explicit ones can easily be
95 * encoded by using temporary expressions which call type-transformed identity functions.
96 * <p>
97 * Examples:
98 * <blockquote><pre>{@code
99 * (a0:J)=>{ a0 }
100 * == identity(long)
101 * (a0:I)=>{ t1:V = System.out#println(a0); void }
102 * == System.out#println(int)
103 * (a0:L)=>{ t1:V = System.out#println(a0); a0 }
104 * == identity, with printing side-effect
105 * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
106 * t3:L = BoundMethodHandle#target(a0);
107 * t4:L = MethodHandle#invoke(t3, t2, a1); t4 }
108 * == general invoker for unary insertArgument combination
109 * (a0:L, a1:L)=>{ t2:L = FilterMethodHandle#filter(a0);
110 * t3:L = MethodHandle#invoke(t2, a1);
111 * t4:L = FilterMethodHandle#target(a0);
112 * t5:L = MethodHandle#invoke(t4, t3); t5 }
113 * == general invoker for unary filterArgument combination
114 * (a0:L, a1:L)=>{ ...(same as previous example)...
115 * t5:L = MethodHandle#invoke(t4, t3, a1); t5 }
116 * == general invoker for unary/unary foldArgument combination
117 * (a0:L, a1:I)=>{ t2:I = identity(long).asType((int)->long)(a1); t2 }
118 * == invoker for identity method handle which performs i2l
119 * (a0:L, a1:L)=>{ t2:L = BoundMethodHandle#argument(a0);
120 * t3:L = Class#cast(t2,a1); t3 }
121 * == invoker for identity method handle which performs cast
122 * }</pre></blockquote>
123 * <p>
124 * @author John Rose, JSR 292 EG
125 */
126 @AOTSafeClassInitializer
127 class LambdaForm {
128 final int arity;
129 final int result;
130 final boolean forceInline;
131 final MethodHandle customized;
132 @Stable final Name[] names;
133 final Kind kind;
134 MemberName vmentry; // low-level behavior, or null if not yet prepared
135 private boolean isCompiled;
136
137 // Either a LambdaForm cache (managed by LambdaFormEditor) or a link to uncustomized version (for customized LF)
138 volatile Object transformCache;
139
140 public static final int VOID_RESULT = -1, LAST_RESULT = -2;
141
142 /// Represents the "basic" types that exist in the JVM linkage and stack/locals.
143 /// All objects are erased to a reference.
144 /// All subwords (boolean, byte, char, short) are promoted to int.
145 enum BasicType {
146 L_TYPE('L', Object.class, Wrapper.OBJECT, TypeKind.REFERENCE),
147 I_TYPE('I', int.class, Wrapper.INT, TypeKind.INT),
148 J_TYPE('J', long.class, Wrapper.LONG, TypeKind.LONG),
149 F_TYPE('F', float.class, Wrapper.FLOAT, TypeKind.FLOAT),
150 D_TYPE('D', double.class, Wrapper.DOUBLE, TypeKind.DOUBLE), // end arg types
151 V_TYPE('V', void.class, Wrapper.VOID, TypeKind.VOID); // only valid in method return
152
153 static final @Stable BasicType[] ALL_TYPES = BasicType.values();
154 static final @Stable BasicType[] ARG_TYPES = Arrays.copyOf(ALL_TYPES, ALL_TYPES.length-1);
155
156 static final int ARG_TYPE_LIMIT = ARG_TYPES.length;
157 static final int TYPE_LIMIT = ALL_TYPES.length;
158
159 final char btChar;
160 final Class<?> btClass;
161 final Wrapper btWrapper;
162 final TypeKind btKind;
163
164 private BasicType(char btChar, Class<?> btClass, Wrapper wrapper, TypeKind typeKind) {
165 this.btChar = btChar;
166 this.btClass = btClass;
167 this.btWrapper = wrapper;
168 this.btKind = typeKind;
169 }
170
171 char basicTypeChar() {
172 return btChar;
173 }
174 Class<?> basicTypeClass() {
175 return btClass;
176 }
177 Wrapper basicTypeWrapper() {
178 return btWrapper;
179 }
180 TypeKind basicTypeKind() {
181 return btKind;
182 }
183 int basicTypeSlots() {
184 return btWrapper.stackSlots();
185 }
186
187 static BasicType basicType(byte type) {
188 return ALL_TYPES[type];
189 }
190 static BasicType basicType(char type) {
191 return switch (type) {
192 case 'L' -> L_TYPE;
193 case 'I' -> I_TYPE;
194 case 'J' -> J_TYPE;
195 case 'F' -> F_TYPE;
196 case 'D' -> D_TYPE;
197 case 'V' -> V_TYPE;
198 // all subword types are represented as ints
199 case 'Z', 'B', 'S', 'C' -> I_TYPE;
200 default -> throw newInternalError("Unknown type char: '" + type + "'");
201 };
202 }
203 static BasicType basicType(Class<?> type) {
204 return basicType(Wrapper.basicTypeChar(type));
205 }
206 static int[] basicTypeOrds(BasicType[] types) {
207 if (types == null) {
208 return null;
209 }
210 int[] a = new int[types.length];
211 for(int i = 0; i < types.length; ++i) {
212 a[i] = types[i].ordinal();
213 }
214 return a;
215 }
216
217 static char basicTypeChar(Class<?> type) {
218 return basicType(type).btChar;
219 }
220
221 static int[] basicTypesOrd(Class<?>[] types) {
222 int[] ords = new int[types.length];
223 for (int i = 0; i < ords.length; i++) {
224 ords[i] = basicType(types[i]).ordinal();
225 }
226 return ords;
227 }
228
229 static boolean isBasicTypeChar(char c) {
230 return "LIJFDV".indexOf(c) >= 0;
231 }
232 static boolean isArgBasicTypeChar(char c) {
233 return "LIJFD".indexOf(c) >= 0;
234 }
235
236 static { assert(checkBasicType()); }
237 private static boolean checkBasicType() {
238 for (int i = 0; i < ARG_TYPE_LIMIT; i++) {
239 assert ARG_TYPES[i].ordinal() == i;
240 assert ARG_TYPES[i] == ALL_TYPES[i];
241 }
242 for (int i = 0; i < TYPE_LIMIT; i++) {
243 assert ALL_TYPES[i].ordinal() == i;
244 }
245 assert ALL_TYPES[TYPE_LIMIT - 1] == V_TYPE;
246 assert !Arrays.asList(ARG_TYPES).contains(V_TYPE);
247 return true;
248 }
249 }
250
251 enum Kind {
252 GENERIC("invoke"),
253 IDENTITY("identity"),
254 CONSTANT("constant"),
255 BOUND_REINVOKER("BMH.reinvoke", "reinvoke"),
256 REINVOKER("MH.reinvoke", "reinvoke"),
257 DELEGATE("MH.delegate", "delegate"),
258 EXACT_LINKER("MH.invokeExact_MT", "invokeExact_MT"),
259 EXACT_INVOKER("MH.exactInvoker", "exactInvoker"),
260 GENERIC_LINKER("MH.invoke_MT", "invoke_MT"),
261 GENERIC_INVOKER("MH.invoker", "invoker"),
262 LINK_TO_TARGET_METHOD("linkToTargetMethod"),
263 LINK_TO_CALL_SITE("linkToCallSite"),
264 DIRECT_INVOKE_VIRTUAL("DMH.invokeVirtual", "invokeVirtual"),
265 DIRECT_INVOKE_SPECIAL("DMH.invokeSpecial", "invokeSpecial"),
266 DIRECT_INVOKE_SPECIAL_IFC("DMH.invokeSpecialIFC", "invokeSpecialIFC"),
267 DIRECT_INVOKE_STATIC("DMH.invokeStatic", "invokeStatic"),
268 DIRECT_NEW_INVOKE_SPECIAL("DMH.newInvokeSpecial", "newInvokeSpecial"),
269 DIRECT_INVOKE_INTERFACE("DMH.invokeInterface", "invokeInterface"),
270 DIRECT_INVOKE_STATIC_INIT("DMH.invokeStaticInit", "invokeStaticInit"),
271 FIELD_ACCESS("fieldAccess"),
272 FIELD_ACCESS_INIT("fieldAccessInit"),
273 VOLATILE_FIELD_ACCESS("volatileFieldAccess"),
274 VOLATILE_FIELD_ACCESS_INIT("volatileFieldAccessInit"),
275 FIELD_ACCESS_B("fieldAccessB"),
276 FIELD_ACCESS_INIT_B("fieldAccessInitB"),
277 VOLATILE_FIELD_ACCESS_B("volatileFieldAccessB"),
278 VOLATILE_FIELD_ACCESS_INIT_B("volatileFieldAccessInitB"),
279 FIELD_ACCESS_C("fieldAccessC"),
280 FIELD_ACCESS_INIT_C("fieldAccessInitC"),
281 VOLATILE_FIELD_ACCESS_C("volatileFieldAccessC"),
282 VOLATILE_FIELD_ACCESS_INIT_C("volatileFieldAccessInitC"),
283 FIELD_ACCESS_S("fieldAccessS"),
284 FIELD_ACCESS_INIT_S("fieldAccessInitS"),
285 VOLATILE_FIELD_ACCESS_S("volatileFieldAccessS"),
286 VOLATILE_FIELD_ACCESS_INIT_S("volatileFieldAccessInitS"),
287 FIELD_ACCESS_Z("fieldAccessZ"),
288 FIELD_ACCESS_INIT_Z("fieldAccessInitZ"),
289 VOLATILE_FIELD_ACCESS_Z("volatileFieldAccessZ"),
290 VOLATILE_FIELD_ACCESS_INIT_Z("volatileFieldAccessInitZ"),
291 FIELD_ACCESS_CAST("fieldAccessCast"),
292 FIELD_ACCESS_INIT_CAST("fieldAccessInitCast"),
293 VOLATILE_FIELD_ACCESS_CAST("volatileFieldAccessCast"),
294 VOLATILE_FIELD_ACCESS_INIT_CAST("volatileFieldAccessInitCast"),
295 TRY_FINALLY("tryFinally"),
296 TABLE_SWITCH("tableSwitch"),
297 COLLECTOR("collector"),
298 LOOP("loop"),
299 GUARD("guard"),
300 GUARD_WITH_CATCH("guardWithCatch"),
301 VARHANDLE_EXACT_INVOKER("VH.exactInvoker"),
302 VARHANDLE_INVOKER("VH.invoker", "invoker"),
303 VARHANDLE_LINKER("VH.invoke_MT", "invoke_MT");
304
305 final String defaultLambdaName;
306 final String methodName;
307
308 private Kind(String defaultLambdaName) {
309 this(defaultLambdaName, defaultLambdaName);
310 }
311
312 private Kind(String defaultLambdaName, String methodName) {
313 this.defaultLambdaName = defaultLambdaName;
314 this.methodName = methodName;
315 }
316 }
317
318 // private version that doesn't do checks or defensive copies
319 private LambdaForm(int arity, int result, boolean forceInline, MethodHandle customized, Name[] names, Kind kind) {
320 this.arity = arity;
321 this.result = result;
322 this.forceInline = forceInline;
323 this.customized = customized;
324 this.names = names;
325 this.kind = kind;
326 this.vmentry = null;
327 this.isCompiled = false;
328 }
329
330 // root factory pre/post processing and calls simple constructor
331 private static LambdaForm create(int arity, Name[] names, int result, boolean forceInline, MethodHandle customized, Kind kind) {
332 names = names.clone();
333 assert(namesOK(arity, names));
334 result = fixResult(result, names);
335
336 boolean canInterpret = normalizeNames(arity, names);
337 LambdaForm form = new LambdaForm(arity, result, forceInline, customized, names, kind);
338 assert(form.nameRefsAreLegal());
339 if (!canInterpret) {
340 form.compileToBytecode();
341 }
342 return form;
343 }
344
345 // derived factories with defaults
346 private static final int DEFAULT_RESULT = LAST_RESULT;
347 private static final boolean DEFAULT_FORCE_INLINE = true;
348 private static final MethodHandle DEFAULT_CUSTOMIZED = null;
349 private static final Kind DEFAULT_KIND = Kind.GENERIC;
350
351 static LambdaForm create(int arity, Name[] names, int result) {
352 return create(arity, names, result, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, DEFAULT_KIND);
353 }
354 static LambdaForm create(int arity, Name[] names, int result, Kind kind) {
355 return create(arity, names, result, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, kind);
356 }
357 static LambdaForm create(int arity, Name[] names) {
358 return create(arity, names, DEFAULT_RESULT, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, DEFAULT_KIND);
359 }
360 static LambdaForm create(int arity, Name[] names, Kind kind) {
361 return create(arity, names, DEFAULT_RESULT, DEFAULT_FORCE_INLINE, DEFAULT_CUSTOMIZED, kind);
362 }
363 static LambdaForm create(int arity, Name[] names, boolean forceInline, Kind kind) {
364 return create(arity, names, DEFAULT_RESULT, forceInline, DEFAULT_CUSTOMIZED, kind);
365 }
366
367 private static int fixResult(int result, Name[] names) {
368 if (result == LAST_RESULT)
369 result = names.length - 1; // might still be void
370 if (result >= 0 && names[result].type == V_TYPE)
371 result = VOID_RESULT;
372 return result;
373 }
374
375 static boolean debugNames() {
376 return DEBUG_NAME_COUNTERS != null;
377 }
378
379 static void associateWithDebugName(LambdaForm form, String name) {
380 assert (debugNames());
381 synchronized (DEBUG_NAMES) {
382 DEBUG_NAMES.put(form, name);
383 }
384 }
385
386 String lambdaName() {
387 if (DEBUG_NAMES != null) {
388 synchronized (DEBUG_NAMES) {
389 String name = DEBUG_NAMES.get(this);
390 if (name == null) {
391 name = generateDebugName();
392 }
393 return name;
394 }
395 }
396 return kind.defaultLambdaName;
397 }
398
399 private String generateDebugName() {
400 assert (debugNames());
401 String debugNameStem = kind.defaultLambdaName;
402 Integer ctr = DEBUG_NAME_COUNTERS.getOrDefault(debugNameStem, 0);
403 DEBUG_NAME_COUNTERS.put(debugNameStem, ctr + 1);
404 StringBuilder buf = new StringBuilder(debugNameStem);
405 int leadingZero = buf.length();
406 buf.append((int) ctr);
407 for (int i = buf.length() - leadingZero; i < 3; i++) {
408 buf.insert(leadingZero, '0');
409 }
410 buf.append('_');
411 buf.append(basicTypeSignature());
412 String name = buf.toString();
413 associateWithDebugName(this, name);
414 return name;
415 }
416
417 private static boolean namesOK(int arity, Name[] names) {
418 for (int i = 0; i < names.length; i++) {
419 Name n = names[i];
420 assert(n != null) : "n is null";
421 if (i < arity)
422 assert( n.isParam()) : n + " is not param at " + i;
423 else
424 assert(!n.isParam()) : n + " is param at " + i;
425 }
426 return true;
427 }
428
429 /** Customize LambdaForm for a particular MethodHandle */
430 LambdaForm customize(MethodHandle mh) {
431 if (customized == mh) {
432 return this;
433 }
434 LambdaForm customForm = LambdaForm.create(arity, names, result, forceInline, mh, kind);
435 if (COMPILE_THRESHOLD >= 0 && isCompiled) {
436 // If shared LambdaForm has been compiled, compile customized version as well.
437 customForm.compileToBytecode();
438 }
439 customForm.transformCache = this; // LambdaFormEditor should always use uncustomized form.
440 return customForm;
441 }
442
443 /** Get uncustomized flavor of the LambdaForm */
444 LambdaForm uncustomize() {
445 if (customized == null) {
446 return this;
447 }
448 assert(transformCache != null); // Customized LambdaForm should always has a link to uncustomized version.
449 LambdaForm uncustomizedForm = (LambdaForm)transformCache;
450 if (COMPILE_THRESHOLD >= 0 && isCompiled) {
451 // If customized LambdaForm has been compiled, compile uncustomized version as well.
452 uncustomizedForm.compileToBytecode();
453 }
454 return uncustomizedForm;
455 }
456
457 /** Renumber and/or replace params so that they are interned and canonically numbered.
458 * @return true if we can interpret
459 */
460 private static boolean normalizeNames(int arity, Name[] names) {
461 Name[] oldNames = names.clone();
462 int maxOutArity = 0;
463 for (int i = 0; i < names.length; i++) {
464 Name n = names[i];
465 names[i] = n.withIndex(i);
466 if (n.arguments != null && maxOutArity < n.arguments.length)
467 maxOutArity = n.arguments.length;
468 }
469 if (oldNames != null) {
470 for (int i = Math.max(1, arity); i < names.length; i++) {
471 Name fixed = names[i].replaceNames(oldNames, names, 0, i);
472 names[i] = fixed.withIndex(i);
473 }
474 }
475 int maxInterned = Math.min(arity, INTERNED_ARGUMENT_LIMIT);
476 boolean needIntern = false;
477 for (int i = 0; i < maxInterned; i++) {
478 Name n = names[i], n2 = internArgument(n);
479 if (n != n2) {
480 names[i] = n2;
481 needIntern = true;
482 }
483 }
484 if (needIntern) {
485 for (int i = arity; i < names.length; i++) {
486 names[i].internArguments();
487 }
488 }
489
490 // return true if we can interpret
491 if (maxOutArity > MethodType.MAX_MH_INVOKER_ARITY) {
492 // Cannot use LF interpreter on very high arity expressions.
493 assert(maxOutArity <= MethodType.MAX_JVM_ARITY);
494 return false;
495 }
496 return true;
497 }
498
499 /**
500 * Check that all embedded Name references are localizable to this lambda,
501 * and are properly ordered after their corresponding definitions.
502 * <p>
503 * Note that a Name can be local to multiple lambdas, as long as
504 * it possesses the same index in each use site.
505 * This allows Name references to be freely reused to construct
506 * fresh lambdas, without confusion.
507 */
508 boolean nameRefsAreLegal() {
509 assert(arity >= 0 && arity <= names.length);
510 assert(result >= -1 && result < names.length);
511 // Do all names possess an index consistent with their local definition order?
512 for (int i = 0; i < arity; i++) {
513 Name n = names[i];
514 assert(n.index() == i) : Arrays.asList(n.index(), i);
515 assert(n.isParam());
516 }
517 // Also, do all local name references
518 for (int i = arity; i < names.length; i++) {
519 Name n = names[i];
520 assert(n.index() == i);
521 for (Object arg : n.arguments) {
522 if (arg instanceof Name n2) {
523 int i2 = n2.index;
524 assert(0 <= i2 && i2 < names.length) : n.debugString() + ": 0 <= i2 && i2 < names.length: 0 <= " + i2 + " < " + names.length;
525 assert(names[i2] == n2) : Arrays.asList("-1-", i, "-2-", n.debugString(), "-3-", i2, "-4-", n2.debugString(), "-5-", names[i2].debugString(), "-6-", this);
526 assert(i2 < i); // ref must come after def!
527 }
528 }
529 }
530 return true;
531 }
532
533 // /** Invoke this form on the given arguments. */
534 // final Object invoke(Object... args) throws Throwable {
535 // // NYI: fit this into the fast path?
536 // return interpretWithArguments(args);
537 // }
538
539 /** Report the return type. */
540 BasicType returnType() {
541 if (result < 0) return V_TYPE;
542 Name n = names[result];
543 return n.type;
544 }
545
546 /** Report the N-th argument type. */
547 BasicType parameterType(int n) {
548 return parameter(n).type;
549 }
550
551 /** Report the N-th argument name. */
552 Name parameter(int n) {
553 Name param = names[n];
554 assert(n < arity && param.isParam());
555 return param;
556 }
557
558 /** Report the N-th argument type constraint. */
559 Object parameterConstraint(int n) {
560 return parameter(n).constraint;
561 }
562
563 /** Report the arity. */
564 int arity() {
565 return arity;
566 }
567
568 /** Report the number of expressions (non-parameter names). */
569 int expressionCount() {
570 return names.length - arity;
571 }
572
573 /** Return the method type corresponding to my basic type signature. */
574 MethodType methodType() {
575 Class<?>[] ptypes = new Class<?>[arity];
576 for (int i = 0; i < arity; ++i) {
577 ptypes[i] = parameterType(i).btClass;
578 }
579 return MethodType.methodType(returnType().btClass, ptypes, true);
580 }
581
582 /** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
583 final String basicTypeSignature() {
584 StringBuilder buf = new StringBuilder(arity() + 3);
585 for (int i = 0, a = arity(); i < a; i++)
586 buf.append(parameterType(i).basicTypeChar());
587 return buf.append('_').append(returnType().basicTypeChar()).toString();
588 }
589 static int signatureArity(String sig) {
590 assert(isValidSignature(sig));
591 return sig.indexOf('_');
592 }
593 static boolean isValidSignature(String sig) {
594 int arity = sig.indexOf('_');
595 if (arity < 0) return false; // must be of the form *_*
596 int siglen = sig.length();
597 if (siglen != arity + 2) return false; // *_X
598 for (int i = 0; i < siglen; i++) {
599 if (i == arity) continue; // skip '_'
600 char c = sig.charAt(i);
601 if (c == 'V')
602 return (i == siglen - 1 && arity == siglen - 2);
603 if (!isArgBasicTypeChar(c)) return false; // must be [LIJFD]
604 }
605 return true; // [LIJFD]*_[LIJFDV]
606 }
607
608 /**
609 * Check if i-th name is a call to MethodHandleImpl.selectAlternative.
610 */
611 boolean isSelectAlternative(int pos) {
612 // selectAlternative idiom:
613 // t_{n}:L=MethodHandleImpl.selectAlternative(...)
614 // t_{n+1}:?=MethodHandle.invokeBasic(t_{n}, ...)
615 if (pos+1 >= names.length) return false;
616 Name name0 = names[pos];
617 Name name1 = names[pos+1];
618 return name0.refersTo(MethodHandleImpl.class, "selectAlternative") &&
619 name1.isInvokeBasic() &&
620 name1.lastUseIndex(name0) == 0 && // t_{n+1}:?=MethodHandle.invokeBasic(t_{n}, ...)
621 lastUseIndex(name0) == pos+1; // t_{n} is local: used only in t_{n+1}
622 }
623
624 private boolean isMatchingIdiom(int pos, String idiomName, int nArgs) {
625 if (pos+2 >= names.length) return false;
626 Name name0 = names[pos];
627 Name name1 = names[pos+1];
628 Name name2 = names[pos+2];
629 return name1.refersTo(MethodHandleImpl.class, idiomName) &&
630 name0.isInvokeBasic() &&
631 name2.isInvokeBasic() &&
632 name1.lastUseIndex(name0) == nArgs && // t_{n+1}:L=MethodHandleImpl.<invoker>(<args>, t_{n});
633 lastUseIndex(name0) == pos+1 && // t_{n} is local: used only in t_{n+1}
634 name2.lastUseIndex(name1) == 1 && // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
635 lastUseIndex(name1) == pos+2; // t_{n+1} is local: used only in t_{n+2}
636 }
637
638 /**
639 * Check if i-th name is a start of GuardWithCatch idiom.
640 */
641 boolean isGuardWithCatch(int pos) {
642 // GuardWithCatch idiom:
643 // t_{n}:L=MethodHandle.invokeBasic(...)
644 // t_{n+1}:L=MethodHandleImpl.guardWithCatch(*, *, *, t_{n});
645 // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
646 return isMatchingIdiom(pos, "guardWithCatch", 3);
647 }
648
649 /**
650 * Check if i-th name is a start of the tryFinally idiom.
651 */
652 boolean isTryFinally(int pos) {
653 // tryFinally idiom:
654 // t_{n}:L=MethodHandle.invokeBasic(...)
655 // t_{n+1}:L=MethodHandleImpl.tryFinally(*, *, t_{n})
656 // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
657 return isMatchingIdiom(pos, "tryFinally", 2);
658 }
659
660 /**
661 * Check if i-th name is a start of the tableSwitch idiom.
662 */
663 boolean isTableSwitch(int pos) {
664 // tableSwitch idiom:
665 // t_{n}:L=MethodHandle.invokeBasic(...) // args
666 // t_{n+1}:L=MethodHandleImpl.tableSwitch(*, *, *, t_{n})
667 // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
668 if (pos + 2 >= names.length) return false;
669
670 final int POS_COLLECT_ARGS = pos;
671 final int POS_TABLE_SWITCH = pos + 1;
672 final int POS_UNBOX_RESULT = pos + 2;
673
674 Name collectArgs = names[POS_COLLECT_ARGS];
675 Name tableSwitch = names[POS_TABLE_SWITCH];
676 Name unboxResult = names[POS_UNBOX_RESULT];
677 return tableSwitch.refersTo(MethodHandleImpl.class, "tableSwitch") &&
678 collectArgs.isInvokeBasic() &&
679 unboxResult.isInvokeBasic() &&
680 tableSwitch.lastUseIndex(collectArgs) == 3 && // t_{n+1}:L=MethodHandleImpl.<invoker>(*, *, *, t_{n});
681 lastUseIndex(collectArgs) == POS_TABLE_SWITCH && // t_{n} is local: used only in t_{n+1}
682 unboxResult.lastUseIndex(tableSwitch) == 1 && // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
683 lastUseIndex(tableSwitch) == POS_UNBOX_RESULT; // t_{n+1} is local: used only in t_{n+2}
684 }
685
686 /**
687 * Check if i-th name is a start of the loop idiom.
688 */
689 boolean isLoop(int pos) {
690 // loop idiom:
691 // t_{n}:L=MethodHandle.invokeBasic(...)
692 // t_{n+1}:L=MethodHandleImpl.loop(types, *, t_{n})
693 // t_{n+2}:?=MethodHandle.invokeBasic(*, t_{n+1})
694 return isMatchingIdiom(pos, "loop", 2);
695 }
696
697 /*
698 * Code generation issues:
699 *
700 * Compiled LFs should be reusable in general.
701 * The biggest issue is how to decide when to pull a name into
702 * the bytecode, versus loading a reified form from the MH data.
703 *
704 * For example, an asType wrapper may require execution of a cast
705 * after a call to a MH. The target type of the cast can be placed
706 * as a constant in the LF itself. This will force the cast type
707 * to be compiled into the bytecodes and native code for the MH.
708 * Or, the target type of the cast can be erased in the LF, and
709 * loaded from the MH data. (Later on, if the MH as a whole is
710 * inlined, the data will flow into the inlined instance of the LF,
711 * as a constant, and the end result will be an optimal cast.)
712 *
713 * This erasure of cast types can be done with any use of
714 * reference types. It can also be done with whole method
715 * handles. Erasing a method handle might leave behind
716 * LF code that executes correctly for any MH of a given
717 * type, and load the required MH from the enclosing MH's data.
718 * Or, the erasure might even erase the expected MT.
719 *
720 * Also, for direct MHs, the MemberName of the target
721 * could be erased, and loaded from the containing direct MH.
722 * As a simple case, a LF for all int-valued non-static
723 * field getters would perform a cast on its input argument
724 * (to non-constant base type derived from the MemberName)
725 * and load an integer value from the input object
726 * (at a non-constant offset also derived from the MemberName).
727 * Such MN-erased LFs would be inlinable back to optimized
728 * code, whenever a constant enclosing DMH is available
729 * to supply a constant MN from its data.
730 *
731 * The main problem here is to keep LFs reasonably generic,
732 * while ensuring that hot spots will inline good instances.
733 * "Reasonably generic" means that we don't end up with
734 * repeated versions of bytecode or machine code that do
735 * not differ in their optimized form. Repeated versions
736 * of machine would have the undesirable overheads of
737 * (a) redundant compilation work and (b) extra I$ pressure.
738 * To control repeated versions, we need to be ready to
739 * erase details from LFs and move them into MH data,
740 * whenever those details are not relevant to significant
741 * optimization. "Significant" means optimization of
742 * code that is actually hot.
743 *
744 * Achieving this may require dynamic splitting of MHs, by replacing
745 * a generic LF with a more specialized one, on the same MH,
746 * if (a) the MH is frequently executed and (b) the MH cannot
747 * be inlined into a containing caller, such as an invokedynamic.
748 *
749 * Compiled LFs that are no longer used should be GC-able.
750 * If they contain non-BCP references, they should be properly
751 * interlinked with the class loader(s) that their embedded types
752 * depend on. This probably means that reusable compiled LFs
753 * will be tabulated (indexed) on relevant class loaders,
754 * or else that the tables that cache them will have weak links.
755 */
756
757 /**
758 * Make this LF directly executable, as part of a MethodHandle.
759 * Invariant: Every MH which is invoked must prepare its LF
760 * before invocation.
761 * (In principle, the JVM could do this very lazily,
762 * as a sort of pre-invocation linkage step.)
763 */
764 public void prepare() {
765 if (COMPILE_THRESHOLD == 0 && !forceInterpretation() && !isCompiled) {
766 compileToBytecode();
767 }
768 if (this.vmentry != null) {
769 // already prepared (e.g., a primitive DMH invoker form)
770 return;
771 }
772 MethodType mtype = methodType();
773 MethodTypeForm form = mtype.form();
774
775 MemberName entry = form.cachedInterpretEntry();
776 if (entry == null) {
777 assert (isValidSignature(basicTypeSignature()));
778 entry = InvokerBytecodeGenerator.generateLambdaFormInterpreterEntryPoint(mtype);
779 entry = form.setCachedInterpretEntry(entry);
780 }
781 this.vmentry = entry;
782 // TO DO: Maybe add invokeGeneric, invokeWithArguments
783 }
784
785 private static @Stable PerfCounter LF_FAILED;
786
787 private static PerfCounter failedCompilationCounter() {
788 if (LF_FAILED == null) {
789 LF_FAILED = PerfCounter.newPerfCounter("java.lang.invoke.failedLambdaFormCompilations");
790 }
791 return LF_FAILED;
792 }
793
794 /** Generate optimizable bytecode for this form. */
795 void compileToBytecode() {
796 if (forceInterpretation()) {
797 return; // this should not be compiled
798 }
799 if (vmentry != null && isCompiled) {
800 return; // already compiled somehow
801 }
802
803 // Obtain the invoker MethodType outside of the following try block.
804 // This ensures that an IllegalArgumentException is directly thrown if the
805 // type would have 256 or more parameters
806 MethodType invokerType = methodType();
807 assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
808 try {
809 vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
810 if (TRACE_INTERPRETER)
811 traceInterpreter("compileToBytecode", this);
812 isCompiled = true;
813 } catch (InvokerBytecodeGenerator.BytecodeGenerationException bge) {
814 // bytecode generation failed - mark this LambdaForm as to be run in interpretation mode only
815 invocationCounter = -1;
816 failedCompilationCounter().increment();
817 if (LOG_LF_COMPILATION_FAILURE) {
818 System.out.println("LambdaForm compilation failed: " + this);
819 bge.printStackTrace(System.out);
820 }
821 } catch (Error e) {
822 // Pass through any error
823 throw e;
824 } catch (Exception e) {
825 // Wrap any exception
826 throw newInternalError(this.toString(), e);
827 }
828 }
829
830 // The next few routines are called only from assert expressions
831 // They verify that the built-in invokers process the correct raw data types.
832 private static boolean argumentTypesMatch(String sig, Object[] av) {
833 int arity = signatureArity(sig);
834 assert(av.length == arity) : "av.length == arity: av.length=" + av.length + ", arity=" + arity;
835 assert(av[0] instanceof MethodHandle) : "av[0] not instance of MethodHandle: " + av[0];
836 MethodHandle mh = (MethodHandle) av[0];
837 MethodType mt = mh.type();
838 assert(mt.parameterCount() == arity-1);
839 for (int i = 0; i < av.length; i++) {
840 Class<?> pt = (i == 0 ? MethodHandle.class : mt.parameterType(i-1));
841 assert(valueMatches(basicType(sig.charAt(i)), pt, av[i]));
842 }
843 return true;
844 }
845 private static boolean valueMatches(BasicType tc, Class<?> type, Object x) {
846 // The following line is needed because (...)void method handles can use non-void invokers
847 if (type == void.class) tc = V_TYPE; // can drop any kind of value
848 assert tc == basicType(type) : tc + " == basicType(" + type + ")=" + basicType(type);
849 switch (tc) {
850 case I_TYPE: assert checkInt(type, x) : "checkInt(" + type + "," + x +")"; break;
851 case J_TYPE: assert x instanceof Long : "instanceof Long: " + x; break;
852 case F_TYPE: assert x instanceof Float : "instanceof Float: " + x; break;
853 case D_TYPE: assert x instanceof Double : "instanceof Double: " + x; break;
854 case L_TYPE: assert checkRef(type, x) : "checkRef(" + type + "," + x + ")"; break;
855 case V_TYPE: break; // allow anything here; will be dropped
856 default: assert(false);
857 }
858 return true;
859 }
860 private static boolean checkInt(Class<?> type, Object x) {
861 assert(x instanceof Integer);
862 if (type == int.class) return true;
863 Wrapper w = Wrapper.forBasicType(type);
864 assert(w.isSubwordOrInt());
865 Object x1 = Wrapper.INT.wrap(w.wrap(x));
866 return x.equals(x1);
867 }
868 private static boolean checkRef(Class<?> type, Object x) {
869 assert(!type.isPrimitive());
870 if (x == null) return true;
871 if (type.isInterface()) return true;
872 return type.isInstance(x);
873 }
874
875 /** If the invocation count hits the threshold we spin bytecodes and call that subsequently. */
876 private static final int COMPILE_THRESHOLD;
877 static {
878 COMPILE_THRESHOLD = Math.max(-1, MethodHandleStatics.COMPILE_THRESHOLD);
879 }
880 private int invocationCounter = 0; // a value of -1 indicates LambdaForm interpretation mode forever
881
882 private boolean forceInterpretation() {
883 return invocationCounter == -1;
884 }
885
886 /** Interpretively invoke this form on the given arguments. */
887 @Hidden
888 @DontInline
889 Object interpretWithArguments(Object... argumentValues) throws Throwable {
890 if (TRACE_INTERPRETER)
891 return interpretWithArgumentsTracing(argumentValues);
892 checkInvocationCounter();
893 assert(arityCheck(argumentValues));
894 Object[] values = Arrays.copyOf(argumentValues, names.length);
895 for (int i = argumentValues.length; i < values.length; i++) {
896 values[i] = interpretName(names[i], values);
897 }
898 Object rv = (result < 0) ? null : values[result];
899 assert(resultCheck(argumentValues, rv));
900 return rv;
901 }
902
903 /** Evaluate a single Name within this form, applying its function to its arguments. */
904 @Hidden
905 @DontInline
906 Object interpretName(Name name, Object[] values) throws Throwable {
907 if (TRACE_INTERPRETER)
908 traceInterpreter("| interpretName", name.debugString(), (Object[]) null);
909 Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
910 for (int i = 0; i < arguments.length; i++) {
911 Object a = arguments[i];
912 if (a instanceof Name n) {
913 int i2 = n.index();
914 assert(names[i2] == a);
915 a = values[i2];
916 arguments[i] = a;
917 }
918 }
919 return name.function.invokeWithArguments(arguments);
920 }
921
922 private void checkInvocationCounter() {
923 if (COMPILE_THRESHOLD != 0 &&
924 !forceInterpretation() && invocationCounter < COMPILE_THRESHOLD) {
925 invocationCounter++; // benign race
926 if (invocationCounter >= COMPILE_THRESHOLD) {
927 // Replace vmentry with a bytecode version of this LF.
928 compileToBytecode();
929 }
930 }
931 }
932 Object interpretWithArgumentsTracing(Object... argumentValues) throws Throwable {
933 traceInterpreter("[ interpretWithArguments", this, argumentValues);
934 if (!forceInterpretation() && invocationCounter < COMPILE_THRESHOLD) {
935 int ctr = invocationCounter++; // benign race
936 traceInterpreter("| invocationCounter", ctr);
937 if (invocationCounter >= COMPILE_THRESHOLD) {
938 compileToBytecode();
939 }
940 }
941 Object rval;
942 try {
943 assert(arityCheck(argumentValues));
944 Object[] values = Arrays.copyOf(argumentValues, names.length);
945 for (int i = argumentValues.length; i < values.length; i++) {
946 values[i] = interpretName(names[i], values);
947 }
948 rval = (result < 0) ? null : values[result];
949 } catch (Throwable ex) {
950 traceInterpreter("] throw =>", ex);
951 throw ex;
952 }
953 traceInterpreter("] return =>", rval);
954 return rval;
955 }
956
957 static void traceInterpreter(String event, Object obj, Object... args) {
958 if (TRACE_INTERPRETER) {
959 System.out.println("LFI: "+event+" "+(obj != null ? obj : "")+(args != null && args.length != 0 ? Arrays.asList(args) : ""));
960 }
961 }
962 static void traceInterpreter(String event, Object obj) {
963 traceInterpreter(event, obj, (Object[])null);
964 }
965 private boolean arityCheck(Object[] argumentValues) {
966 assert(argumentValues.length == arity) : arity+"!="+Arrays.asList(argumentValues)+".length";
967 // also check that the leading (receiver) argument is somehow bound to this LF:
968 assert(argumentValues[0] instanceof MethodHandle) : "not MH: " + argumentValues[0];
969 MethodHandle mh = (MethodHandle) argumentValues[0];
970 assert(mh.internalForm() == this);
971 // note: argument #0 could also be an interface wrapper, in the future
972 argumentTypesMatch(basicTypeSignature(), argumentValues);
973 return true;
974 }
975 private boolean resultCheck(Object[] argumentValues, Object result) {
976 MethodHandle mh = (MethodHandle) argumentValues[0];
977 MethodType mt = mh.type();
978 assert(valueMatches(returnType(), mt.returnType(), result));
979 return true;
980 }
981
982 public String toString() {
983 return debugString(-1);
984 }
985
986 String debugString(int indentLevel) {
987 String prefix = MethodHandle.debugPrefix(indentLevel);
988 String lambdaName = lambdaName();
989 StringBuilder buf = new StringBuilder(lambdaName);
990 buf.append("=Lambda(");
991 for (int i = 0; i < names.length; i++) {
992 if (i == arity) buf.append(")=>{");
993 Name n = names[i];
994 if (i >= arity) buf.append("\n ").append(prefix);
995 buf.append(n.paramString());
996 if (i < arity) {
997 if (i+1 < arity) buf.append(",");
998 continue;
999 }
1000 buf.append("=").append(n.exprString());
1001 buf.append(";");
1002 }
1003 if (arity == names.length) buf.append(")=>{");
1004 buf.append(result < 0 ? "void" : names[result]).append("}");
1005 if (TRACE_INTERPRETER) {
1006 // Extra verbosity:
1007 buf.append(":").append(basicTypeSignature());
1008 buf.append("/").append(vmentry);
1009 }
1010 return buf.toString();
1011 }
1012
1013 @Override
1014 public boolean equals(Object obj) {
1015 return obj instanceof LambdaForm lf && equals(lf);
1016 }
1017 public boolean equals(LambdaForm that) {
1018 if (this.result != that.result) return false;
1019 return Arrays.equals(this.names, that.names);
1020 }
1021 public int hashCode() {
1022 return result + 31 * Arrays.hashCode(names);
1023 }
1024 LambdaFormEditor editor() {
1025 return LambdaFormEditor.lambdaFormEditor(this);
1026 }
1027
1028 boolean contains(Name name) {
1029 int pos = name.index();
1030 if (pos >= 0) {
1031 return pos < names.length && name.equals(names[pos]);
1032 }
1033 for (int i = arity; i < names.length; i++) {
1034 if (name.equals(names[i]))
1035 return true;
1036 }
1037 return false;
1038 }
1039
1040 @AOTSafeClassInitializer
1041 static class NamedFunction {
1042 final MemberName member;
1043 private @Stable MethodHandle resolvedHandle;
1044 private @Stable MethodType type;
1045
1046 NamedFunction(MethodHandle resolvedHandle) {
1047 this(resolvedHandle.internalMemberName(), resolvedHandle);
1048 }
1049 NamedFunction(MemberName member, MethodHandle resolvedHandle) {
1050 this.member = member;
1051 this.resolvedHandle = resolvedHandle;
1052 // The following assert is almost always correct, but will fail for corner cases, such as PrivateInvokeTest.
1053 //assert(!isInvokeBasic(member));
1054 }
1055 NamedFunction(MethodType basicInvokerType) {
1056 assert(basicInvokerType == basicInvokerType.basicType()) : basicInvokerType;
1057 if (basicInvokerType.parameterSlotCount() < MethodType.MAX_MH_INVOKER_ARITY) {
1058 this.resolvedHandle = basicInvokerType.invokers().basicInvoker();
1059 this.member = resolvedHandle.internalMemberName();
1060 } else {
1061 // necessary to pass BigArityTest
1062 this.member = Invokers.invokeBasicMethod(basicInvokerType);
1063 }
1064 assert(isInvokeBasic(member));
1065 }
1066
1067 private static boolean isInvokeBasic(MemberName member) {
1068 return member != null &&
1069 member.getDeclaringClass() == MethodHandle.class &&
1070 "invokeBasic".equals(member.getName());
1071 }
1072
1073 // The next 2 constructors are used to break circular dependencies on MH.invokeStatic, etc.
1074 // Any LambdaForm containing such a member is not interpretable.
1075 // This is OK, since all such LFs are prepared with special primitive vmentry points.
1076 // And even without the resolvedHandle, the name can still be compiled and optimized.
1077 NamedFunction(Method method) {
1078 this(new MemberName(method));
1079 }
1080 NamedFunction(MemberName member) {
1081 this(member, null);
1082 }
1083
1084 MethodHandle resolvedHandle() {
1085 if (resolvedHandle == null) resolve();
1086 return resolvedHandle;
1087 }
1088
1089 synchronized void resolve() {
1090 if (resolvedHandle == null) {
1091 resolvedHandle = DirectMethodHandle.make(member);
1092 }
1093 }
1094
1095 @Override
1096 public boolean equals(Object other) {
1097 if (this == other) return true;
1098 if (other == null) return false;
1099 return (other instanceof NamedFunction that)
1100 && this.member != null
1101 && this.member.equals(that.member);
1102 }
1103
1104 @Override
1105 public int hashCode() {
1106 if (member != null)
1107 return member.hashCode();
1108 return super.hashCode();
1109 }
1110
1111 static final MethodType INVOKER_METHOD_TYPE =
1112 MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1113
1114 private static MethodHandle computeInvoker(MethodTypeForm typeForm) {
1115 typeForm = typeForm.basicType().form(); // normalize to basic type
1116 MethodHandle mh = typeForm.cachedMethodHandle(MethodTypeForm.MH_NF_INV);
1117 if (mh != null) return mh;
1118 MemberName invoker = InvokerBytecodeGenerator.generateNamedFunctionInvoker(typeForm); // this could take a while
1119 mh = DirectMethodHandle.make(invoker);
1120 MethodHandle mh2 = typeForm.cachedMethodHandle(MethodTypeForm.MH_NF_INV);
1121 if (mh2 != null) return mh2; // benign race
1122 if (!mh.type().equals(INVOKER_METHOD_TYPE))
1123 throw newInternalError(mh.debugString());
1124 return typeForm.setCachedMethodHandle(MethodTypeForm.MH_NF_INV, mh);
1125 }
1126
1127 @Hidden
1128 Object invokeWithArguments(Object... arguments) throws Throwable {
1129 // If we have a cached invoker, call it right away.
1130 // NOTE: The invoker always returns a reference value.
1131 if (TRACE_INTERPRETER) return invokeWithArgumentsTracing(arguments);
1132 return invoker().invokeBasic(resolvedHandle(), arguments);
1133 }
1134
1135 @Hidden
1136 Object invokeWithArgumentsTracing(Object[] arguments) throws Throwable {
1137 Object rval;
1138 try {
1139 traceInterpreter("[ call", this, arguments);
1140 // resolvedHandle might be uninitialized, ok for tracing
1141 if (resolvedHandle == null) {
1142 traceInterpreter("| resolve", this);
1143 resolvedHandle();
1144 }
1145 rval = invoker().invokeBasic(resolvedHandle(), arguments);
1146 } catch (Throwable ex) {
1147 traceInterpreter("] throw =>", ex);
1148 throw ex;
1149 }
1150 traceInterpreter("] return =>", rval);
1151 return rval;
1152 }
1153
1154 private MethodHandle invoker() {
1155 return computeInvoker(methodType().form());
1156 }
1157
1158 MethodType methodType() {
1159 MethodType type = this.type;
1160 if (type == null) {
1161 this.type = type = calculateMethodType(member, resolvedHandle);
1162 }
1163 return type;
1164 }
1165
1166 private static MethodType calculateMethodType(MemberName member, MethodHandle resolvedHandle) {
1167 if (resolvedHandle != null) {
1168 return resolvedHandle.type();
1169 } else {
1170 // only for certain internal LFs during bootstrapping
1171 return member.getInvocationType();
1172 }
1173 }
1174
1175 MemberName member() {
1176 assert(assertMemberIsConsistent());
1177 return member;
1178 }
1179
1180 // Called only from assert.
1181 private boolean assertMemberIsConsistent() {
1182 if (resolvedHandle instanceof DirectMethodHandle) {
1183 MemberName m = resolvedHandle.internalMemberName();
1184 assert(m.equals(member));
1185 }
1186 return true;
1187 }
1188
1189 Class<?> memberDeclaringClassOrNull() {
1190 return (member == null) ? null : member.getDeclaringClass();
1191 }
1192
1193 BasicType returnType() {
1194 return basicType(methodType().returnType());
1195 }
1196
1197 BasicType parameterType(int n) {
1198 return basicType(methodType().parameterType(n));
1199 }
1200
1201 int arity() {
1202 return methodType().parameterCount();
1203 }
1204
1205 public String toString() {
1206 if (member == null) return String.valueOf(resolvedHandle);
1207 return member.getDeclaringClass().getSimpleName()+"."+member.getName();
1208 }
1209
1210 public boolean isIdentity() {
1211 return this.equals(identity(returnType()));
1212 }
1213
1214 public MethodHandleImpl.Intrinsic intrinsicName() {
1215 return resolvedHandle != null
1216 ? resolvedHandle.intrinsicName()
1217 : MethodHandleImpl.Intrinsic.NONE;
1218 }
1219
1220 public Object intrinsicData() {
1221 return resolvedHandle != null
1222 ? resolvedHandle.intrinsicData()
1223 : null;
1224 }
1225 }
1226
1227 public static String basicTypeSignature(MethodType type) {
1228 int params = type.parameterCount();
1229 char[] sig = new char[params + 2];
1230 int sigp = 0;
1231 while (sigp < params) {
1232 sig[sigp] = basicTypeChar(type.parameterType(sigp++));
1233 }
1234 sig[sigp++] = '_';
1235 sig[sigp++] = basicTypeChar(type.returnType());
1236 assert(sigp == sig.length);
1237 return String.valueOf(sig);
1238 }
1239
1240 /** Hack to make signatures more readable when they show up in method names.
1241 * Signature should start with a sequence of uppercase ASCII letters.
1242 * Runs of three or more are replaced by a single letter plus a decimal repeat count.
1243 * A tail of anything other than uppercase ASCII is passed through unchanged.
1244 * @param signature sequence of uppercase ASCII letters with possible repetitions
1245 * @return same sequence, with repetitions counted by decimal numerals
1246 */
1247 public static String shortenSignature(String signature) {
1248 final int NO_CHAR = -1, MIN_RUN = 3;
1249 int c0, c1 = NO_CHAR, c1reps = 0;
1250 StringBuilder buf = null;
1251 int len = signature.length();
1252 if (len < MIN_RUN) return signature;
1253 for (int i = 0; i <= len; i++) {
1254 if (c1 != NO_CHAR && !('A' <= c1 && c1 <= 'Z')) {
1255 // wrong kind of char; bail out here
1256 if (buf != null) {
1257 buf.append(signature, i - c1reps, len);
1258 }
1259 break;
1260 }
1261 // shift in the next char:
1262 c0 = c1; c1 = (i == len ? NO_CHAR : signature.charAt(i));
1263 if (c1 == c0) { ++c1reps; continue; }
1264 // shift in the next count:
1265 int c0reps = c1reps; c1reps = 1;
1266 // end of a character run
1267 if (c0reps < MIN_RUN) {
1268 if (buf != null) {
1269 while (--c0reps >= 0)
1270 buf.append((char)c0);
1271 }
1272 continue;
1273 }
1274 // found three or more in a row
1275 if (buf == null)
1276 buf = new StringBuilder().append(signature, 0, i - c0reps);
1277 buf.append((char)c0).append(c0reps);
1278 }
1279 return (buf == null) ? signature : buf.toString();
1280 }
1281
1282 static final class Name {
1283 final BasicType type;
1284 final short index;
1285 final NamedFunction function;
1286 final Object constraint; // additional type information, if not null
1287 @Stable final Object[] arguments;
1288
1289 private static final Object[] EMPTY_ARGS = new Object[0];
1290
1291 private Name(int index, BasicType type, NamedFunction function, Object[] arguments, Object constraint) {
1292 this.index = (short)index;
1293 this.type = type;
1294 this.function = function;
1295 this.arguments = arguments;
1296 this.constraint = constraint;
1297 assert(this.index == index && typesMatch(function, arguments));
1298 assert(constraint == null || isParam()); // only params have constraints
1299 assert(constraint == null || constraint instanceof ClassSpecializer.SpeciesData || constraint instanceof Class);
1300 }
1301
1302 Name(MethodHandle function, Object... arguments) {
1303 this(new NamedFunction(function), arguments);
1304 }
1305 Name(MethodType functionType, Object... arguments) {
1306 this(new NamedFunction(functionType), arguments);
1307 assert(arguments[0] instanceof Name name && name.type == L_TYPE);
1308 }
1309 Name(MemberName function, Object... arguments) {
1310 this(new NamedFunction(function), arguments);
1311 }
1312 Name(NamedFunction function) {
1313 this(-1, function.returnType(), function, EMPTY_ARGS, null);
1314 }
1315 Name(NamedFunction function, Object arg) {
1316 this(-1, function.returnType(), function, new Object[] { arg }, null);
1317 }
1318 Name(NamedFunction function, Object arg0, Object arg1) {
1319 this(-1, function.returnType(), function, new Object[] { arg0, arg1 }, null);
1320 }
1321 Name(NamedFunction function, Object... arguments) {
1322 this(-1, function.returnType(), function, Arrays.copyOf(arguments, arguments.length, Object[].class), null);
1323 }
1324 /** Create a raw parameter of the given type, with an expected index. */
1325 Name(int index, BasicType type) {
1326 this(index, type, null, null, null);
1327 }
1328 /** Create a raw parameter of the given type. */
1329 Name(BasicType type) { this(-1, type); }
1330
1331 BasicType type() { return type; }
1332 int index() { return index; }
1333
1334 char typeChar() {
1335 return type.btChar;
1336 }
1337
1338 Name withIndex(int i) {
1339 if (i == this.index) return this;
1340 return new Name(i, type, function, arguments, constraint);
1341 }
1342
1343 Name withConstraint(Object constraint) {
1344 if (constraint == this.constraint) return this;
1345 return new Name(index, type, function, arguments, constraint);
1346 }
1347
1348 Name replaceName(Name oldName, Name newName) { // FIXME: use replaceNames uniformly
1349 if (oldName == newName) return this;
1350 @SuppressWarnings("LocalVariableHidesMemberVariable")
1351 Object[] arguments = this.arguments;
1352 if (arguments == null) return this;
1353 boolean replaced = false;
1354 for (int j = 0; j < arguments.length; j++) {
1355 if (arguments[j] == oldName) {
1356 if (!replaced) {
1357 replaced = true;
1358 arguments = arguments.clone();
1359 }
1360 arguments[j] = newName;
1361 }
1362 }
1363 if (!replaced) return this;
1364 return new Name(function, arguments);
1365 }
1366 /** In the arguments of this Name, replace oldNames[i] pairwise by newNames[i].
1367 * Limit such replacements to {@code start<=i<end}. Return possibly changed self.
1368 */
1369 Name replaceNames(Name[] oldNames, Name[] newNames, int start, int end) {
1370 if (start >= end) return this;
1371 @SuppressWarnings("LocalVariableHidesMemberVariable")
1372 Object[] arguments = this.arguments;
1373 boolean replaced = false;
1374 eachArg:
1375 for (int j = 0; j < arguments.length; j++) {
1376 if (arguments[j] instanceof Name n) {
1377 int check = n.index;
1378 // harmless check to see if the thing is already in newNames:
1379 if (check >= 0 && check < newNames.length && n == newNames[check])
1380 continue eachArg;
1381 // n might not have the correct index: n != oldNames[n.index].
1382 for (int i = start; i < end; i++) {
1383 if (n == oldNames[i]) {
1384 if (n == newNames[i])
1385 continue eachArg;
1386 if (!replaced) {
1387 replaced = true;
1388 arguments = arguments.clone();
1389 }
1390 arguments[j] = newNames[i];
1391 continue eachArg;
1392 }
1393 }
1394 }
1395 }
1396 if (!replaced) return this;
1397 return new Name(function, arguments);
1398 }
1399 void internArguments() {
1400 @SuppressWarnings("LocalVariableHidesMemberVariable")
1401 Object[] arguments = this.arguments;
1402 for (int j = 0; j < arguments.length; j++) {
1403 if (arguments[j] instanceof Name n) {
1404 if (n.isParam() && n.index < INTERNED_ARGUMENT_LIMIT)
1405 arguments[j] = internArgument(n);
1406 }
1407 }
1408 }
1409 boolean isParam() {
1410 return function == null;
1411 }
1412
1413 boolean refersTo(Class<?> declaringClass, String methodName) {
1414 return function != null &&
1415 function.member() != null && function.member().refersTo(declaringClass, methodName);
1416 }
1417
1418 /**
1419 * Check if MemberName is a call to MethodHandle.invokeBasic.
1420 */
1421 boolean isInvokeBasic() {
1422 if (function == null)
1423 return false;
1424 if (arguments.length < 1)
1425 return false; // must have MH argument
1426 MemberName member = function.member();
1427 return member != null && member.refersTo(MethodHandle.class, "invokeBasic") &&
1428 !member.isPublic() && !member.isStatic();
1429 }
1430
1431 /**
1432 * Check if MemberName is a call to MethodHandle.linkToStatic, etc.
1433 */
1434 boolean isLinkerMethodInvoke() {
1435 if (function == null)
1436 return false;
1437 if (arguments.length < 1)
1438 return false; // must have MH argument
1439 MemberName member = function.member();
1440 return member != null &&
1441 member.getDeclaringClass() == MethodHandle.class &&
1442 !member.isPublic() && member.isStatic() &&
1443 member.getName().startsWith("linkTo");
1444 }
1445
1446 public String toString() {
1447 return (isParam()?"a":"t")+(index >= 0 ? index : System.identityHashCode(this))+":"+typeChar();
1448 }
1449 public String debugString() {
1450 String s = paramString();
1451 return (function == null) ? s : s + "=" + exprString();
1452 }
1453 public String paramString() {
1454 String s = toString();
1455 Object c = constraint;
1456 if (c == null)
1457 return s;
1458 if (c instanceof Class<?> cl) c = cl.getSimpleName();
1459 return s + "/" + c;
1460 }
1461 public String exprString() {
1462 if (function == null) return toString();
1463 StringBuilder buf = new StringBuilder(function.toString());
1464 buf.append("(");
1465 String cma = "";
1466 for (Object a : arguments) {
1467 buf.append(cma); cma = ",";
1468 if (a instanceof Name || a instanceof Integer)
1469 buf.append(a);
1470 else
1471 buf.append("(").append(a).append(")");
1472 }
1473 buf.append(")");
1474 return buf.toString();
1475 }
1476
1477 private boolean typesMatch(NamedFunction function, Object ... arguments) {
1478 if (arguments == null) {
1479 assert(function == null);
1480 return true;
1481 }
1482 assert(arguments.length == function.arity()) : "arity mismatch: arguments.length=" + arguments.length + " == function.arity()=" + function.arity() + " in " + debugString();
1483 for (int i = 0; i < arguments.length; i++) {
1484 assert (typesMatch(function.parameterType(i), arguments[i])) : "types don't match: function.parameterType(" + i + ")=" + function.parameterType(i) + ", arguments[" + i + "]=" + arguments[i] + " in " + debugString();
1485 }
1486 return true;
1487 }
1488
1489 private static boolean typesMatch(BasicType parameterType, Object object) {
1490 if (object instanceof Name name) {
1491 return name.type == parameterType;
1492 }
1493 switch (parameterType) {
1494 case I_TYPE: return object instanceof Integer;
1495 case J_TYPE: return object instanceof Long;
1496 case F_TYPE: return object instanceof Float;
1497 case D_TYPE: return object instanceof Double;
1498 }
1499 assert(parameterType == L_TYPE);
1500 return true;
1501 }
1502
1503 /** Return the index of the last occurrence of n in the argument array.
1504 * Return -1 if the name is not used.
1505 */
1506 int lastUseIndex(Name n) {
1507 Object[] arguments = this.arguments;
1508 if (arguments == null) return -1;
1509 for (int i = arguments.length; --i >= 0; ) {
1510 if (arguments[i] == n) return i;
1511 }
1512 return -1;
1513 }
1514
1515 public boolean equals(Name that) {
1516 if (this == that) return true;
1517 if (isParam())
1518 // each parameter is a unique atom
1519 return false; // this != that
1520 return
1521 //this.index == that.index &&
1522 this.type == that.type &&
1523 this.function.equals(that.function) &&
1524 Arrays.equals(this.arguments, that.arguments);
1525 }
1526 @Override
1527 public boolean equals(Object x) {
1528 return x instanceof Name n && equals(n);
1529 }
1530 @Override
1531 public int hashCode() {
1532 if (isParam())
1533 return index | (type.ordinal() << 8);
1534 return function.hashCode() ^ Arrays.hashCode(arguments);
1535 }
1536 }
1537
1538 /** Return the index of the last name which contains n as an argument.
1539 * Return -1 if the name is not used. Return names.length if it is the return value.
1540 */
1541 int lastUseIndex(Name n) {
1542 int ni = n.index, nmax = names.length;
1543 assert(names[ni] == n);
1544 if (result == ni) return nmax; // live all the way beyond the end
1545 for (int i = nmax; --i > ni; ) {
1546 if (names[i].lastUseIndex(n) >= 0)
1547 return i;
1548 }
1549 return -1;
1550 }
1551
1552 /** Return the number of times n is used as an argument or return value. */
1553 int useCount(Name n) {
1554 int count = (result == n.index) ? 1 : 0;
1555 int i = Math.max(n.index + 1, arity);
1556 Name[] names = this.names;
1557 while (i < names.length) {
1558 Object[] arguments = names[i++].arguments;
1559 if (arguments != null) {
1560 for (Object argument : arguments) {
1561 if (argument == n) {
1562 count++;
1563 }
1564 }
1565 }
1566 }
1567 return count;
1568 }
1569
1570 static Name argument(int which, BasicType type) {
1571 if (which >= INTERNED_ARGUMENT_LIMIT)
1572 return new Name(which, type);
1573 return INTERNED_ARGUMENTS[type.ordinal()][which];
1574 }
1575 static Name internArgument(Name n) {
1576 assert(n.isParam()) : "not param: " + n;
1577 assert(n.index < INTERNED_ARGUMENT_LIMIT);
1578 if (n.constraint != null) return n;
1579 return argument(n.index, n.type);
1580 }
1581 static Name[] arguments(int extra, MethodType types) {
1582 int length = types.parameterCount();
1583 Name[] names = new Name[length + extra];
1584 for (int i = 0; i < length; i++)
1585 names[i] = argument(i, basicType(types.parameterType(i)));
1586 return names;
1587 }
1588
1589 static Name[] invokeArguments(int extra, MethodType types) {
1590 int length = types.parameterCount();
1591 Name[] names = new Name[length + extra + 1];
1592 names[0] = argument(0, L_TYPE);
1593 for (int i = 0; i < length; i++)
1594 names[i + 1] = argument(i + 1, basicType(types.parameterType(i)));
1595 return names;
1596 }
1597
1598 static final int INTERNED_ARGUMENT_LIMIT = 10;
1599 private static final Name[][] INTERNED_ARGUMENTS
1600 = new Name[ARG_TYPE_LIMIT][INTERNED_ARGUMENT_LIMIT];
1601 static {
1602 for (BasicType type : BasicType.ARG_TYPES) {
1603 int ord = type.ordinal();
1604 for (int i = 0; i < INTERNED_ARGUMENTS[ord].length; i++) {
1605 INTERNED_ARGUMENTS[ord][i] = new Name(i, type);
1606 }
1607 }
1608 }
1609
1610 private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
1611
1612 static LambdaForm identityForm(BasicType type) {
1613 int ord = type.ordinal();
1614 LambdaForm form = LF_identity[ord];
1615 if (form != null) {
1616 return form;
1617 }
1618 createIdentityForm(type);
1619 return LF_identity[ord];
1620 }
1621
1622 static NamedFunction identity(BasicType type) {
1623 int ord = type.ordinal();
1624 NamedFunction function = NF_identity[ord];
1625 if (function != null) {
1626 return function;
1627 }
1628 createIdentityForm(type);
1629 return NF_identity[ord];
1630 }
1631
1632 static LambdaForm constantForm(BasicType type) {
1633 assert type != null && type != V_TYPE : type;
1634 var cached = LF_constant[type.ordinal()];
1635 if (cached != null)
1636 return cached;
1637 return createConstantForm(type);
1638 }
1639
1640 private static LambdaForm createConstantForm(BasicType type) {
1641 UNSAFE.ensureClassInitialized(BoundMethodHandle.class); // defend access to SimpleMethodHandle
1642 var species = SimpleMethodHandle.BMH_SPECIES.extendWith(type);
1643 var carrier = argument(0, L_TYPE).withConstraint(species); // BMH bound with data
1644 Name[] constNames = new Name[] { carrier, new Name(species.getterFunction(0), carrier) };
1645 return LF_constant[type.ordinal()] = create(1, constNames, Kind.CONSTANT);
1646 }
1647
1648 private static final @Stable LambdaForm[] LF_identity = new LambdaForm[TYPE_LIMIT];
1649 private static final @Stable NamedFunction[] NF_identity = new NamedFunction[TYPE_LIMIT];
1650 private static final @Stable LambdaForm[] LF_constant = new LambdaForm[ARG_TYPE_LIMIT]; // no void
1651
1652 private static final Object createIdentityFormLock = new Object();
1653 private static void createIdentityForm(BasicType type) {
1654 // Avoid racy initialization during bootstrap
1655 UNSAFE.ensureClassInitialized(BoundMethodHandle.class);
1656 synchronized (createIdentityFormLock) {
1657 final int ord = type.ordinal();
1658 LambdaForm idForm = LF_identity[ord];
1659 if (idForm != null) {
1660 return;
1661 }
1662 char btChar = type.basicTypeChar();
1663 boolean isVoid = (type == V_TYPE);
1664 Class<?> btClass = type.btClass;
1665 MethodType idType = (isVoid) ? MethodType.methodType(btClass) : MethodType.methodType(btClass, btClass);
1666
1667 // Look up symbolic names. It might not be necessary to have these,
1668 // but if we need to emit direct references to bytecodes, it helps.
1669 MemberName idMem = new MemberName(LambdaForm.class, "identity_"+btChar, idType, REF_invokeStatic);
1670 try {
1671 idMem = IMPL_NAMES.resolveOrFail(REF_invokeStatic, idMem, null, LM_TRUSTED, NoSuchMethodException.class);
1672 } catch (IllegalAccessException|NoSuchMethodException ex) {
1673 throw newInternalError(ex);
1674 }
1675
1676 NamedFunction idFun;
1677
1678 // Create the LFs and NamedFunctions. Precompiling LFs to byte code is needed to break circular
1679 // bootstrap dependency on this method in case we're interpreting LFs
1680 if (isVoid) {
1681 Name[] idNames = new Name[] { argument(0, L_TYPE) };
1682 idForm = LambdaForm.create(1, idNames, VOID_RESULT, Kind.IDENTITY);
1683 idForm.compileToBytecode();
1684 idFun = new NamedFunction(idMem, SimpleMethodHandle.make(idMem.getInvocationType(), idForm));
1685 } else {
1686 Name[] idNames = new Name[] { argument(0, L_TYPE), argument(1, type) };
1687 idForm = LambdaForm.create(2, idNames, 1, Kind.IDENTITY);
1688 idForm.compileToBytecode();
1689 idFun = new NamedFunction(idMem, MethodHandleImpl.makeIntrinsic(idMem.getInvocationType(), idForm,
1690 MethodHandleImpl.Intrinsic.IDENTITY));
1691 }
1692
1693 LF_identity[ord] = idForm;
1694 NF_identity[ord] = idFun;
1695
1696 assert(idFun.isIdentity());
1697 }
1698 }
1699
1700 // Avoid appealing to ValueConversions at bootstrap time:
1701 private static int identity_I(int x) { return x; }
1702 private static long identity_J(long x) { return x; }
1703 private static float identity_F(float x) { return x; }
1704 private static double identity_D(double x) { return x; }
1705 private static Object identity_L(Object x) { return x; }
1706 private static void identity_V() { return; }
1707 /**
1708 * Internal marker for byte-compiled LambdaForms.
1709 */
1710 /*non-public*/
1711 @Target(ElementType.METHOD)
1712 @Retention(RetentionPolicy.RUNTIME)
1713 @interface Compiled {
1714 }
1715
1716 private static final HashMap<String,Integer> DEBUG_NAME_COUNTERS;
1717 private static final HashMap<LambdaForm,String> DEBUG_NAMES;
1718 static {
1719 if (debugEnabled()) {
1720 DEBUG_NAME_COUNTERS = new HashMap<>();
1721 DEBUG_NAMES = new HashMap<>();
1722 } else {
1723 DEBUG_NAME_COUNTERS = null;
1724 DEBUG_NAMES = null;
1725 }
1726 }
1727
1728 static {
1729 // The Holder class will contain pre-generated forms resolved
1730 // using MemberName.getFactory(). However, that doesn't initialize the
1731 // class, which subtly breaks inlining etc. By forcing
1732 // initialization of the Holder class we avoid these issues.
1733 UNSAFE.ensureClassInitialized(Holder.class);
1734 }
1735
1736 /// Holds pre-generated bytecode for common lambda forms.
1737 ///
1738 /// This class may be substituted in the JDK's modules image, or in an AOT
1739 /// cache, by a version generated by [GenerateJLIClassesHelper].
1740 ///
1741 /// The method names of this class are internal tokens recognized by
1742 /// [InvokerBytecodeGenerator#lookupPregenerated] and are subject to change.
1743 @AOTSafeClassInitializer
1744 final class Holder {}
1745
1746 // The following hack is necessary in order to suppress TRACE_INTERPRETER
1747 // during execution of the static initializes of this class.
1748 // Turning on TRACE_INTERPRETER too early will cause
1749 // stack overflows and other misbehavior during attempts to trace events
1750 // that occur during LambdaForm.<clinit>.
1751 // Therefore, do not move this line higher in this file, and do not remove.
1752 private static final boolean TRACE_INTERPRETER = MethodHandleStatics.TRACE_INTERPRETER;
1753 }