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