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