1 /* 2 * Copyright (c) 2008, 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 static jdk.internal.org.objectweb.asm.Opcodes.*; 29 import static java.lang.invoke.LambdaForm.*; 30 import static java.lang.invoke.LambdaForm.BasicType.*; 31 import static java.lang.invoke.MethodHandleStatics.*; 32 33 import java.lang.invoke.LambdaForm.NamedFunction; 34 import java.lang.invoke.MethodHandles.Lookup; 35 import java.lang.reflect.Field; 36 import java.util.Arrays; 37 import java.util.HashMap; 38 39 import sun.invoke.util.ValueConversions; 40 import sun.invoke.util.Wrapper; 41 42 import jdk.internal.org.objectweb.asm.ClassWriter; 43 import jdk.internal.org.objectweb.asm.MethodVisitor; 44 import jdk.internal.org.objectweb.asm.Type; 45 46 /** 47 * The flavor of method handle which emulates an invoke instruction 48 * on a predetermined argument. The JVM dispatches to the correct method 49 * when the handle is created, not when it is invoked. 50 * 51 * All bound arguments are encapsulated in dedicated species. 52 */ 53 /* non-public */ abstract class BoundMethodHandle extends MethodHandle { 54 55 /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) { 56 super(type, form); 57 } 58 59 // 60 // BMH API and internals 61 // 62 63 static MethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) { 64 // for some type signatures, there exist pre-defined concrete BMH classes 65 try { 66 switch (xtype) { 67 case L_TYPE: 68 if (true) return bindSingle(type, form, x); // Use known fast path. 69 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(L_TYPE).constructor[0].invokeBasic(type, form, x); 70 case I_TYPE: 71 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x)); 72 case J_TYPE: 73 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor[0].invokeBasic(type, form, (long) x); 74 case F_TYPE: 75 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor[0].invokeBasic(type, form, (float) x); 76 case D_TYPE: 77 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor[0].invokeBasic(type, form, (double) x); 78 default : throw newInternalError("unexpected xtype: " + xtype); 79 } 80 } catch (Throwable t) { 81 throw newInternalError(t); 82 } 83 } 84 85 static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) { 86 return new Species_L(type, form, x); 87 } 88 89 MethodHandle cloneExtend(MethodType type, LambdaForm form, BasicType xtype, Object x) { 90 try { 91 switch (xtype) { 92 case L_TYPE: return copyWithExtendL(type, form, x); 93 case I_TYPE: return copyWithExtendI(type, form, ValueConversions.widenSubword(x)); 94 case J_TYPE: return copyWithExtendJ(type, form, (long) x); 95 case F_TYPE: return copyWithExtendF(type, form, (float) x); 96 case D_TYPE: return copyWithExtendD(type, form, (double) x); 97 } 98 } catch (Throwable t) { 99 throw newInternalError(t); 100 } 101 throw newInternalError("unexpected type: " + xtype); 102 } 103 104 @Override 105 MethodHandle bindArgument(int pos, BasicType basicType, Object value) { 106 MethodType type = type().dropParameterTypes(pos, pos+1); 107 LambdaForm form = internalForm().bind(1+pos, speciesData()); 108 return cloneExtend(type, form, basicType, value); 109 } 110 111 @Override 112 MethodHandle dropArguments(MethodType srcType, int pos, int drops) { 113 LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos + drops)); 114 try { 115 return copyWith(srcType, form); 116 } catch (Throwable t) { 117 throw newInternalError(t); 118 } 119 } 120 121 @Override 122 MethodHandle permuteArguments(MethodType newType, int[] reorder) { 123 try { 124 return copyWith(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList()))); 125 } catch (Throwable t) { 126 throw newInternalError(t); 127 } 128 } 129 130 /** 131 * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a 132 * static field containing this value, and they must accordingly implement this method. 133 */ 134 public abstract SpeciesData speciesData(); 135 136 /** 137 * Return the number of fields in this BMH. Equivalent to speciesData().fieldCount(). 138 */ 139 public abstract int fieldCount(); 140 141 @Override 142 final Object internalProperties() { 143 return "/BMH="+internalValues(); 144 } 145 146 @Override 147 final Object internalValues() { 148 Object[] boundValues = new Object[speciesData().fieldCount()]; 149 for (int i = 0; i < boundValues.length; ++i) { 150 boundValues[i] = arg(i); 151 } 152 return Arrays.asList(boundValues); 153 } 154 155 public final Object arg(int i) { 156 try { 157 switch (speciesData().fieldType(i)) { 158 case L_TYPE: return speciesData().getters[i].invokeBasic(this); 159 case I_TYPE: return (int) speciesData().getters[i].invokeBasic(this); 160 case J_TYPE: return (long) speciesData().getters[i].invokeBasic(this); 161 case F_TYPE: return (float) speciesData().getters[i].invokeBasic(this); 162 case D_TYPE: return (double) speciesData().getters[i].invokeBasic(this); 163 } 164 } catch (Throwable ex) { 165 throw newInternalError(ex); 166 } 167 throw new InternalError("unexpected type: " + speciesData().typeChars+"."+i); 168 } 169 170 // 171 // cloning API 172 // 173 174 // The following is a grossly irregular hack: 175 @Override MethodHandle reinvokerTarget() { 176 try { 177 return (MethodHandle) arg(0); 178 } catch (Throwable ex) { 179 throw newInternalError(ex); 180 } 181 } 182 183 @Override 184 public abstract BoundMethodHandle copyWith(MethodType mt, LambdaForm lf); 185 public abstract BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg); 186 public abstract BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int narg); 187 public abstract BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long narg); 188 public abstract BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float narg); 189 public abstract BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg); 190 191 // 192 // concrete BMH classes required to close bootstrap loops 193 // 194 195 private // make it private to force users to access the enclosing class first 196 static final class Species_L extends BoundMethodHandle { 197 final Object argL0; 198 private Species_L(MethodType mt, LambdaForm lf, Object argL0) { 199 super(mt, lf); 200 this.argL0 = argL0; 201 } 202 @Override 203 public SpeciesData speciesData() { 204 return SPECIES_DATA; 205 } 206 @Override 207 public int fieldCount() { 208 return 1; 209 } 210 public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class); 211 public static BoundMethodHandle make(MethodType mt, LambdaForm lf, Object argL0) { 212 return new Species_L(mt, lf, argL0); 213 } 214 @Override 215 public final BoundMethodHandle copyWith(MethodType mt, LambdaForm lf) { 216 return new Species_L(mt, lf, argL0); 217 } 218 @Override 219 public final BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg) { 220 try { 221 return (BoundMethodHandle) SPECIES_DATA.extendWith(L_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg); 222 } catch (Throwable ex) { 223 throw uncaughtException(ex); 224 } 225 } 226 @Override 227 public final BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int narg) { 228 try { 229 return (BoundMethodHandle) SPECIES_DATA.extendWith(I_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg); 230 } catch (Throwable ex) { 231 throw uncaughtException(ex); 232 } 233 } 234 @Override 235 public final BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long narg) { 236 try { 237 return (BoundMethodHandle) SPECIES_DATA.extendWith(J_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg); 238 } catch (Throwable ex) { 239 throw uncaughtException(ex); 240 } 241 } 242 @Override 243 public final BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float narg) { 244 try { 245 return (BoundMethodHandle) SPECIES_DATA.extendWith(F_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg); 246 } catch (Throwable ex) { 247 throw uncaughtException(ex); 248 } 249 } 250 @Override 251 public final BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg) { 252 try { 253 return (BoundMethodHandle) SPECIES_DATA.extendWith(D_TYPE).constructor[0].invokeBasic(mt, lf, argL0, narg); 254 } catch (Throwable ex) { 255 throw uncaughtException(ex); 256 } 257 } 258 } 259 260 // 261 // BMH species meta-data 262 // 263 264 /** 265 * Meta-data wrapper for concrete BMH types. 266 * Each BMH type corresponds to a given sequence of basic field types (LIJFD). 267 * The fields are immutable; their values are fully specified at object construction. 268 * Each BMH type supplies an array of getter functions which may be used in lambda forms. 269 * A BMH is constructed by cloning a shorter BMH and adding one or more new field values. 270 * As a degenerate and common case, the "shorter BMH" can be missing, and contributes zero prior fields. 271 */ 272 static class SpeciesData { 273 final String typeChars; 274 final BasicType[] typeCodes; 275 final Class<? extends BoundMethodHandle> clazz; 276 // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH 277 // Therefore, we need a non-final link in the chain. Use array elements. 278 final MethodHandle[] constructor; 279 final MethodHandle[] getters; 280 final NamedFunction[] nominalGetters; 281 final SpeciesData[] extensions; 282 283 public int fieldCount() { 284 return typeCodes.length; 285 } 286 public BasicType fieldType(int i) { 287 return typeCodes[i]; 288 } 289 public char fieldTypeChar(int i) { 290 return typeChars.charAt(i); 291 } 292 293 public String toString() { 294 return "SpeciesData["+(isPlaceholder() ? "<placeholder>" : clazz.getSimpleName())+":"+typeChars+"]"; 295 } 296 297 /** 298 * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that 299 * represents a MH bound to a generic invoker, which in turn forwards to the corresponding 300 * getter. 301 */ 302 NamedFunction getterFunction(int i) { 303 return nominalGetters[i]; 304 } 305 306 static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class); 307 308 private SpeciesData(String types, Class<? extends BoundMethodHandle> clazz) { 309 this.typeChars = types; 310 this.typeCodes = basicTypes(types); 311 this.clazz = clazz; 312 if (!INIT_DONE) { 313 this.constructor = new MethodHandle[1]; // only one ctor 314 this.getters = new MethodHandle[types.length()]; 315 this.nominalGetters = new NamedFunction[types.length()]; 316 } else { 317 this.constructor = Factory.makeCtors(clazz, types, null); 318 this.getters = Factory.makeGetters(clazz, types, null); 319 this.nominalGetters = Factory.makeNominalGetters(clazz, types, null, this.getters); 320 } 321 this.extensions = new SpeciesData[ARG_TYPE_LIMIT]; 322 } 323 324 private void initForBootstrap() { 325 assert(!INIT_DONE); 326 if (constructor[0] == null) { 327 String types = typeChars; 328 Factory.makeCtors(clazz, types, this.constructor); 329 Factory.makeGetters(clazz, types, this.getters); 330 Factory.makeNominalGetters(clazz, types, this.nominalGetters, this.getters); 331 } 332 } 333 334 private SpeciesData(String typeChars) { 335 // Placeholder only. 336 this.typeChars = typeChars; 337 this.typeCodes = basicTypes(typeChars); 338 this.clazz = null; 339 this.constructor = null; 340 this.getters = null; 341 this.nominalGetters = null; 342 this.extensions = null; 343 } 344 private boolean isPlaceholder() { return clazz == null; } 345 346 private static final HashMap<String, SpeciesData> CACHE = new HashMap<>(); 347 static { CACHE.put("", EMPTY); } // make bootstrap predictable 348 private static final boolean INIT_DONE; // set after <clinit> finishes... 349 350 SpeciesData extendWith(byte type) { 351 return extendWith(BasicType.basicType(type)); 352 } 353 354 SpeciesData extendWith(BasicType type) { 355 int ord = type.ordinal(); 356 SpeciesData d = extensions[ord]; 357 if (d != null) return d; 358 extensions[ord] = d = get(typeChars+type.basicTypeChar()); 359 return d; 360 } 361 362 SpeciesData extendWith(Class<?> type) { 363 return extendWith(basicType(type)); 364 } 365 366 SpeciesData extendWithChar(char type) { 367 return extendWith(basicType(type)); 368 } 369 370 private static SpeciesData get(String types) { 371 // Acquire cache lock for query. 372 SpeciesData d = lookupCache(types); 373 if (!d.isPlaceholder()) 374 return d; 375 synchronized (d) { 376 // Use synch. on the placeholder to prevent multiple instantiation of one species. 377 // Creating this class forces a recursive call to getForClass. 378 if (lookupCache(types).isPlaceholder()) 379 Factory.generateConcreteBMHClass(types); 380 } 381 // Reacquire cache lock. 382 d = lookupCache(types); 383 // Class loading must have upgraded the cache. 384 assert(d != null && !d.isPlaceholder()); 385 return d; 386 } 387 static SpeciesData getForClass(String types, Class<? extends BoundMethodHandle> clazz) { 388 // clazz is a new class which is initializing its SPECIES_DATA field 389 return updateCache(types, new SpeciesData(types, clazz)); 390 } 391 private static synchronized SpeciesData lookupCache(String types) { 392 SpeciesData d = CACHE.get(types); 393 if (d != null) return d; 394 d = new SpeciesData(types); 395 assert(d.isPlaceholder()); 396 CACHE.put(types, d); 397 return d; 398 } 399 private static synchronized SpeciesData updateCache(String types, SpeciesData d) { 400 SpeciesData d2; 401 assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder()); 402 assert(!d.isPlaceholder()); 403 CACHE.put(types, d); 404 return d; 405 } 406 407 static void initStatics() {} 408 409 static { 410 // pre-fill the BMH speciesdata cache with BMH's inner classes 411 final Class<BoundMethodHandle> rootCls = BoundMethodHandle.class; 412 try { 413 for (Class<?> c : rootCls.getDeclaredClasses()) { 414 if (rootCls.isAssignableFrom(c)) { 415 final Class<? extends BoundMethodHandle> cbmh = c.asSubclass(BoundMethodHandle.class); 416 SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh); 417 assert(d != null) : cbmh.getName(); 418 assert(d.clazz == cbmh); 419 assert(d == lookupCache(d.typeChars)); 420 } 421 } 422 } catch (Throwable e) { 423 throw newInternalError(e); 424 } 425 426 for (SpeciesData d : CACHE.values()) { 427 d.initForBootstrap(); 428 } 429 // Note: Do not simplify this, because INIT_DONE must not be 430 // a compile-time constant during bootstrapping. 431 INIT_DONE = Boolean.TRUE; 432 } 433 } 434 435 static SpeciesData getSpeciesData(String types) { 436 return SpeciesData.get(types); 437 } 438 439 /** 440 * Generation of concrete BMH classes. 441 * 442 * A concrete BMH species is fit for binding a number of values adhering to a 443 * given type pattern. Reference types are erased. 444 * 445 * BMH species are cached by type pattern. 446 * 447 * A BMH species has a number of fields with the concrete (possibly erased) types of 448 * bound values. Setters are provided as an API in BMH. Getters are exposed as MHs, 449 * which can be included as names in lambda forms. 450 */ 451 static class Factory { 452 453 static final String JLO_SIG = "Ljava/lang/Object;"; 454 static final String JLS_SIG = "Ljava/lang/String;"; 455 static final String JLC_SIG = "Ljava/lang/Class;"; 456 static final String MH = "java/lang/invoke/MethodHandle"; 457 static final String MH_SIG = "L"+MH+";"; 458 static final String BMH = "java/lang/invoke/BoundMethodHandle"; 459 static final String BMH_SIG = "L"+BMH+";"; 460 static final String SPECIES_DATA = "java/lang/invoke/BoundMethodHandle$SpeciesData"; 461 static final String SPECIES_DATA_SIG = "L"+SPECIES_DATA+";"; 462 463 static final String SPECIES_PREFIX_NAME = "Species_"; 464 static final String SPECIES_PREFIX_PATH = BMH + "$" + SPECIES_PREFIX_NAME; 465 466 static final String BMHSPECIES_DATA_EWI_SIG = "(B)" + SPECIES_DATA_SIG; 467 static final String BMHSPECIES_DATA_GFC_SIG = "(" + JLS_SIG + JLC_SIG + ")" + SPECIES_DATA_SIG; 468 static final String MYSPECIES_DATA_SIG = "()" + SPECIES_DATA_SIG; 469 static final String VOID_SIG = "()V"; 470 static final String INT_SIG = "()I"; 471 472 static final String SIG_INCIPIT = "(Ljava/lang/invoke/MethodType;Ljava/lang/invoke/LambdaForm;"; 473 474 static final String[] E_THROWABLE = new String[] { "java/lang/Throwable" }; 475 476 /** 477 * Generate a concrete subclass of BMH for a given combination of bound types. 478 * 479 * A concrete BMH species adheres to the following schema: 480 * 481 * <pre> 482 * class Species_[[types]] extends BoundMethodHandle { 483 * [[fields]] 484 * final SpeciesData speciesData() { return SpeciesData.get("[[types]]"); } 485 * } 486 * </pre> 487 * 488 * The {@code [[types]]} signature is precisely the string that is passed to this 489 * method. 490 * 491 * The {@code [[fields]]} section consists of one field definition per character in 492 * the type signature, adhering to the naming schema described in the definition of 493 * {@link #makeFieldName}. 494 * 495 * For example, a concrete BMH species for two reference and one integral bound values 496 * would have the following shape: 497 * 498 * <pre> 499 * class BoundMethodHandle { ... private static 500 * final class Species_LLI extends BoundMethodHandle { 501 * final Object argL0; 502 * final Object argL1; 503 * final int argI2; 504 * private Species_LLI(MethodType mt, LambdaForm lf, Object argL0, Object argL1, int argI2) { 505 * super(mt, lf); 506 * this.argL0 = argL0; 507 * this.argL1 = argL1; 508 * this.argI2 = argI2; 509 * } 510 * public final SpeciesData speciesData() { return SPECIES_DATA; } 511 * public final int fieldCount() { return 3; } 512 * public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LLI", Species_LLI.class); 513 * public BoundMethodHandle make(MethodType mt, LambdaForm lf, Object argL0, Object argL1, int argI2) { 514 * return new Species_LLI(mt, lf, argL0, argL1, argI2); 515 * } 516 * public final BoundMethodHandle copyWith(MethodType mt, LambdaForm lf) { 517 * return new Species_LLI(mt, lf, argL0, argL1, argI2); 518 * } 519 * public final BoundMethodHandle copyWithExtendL(MethodType mt, LambdaForm lf, Object narg) { 520 * return SPECIES_DATA.extendWith(L_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg); 521 * } 522 * public final BoundMethodHandle copyWithExtendI(MethodType mt, LambdaForm lf, int narg) { 523 * return SPECIES_DATA.extendWith(I_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg); 524 * } 525 * public final BoundMethodHandle copyWithExtendJ(MethodType mt, LambdaForm lf, long narg) { 526 * return SPECIES_DATA.extendWith(J_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg); 527 * } 528 * public final BoundMethodHandle copyWithExtendF(MethodType mt, LambdaForm lf, float narg) { 529 * return SPECIES_DATA.extendWith(F_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg); 530 * } 531 * public final BoundMethodHandle copyWithExtendD(MethodType mt, LambdaForm lf, double narg) { 532 * return SPECIES_DATA.extendWith(D_TYPE).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg); 533 * } 534 * } 535 * </pre> 536 * 537 * @param types the type signature, wherein reference types are erased to 'L' 538 * @return the generated concrete BMH class 539 */ 540 static Class<? extends BoundMethodHandle> generateConcreteBMHClass(String types) { 541 final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); 542 543 String shortTypes = LambdaForm.shortenSignature(types); 544 final String className = SPECIES_PREFIX_PATH + shortTypes; 545 final String sourceFile = SPECIES_PREFIX_NAME + shortTypes; 546 cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, BMH, null); 547 cw.visitSource(sourceFile, null); 548 549 // emit static types and SPECIES_DATA fields 550 cw.visitField(ACC_PUBLIC + ACC_STATIC, "SPECIES_DATA", SPECIES_DATA_SIG, null, null).visitEnd(); 551 552 // emit bound argument fields 553 for (int i = 0; i < types.length(); ++i) { 554 final char t = types.charAt(i); 555 final String fieldName = makeFieldName(types, i); 556 final String fieldDesc = t == 'L' ? JLO_SIG : String.valueOf(t); 557 cw.visitField(ACC_FINAL, fieldName, fieldDesc, null, null).visitEnd(); 558 } 559 560 MethodVisitor mv; 561 562 // emit constructor 563 mv = cw.visitMethod(ACC_PRIVATE, "<init>", makeSignature(types, true), null, null); 564 mv.visitCode(); 565 mv.visitVarInsn(ALOAD, 0); // this 566 mv.visitVarInsn(ALOAD, 1); // type 567 mv.visitVarInsn(ALOAD, 2); // form 568 569 mv.visitMethodInsn(INVOKESPECIAL, BMH, "<init>", makeSignature("", true), false); 570 571 for (int i = 0, j = 0; i < types.length(); ++i, ++j) { 572 // i counts the arguments, j counts corresponding argument slots 573 char t = types.charAt(i); 574 mv.visitVarInsn(ALOAD, 0); 575 mv.visitVarInsn(typeLoadOp(t), j + 3); // parameters start at 3 576 mv.visitFieldInsn(PUTFIELD, className, makeFieldName(types, i), typeSig(t)); 577 if (t == 'J' || t == 'D') { 578 ++j; // adjust argument register access 579 } 580 } 581 582 mv.visitInsn(RETURN); 583 mv.visitMaxs(0, 0); 584 mv.visitEnd(); 585 586 // emit implementation of speciesData() 587 mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "speciesData", MYSPECIES_DATA_SIG, null, null); 588 mv.visitCode(); 589 mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); 590 mv.visitInsn(ARETURN); 591 mv.visitMaxs(0, 0); 592 mv.visitEnd(); 593 594 // emit implementation of fieldCount() 595 mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "fieldCount", INT_SIG, null, null); 596 mv.visitCode(); 597 int fc = types.length(); 598 if (fc <= (ICONST_5 - ICONST_0)) { 599 mv.visitInsn(ICONST_0 + fc); 600 } else { 601 mv.visitIntInsn(SIPUSH, fc); 602 } 603 mv.visitInsn(IRETURN); 604 mv.visitMaxs(0, 0); 605 mv.visitEnd(); 606 // emit make() ...factory method wrapping constructor 607 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "make", makeSignature(types, false), null, null); 608 mv.visitCode(); 609 // make instance 610 mv.visitTypeInsn(NEW, className); 611 mv.visitInsn(DUP); 612 // load mt, lf 613 mv.visitVarInsn(ALOAD, 0); // type 614 mv.visitVarInsn(ALOAD, 1); // form 615 // load factory method arguments 616 for (int i = 0, j = 0; i < types.length(); ++i, ++j) { 617 // i counts the arguments, j counts corresponding argument slots 618 char t = types.charAt(i); 619 mv.visitVarInsn(typeLoadOp(t), j + 2); // parameters start at 3 620 if (t == 'J' || t == 'D') { 621 ++j; // adjust argument register access 622 } 623 } 624 625 // finally, invoke the constructor and return 626 mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", makeSignature(types, true)); 627 mv.visitInsn(ARETURN); 628 mv.visitMaxs(0, 0); 629 mv.visitEnd(); 630 631 // emit copyWith() 632 mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "copyWith", makeSignature("", false), null, null); 633 mv.visitCode(); 634 // make instance 635 mv.visitTypeInsn(NEW, className); 636 mv.visitInsn(DUP); 637 // load mt, lf 638 mv.visitVarInsn(ALOAD, 1); 639 mv.visitVarInsn(ALOAD, 2); 640 // put fields on the stack 641 emitPushFields(types, className, mv); 642 // finally, invoke the constructor and return 643 mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", makeSignature(types, true)); 644 mv.visitInsn(ARETURN); 645 mv.visitMaxs(0, 0); 646 mv.visitEnd(); 647 648 // for each type, emit copyWithExtendT() 649 for (BasicType type : BasicType.ARG_TYPES) { 650 int ord = type.ordinal(); 651 char btChar = type.basicTypeChar(); 652 mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "copyWithExtend" + btChar, makeSignature(String.valueOf(btChar), false), null, E_THROWABLE); 653 mv.visitCode(); 654 // return SPECIES_DATA.extendWith(t).constructor[0].invokeBasic(mt, lf, argL0, ..., narg) 655 // obtain constructor 656 mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); 657 int iconstInsn = ICONST_0 + ord; 658 assert(iconstInsn <= ICONST_5); 659 mv.visitInsn(iconstInsn); 660 mv.visitMethodInsn(INVOKEVIRTUAL, SPECIES_DATA, "extendWith", BMHSPECIES_DATA_EWI_SIG); 661 mv.visitFieldInsn(GETFIELD, SPECIES_DATA, "constructor", "[" + MH_SIG); 662 mv.visitInsn(ICONST_0); 663 mv.visitInsn(AALOAD); 664 // load mt, lf 665 mv.visitVarInsn(ALOAD, 1); 666 mv.visitVarInsn(ALOAD, 2); 667 // put fields on the stack 668 emitPushFields(types, className, mv); 669 // put narg on stack 670 mv.visitVarInsn(typeLoadOp(btChar), 3); 671 // finally, invoke the constructor and return 672 mv.visitMethodInsn(INVOKEVIRTUAL, MH, "invokeBasic", makeSignature(types + btChar, false), false); 673 mv.visitInsn(ARETURN); 674 mv.visitMaxs(0, 0); 675 mv.visitEnd(); 676 } 677 678 // emit class initializer 679 mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", VOID_SIG, null, null); 680 mv.visitCode(); 681 mv.visitLdcInsn(types); 682 mv.visitLdcInsn(Type.getObjectType(className)); 683 mv.visitMethodInsn(INVOKESTATIC, SPECIES_DATA, "getForClass", BMHSPECIES_DATA_GFC_SIG, false); 684 mv.visitFieldInsn(PUTSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); 685 mv.visitInsn(RETURN); 686 mv.visitMaxs(0, 0); 687 mv.visitEnd(); 688 689 cw.visitEnd(); 690 691 // load class 692 final byte[] classFile = cw.toByteArray(); 693 InvokerBytecodeGenerator.maybeDump(className, classFile); 694 Class<? extends BoundMethodHandle> bmhClass = 695 //UNSAFE.defineAnonymousClass(BoundMethodHandle.class, classFile, null).asSubclass(BoundMethodHandle.class); 696 UNSAFE.defineClass(className, classFile, 0, classFile.length, 697 BoundMethodHandle.class.getClassLoader(), null) 698 .asSubclass(BoundMethodHandle.class); 699 UNSAFE.ensureClassInitialized(bmhClass); 700 701 return bmhClass; 702 } 703 704 private static int typeLoadOp(char t) { 705 switch (t) { 706 case 'L': return ALOAD; 707 case 'I': return ILOAD; 708 case 'J': return LLOAD; 709 case 'F': return FLOAD; 710 case 'D': return DLOAD; 711 default : throw newInternalError("unrecognized type " + t); 712 } 713 } 714 715 private static void emitPushFields(String types, String className, MethodVisitor mv) { 716 for (int i = 0; i < types.length(); ++i) { 717 char tc = types.charAt(i); 718 mv.visitVarInsn(ALOAD, 0); 719 mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc)); 720 } 721 } 722 723 static String typeSig(char t) { 724 return t == 'L' ? JLO_SIG : String.valueOf(t); 725 } 726 727 // 728 // Getter MH generation. 729 // 730 731 private static MethodHandle makeGetter(Class<?> cbmhClass, String types, int index) { 732 String fieldName = makeFieldName(types, index); 733 Class<?> fieldType = Wrapper.forBasicType(types.charAt(index)).primitiveType(); 734 try { 735 return LOOKUP.findGetter(cbmhClass, fieldName, fieldType); 736 } catch (NoSuchFieldException | IllegalAccessException e) { 737 throw newInternalError(e); 738 } 739 } 740 741 static MethodHandle[] makeGetters(Class<?> cbmhClass, String types, MethodHandle[] mhs) { 742 if (mhs == null) mhs = new MethodHandle[types.length()]; 743 for (int i = 0; i < mhs.length; ++i) { 744 mhs[i] = makeGetter(cbmhClass, types, i); 745 assert(mhs[i].internalMemberName().getDeclaringClass() == cbmhClass); 746 } 747 return mhs; 748 } 749 750 static MethodHandle[] makeCtors(Class<? extends BoundMethodHandle> cbmh, String types, MethodHandle mhs[]) { 751 if (mhs == null) mhs = new MethodHandle[1]; 752 if (types.equals("")) return mhs; // hack for empty BMH species 753 mhs[0] = makeCbmhCtor(cbmh, types); 754 return mhs; 755 } 756 757 static NamedFunction[] makeNominalGetters(Class<?> cbmhClass, String types, NamedFunction[] nfs, MethodHandle[] getters) { 758 if (nfs == null) nfs = new NamedFunction[types.length()]; 759 for (int i = 0; i < nfs.length; ++i) { 760 nfs[i] = new NamedFunction(getters[i]); 761 } 762 return nfs; 763 } 764 765 // 766 // Auxiliary methods. 767 // 768 769 static SpeciesData speciesDataFromConcreteBMHClass(Class<? extends BoundMethodHandle> cbmh) { 770 try { 771 Field F_SPECIES_DATA = cbmh.getDeclaredField("SPECIES_DATA"); 772 return (SpeciesData) F_SPECIES_DATA.get(null); 773 } catch (ReflectiveOperationException ex) { 774 throw newInternalError(ex); 775 } 776 } 777 778 /** 779 * Field names in concrete BMHs adhere to this pattern: 780 * arg + type + index 781 * where type is a single character (L, I, J, F, D). 782 */ 783 private static String makeFieldName(String types, int index) { 784 assert index >= 0 && index < types.length(); 785 return "arg" + types.charAt(index) + index; 786 } 787 788 private static String makeSignature(String types, boolean ctor) { 789 StringBuilder buf = new StringBuilder(SIG_INCIPIT); 790 for (char c : types.toCharArray()) { 791 buf.append(typeSig(c)); 792 } 793 return buf.append(')').append(ctor ? "V" : BMH_SIG).toString(); 794 } 795 796 static MethodHandle makeCbmhCtor(Class<? extends BoundMethodHandle> cbmh, String types) { 797 try { 798 return LOOKUP.findStatic(cbmh, "make", MethodType.fromMethodDescriptorString(makeSignature(types, false), null)); 799 } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | TypeNotPresentException e) { 800 throw newInternalError(e); 801 } 802 } 803 } 804 805 private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP; 806 807 static void initStatics() {} 808 static { SpeciesData.initStatics(); } 809 810 /** 811 * All subclasses must provide such a value describing their type signature. 812 */ 813 static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY; 814 815 private static final SpeciesData[] SPECIES_DATA_CACHE = new SpeciesData[5]; 816 private static SpeciesData checkCache(int size, String types) { 817 int idx = size - 1; 818 SpeciesData data = SPECIES_DATA_CACHE[idx]; 819 if (data != null) return data; 820 SPECIES_DATA_CACHE[idx] = data = getSpeciesData(types); 821 return data; 822 } 823 static SpeciesData speciesData_L() { return checkCache(1, "L"); } 824 static SpeciesData speciesData_LL() { return checkCache(2, "LL"); } 825 static SpeciesData speciesData_LLL() { return checkCache(3, "LLL"); } 826 static SpeciesData speciesData_LLLL() { return checkCache(4, "LLLL"); } 827 static SpeciesData speciesData_LLLLL() { return checkCache(5, "LLLLL"); } 828 }