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