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