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