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