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