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