1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import jdk.internal.loader.BootLoader;
  29 import jdk.internal.org.objectweb.asm.ClassWriter;
  30 import jdk.internal.org.objectweb.asm.FieldVisitor;
  31 import jdk.internal.org.objectweb.asm.MethodVisitor;
  32 import jdk.internal.vm.annotation.Stable;
  33 import sun.invoke.util.BytecodeName;
  34 
  35 import java.lang.reflect.*;
  36 import java.security.ProtectionDomain;
  37 import java.util.*;
  38 import java.util.concurrent.ConcurrentHashMap;
  39 import java.util.concurrent.ConcurrentMap;
  40 import java.util.function.Function;
  41 
  42 import static java.lang.invoke.LambdaForm.*;
  43 import static java.lang.invoke.MethodHandleStatics.*;
  44 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  45 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  46 
  47 /**
  48  * Class specialization code.
  49  * @param <T> top class under which species classes are created.
  50  * @param <K> key which identifies individual specializations
  51  */
  52 /*non-public*/
  53 abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesData<T,K>> {
  54     private final Class<T> topClass;
  55     private final Class<K> keyType;
  56     private final Class<S> metaType;
  57     private final Method sdAccessor;
  58     private final String sdFieldName;
  59     private final List<Method> transformMethods;
  60     private final MethodType baseConstructorType;
  61     private final S topSpecies;
  62     private final ConcurrentMap<K, S> cache = new ConcurrentHashMap<>();
  63     private final Factory factory;
  64     private @Stable boolean topClassIsSuper;
  65 
  66     /** Return the top type mirror, for type {@code T} */
  67     public final Class<T> topClass() { return topClass; }
  68 
  69     /** Return the key type mirror, for type {@code K} */
  70     public final Class<K> keyType() { return keyType; }
  71 
  72     /** Return the species metadata type mirror, for type {@code S} */
  73     public final Class<S> metaType() { return metaType; }
  74 
  75     /** Report the leading arguments (if any) required by every species factory.
  76      * Every species factory adds its own field types as additional arguments,
  77      * but these arguments always come first, in every factory method.
  78      */
  79     protected MethodType baseConstructorType() { return baseConstructorType; }
  80 
  81     /** Return the trivial species for the null sequence of arguments. */
  82     protected final S topSpecies() { return topSpecies; }
  83 
  84     /** Return the list of transform methods originally given at creation of this specializer. */
  85     protected final List<Method> transformMethods() { return transformMethods; }
  86 
  87     /** Return the factory object used to build and load concrete species code. */
  88     protected final Factory factory() { return factory; }
  89 
  90     /** Constructor for this class specializer.
  91      * @param topClass type mirror for T
  92      * @param keyType type mirror for K
  93      * @param metaType type mirror for S
  94      * @param baseConstructorType principal constructor type
  95      * @param sdAccessor
  96      * @param sdFieldName
  97      */
  98     protected ClassSpecializer(Class<T> topClass, Class<K> keyType, Class<S> metaType,
  99                                MethodType baseConstructorType,
 100                                Method sdAccessor, String sdFieldName,
 101                                List<Method> transformMethods) {
 102         this.topClass = topClass;
 103         this.keyType = keyType;
 104         this.metaType = metaType;
 105         this.sdAccessor = sdAccessor;
 106         if (sdAccessor.getReturnType() != metaType)
 107             throw newInternalError("accessor must return meta-type: "+sdAccessor);
 108         this.transformMethods = List.of(transformMethods.toArray(new Method[transformMethods.size()]));
 109         this.sdFieldName = sdFieldName;
 110         this.baseConstructorType = baseConstructorType.changeReturnType(void.class);
 111         this.factory = makeFactory();
 112         K tsk = topSpeciesKey();
 113         S topSpecies = null;
 114         // Pre-fill the TopClass species-data cache with topSpecies and all TopClass's inner subclasses.
 115         for (Class<? extends T> c : predeclaredSpeciesCode()) {
 116             final Class<? extends T> speciesCode = c.asSubclass(topClass);
 117             K key = factory.loadSpeciesKeyFromPredefinedClass(speciesCode);
 118             S speciesData = newSpeciesData(key);
 119             factory.linkSpeciesDataToCode(speciesData, speciesCode);
 120             factory.linkCodeToSpeciesData(speciesCode, speciesData, true);
 121             cache.put(key, speciesData);
 122             if (tsk != null && tsk.equals(speciesData.key()))  topSpecies = speciesData;
 123         }
 124         if (tsk != null && topSpecies == null) {
 125             // if there is a key, build the top species if needed:
 126             topSpecies = findSpecies(tsk);
 127         }
 128         this.topSpecies = topSpecies;
 129     }
 130 
 131     // Utilities for subclass constructors:
 132     protected static Method reflectMethod(Class<?> defc, String name, Class<?>... ptypes) {
 133         try {
 134             return defc.getDeclaredMethod(name, ptypes);
 135         } catch (NoSuchMethodException ex) {
 136             throw newIAE(defc.getName()+"."+name+MethodType.methodType(void.class, ptypes), ex);
 137         }
 138     }
 139 
 140     protected static <T> Constructor<T> reflectConstructor(Class<T> defc, Class<?>... ptypes) {
 141         try {
 142             return defc.getDeclaredConstructor(ptypes);
 143         } catch (NoSuchMethodException ex) {
 144             throw newIAE(defc.getName()+"("+MethodType.methodType(void.class, ptypes)+")", ex);
 145         }
 146     }
 147 
 148     protected static Field reflectField(Class<?> defc, String name) {
 149         try {
 150             return defc.getDeclaredField(name);
 151         } catch (NoSuchFieldException ex) {
 152             throw newIAE(defc.getName()+"."+name, ex);
 153         }
 154     }
 155 
 156     private static RuntimeException newIAE(String message, Throwable cause) {
 157         return new IllegalArgumentException(message, cause);
 158     }
 159 
 160     public final S findSpecies(K key) {
 161         if (TRACE_RESOLVE) {
 162             System.out.println("[SPECIES_RESOLVE] findSpecies " + key);
 163         }
 164         S speciesData = cache.computeIfAbsent(key, new Function<>() {
 165             @Override
 166             public S apply(K key1) {
 167                 return factory.loadSpecies(newSpeciesData(key1), Factory.USE_CLASS_CACHE);
 168             }
 169         });
 170         if (TRACE_RESOLVE) {
 171             System.out.println("[SPECIES_RESOLVE] findSpecies " + key + " => " + speciesData);
 172         }
 173         // Note:  Species instantiation may throw VirtualMachineError because of
 174         // code cache overflow.  If this happens the species bytecode may be
 175         // loaded but not linked to its species metadata (with MH's etc).
 176         // That will cause a throw out of CHM.computeIfAbsent,
 177         // which will shut down the caller thread.
 178         //
 179         // In a latter attempt to get the same species, the already-loaded
 180         // class will be present in the system dictionary, causing an
 181         // error when the species generator tries to reload it.
 182         // We try to detect this case and link the pre-existing code.
 183         //
 184         // Although it would be better to start fresh by loading a new
 185         // copy, we have to salvage the previously loaded but broken code.
 186         // (As an alternative, we might spin a new class with a new name,
 187         // or use the anonymous class mechanism.)
 188         //
 189         // In the end, as long as everybody goes through the same CHM,
 190         // CHM.computeIfAbsent will ensure only one SpeciesData will be set
 191         // successfully on a concrete class if ever.
 192         // The concrete class is published via SpeciesData instance
 193         // returned here only after the class and species data are linked together.
 194         return speciesData;
 195     }
 196 
 197     /**
 198      * Meta-data wrapper for concrete subtypes of the top class.
 199      * Each concrete subtype corresponds to a given sequence of basic field types (LIJFD).
 200      * The fields are immutable; their values are fully specified at object construction.
 201      * Each species supplies an array of getter functions which may be used in lambda forms.
 202      * A concrete value is always constructed from the full tuple of its field values,
 203      * accompanied by the required constructor parameters.
 204      * There *may* also be transforms which cloning a species instance and
 205      * either replace a constructor parameter or add one or more new field values.
 206      * The shortest possible species has zero fields.
 207      * Subtypes are not interrelated among themselves by subtyping, even though
 208      * it would appear that a shorter species could serve as a supertype of a
 209      * longer one which extends it.
 210      */
 211     public abstract class SpeciesData<T_,K_> {
 212         // Bootstrapping requires circular relations Class -> SpeciesData -> Class
 213         // Therefore, we need non-final links in the chain.  Use @Stable fields.
 214         @Stable private final K key;
 215         @Stable private final List<Class<?>> fieldTypes;
 216         @Stable private Class<? extends T> speciesCode;
 217         @Stable private Field sdField;
 218         @Stable private MethodHandle[] factories = new MethodHandle[0];
 219         @Stable private MethodHandle[] getters;
 220         @Stable private LambdaForm.NamedFunction[] nominalGetters;
 221         @Stable private final MethodHandle[] transformHelpers = new MethodHandle[transformMethods.size()];
 222 
 223         protected SpeciesData(K key) {
 224             this.key = keyType.cast(Objects.requireNonNull(key));
 225             List<Class<?>> types = deriveFieldTypes(key);
 226             int arity = types.size();
 227             this.fieldTypes = List.of(types.toArray(new Class<?>[arity]));
 228         }
 229 
 230         public final K key() {
 231             return key;
 232         }
 233         protected final List<Class<?>> fieldTypes() {
 234             return fieldTypes;
 235         }
 236         protected final int fieldCount() { return fieldTypes.size(); }
 237         protected ClassSpecializer<T,K,S> outer() { return ClassSpecializer.this; }
 238         protected final boolean isResolved() {
 239             return speciesCode != null && factories != null && factories[0] != null;
 240         }
 241         @Override public String toString() {
 242             return metaType.getSimpleName()+"["+key.toString()+" => "+(isResolved() ? speciesCode.getSimpleName() : "UNRESOLVED")+"]";
 243         }
 244         @Override
 245         public int hashCode() {
 246             return key.hashCode();
 247         }
 248         @Override
 249         public boolean equals(Object obj) {
 250             if (!(obj instanceof SpeciesData))  return false;
 251             @SuppressWarnings("rawtypes")
 252             SpeciesData that = (SpeciesData) obj;
 253             return this.outer() == that.outer() && this.key.equals(that.key);
 254         }
 255 
 256         /** Throws NPE if this species is not yet resolved. */
 257         protected final Class<? extends T> speciesCode() {
 258             return Objects.requireNonNull(speciesCode);
 259         }
 260 
 261         /**
 262          * Return a {@link MethodHandle} which can get the indexed field of this species.
 263          * The return type is the type of the species field it accesses.
 264          * The argument type is the {@code fieldHolder} class of this species.
 265          */
 266         protected MethodHandle getter(int i) {
 267             return Objects.requireNonNull(getters[i]);
 268         }
 269 
 270         /**
 271          * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that
 272          * represents a MH bound to a generic invoker, which in turn forwards to the corresponding
 273          * getter.
 274          */
 275         protected LambdaForm.NamedFunction getterFunction(int i) {
 276             LambdaForm.NamedFunction nf = nominalGetters[i];
 277             assert(nf.memberDeclaringClassOrNull() == speciesCode());
 278             assert(nf.returnType() == BasicType.basicType(fieldTypes.get(i)));
 279             return nf;
 280         }
 281 
 282         protected List<LambdaForm.NamedFunction> getterFunctions() {
 283             return List.of(nominalGetters);
 284         }
 285 
 286         protected List<MethodHandle> getters() {
 287             return List.of(getters);
 288         }
 289 
 290         protected MethodHandle factory() {
 291             return Objects.requireNonNull(factories[0]);
 292         }
 293 
 294         protected MethodHandle transformHelper(int whichtm) {
 295             MethodHandle mh = transformHelpers[whichtm];
 296             if (mh != null)  return mh;
 297             mh = deriveTransformHelper(transformMethods().get(whichtm), whichtm);
 298             // Do a little type checking before we start using the MH.
 299             // (It will be called with invokeBasic, so this is our only chance.)
 300             final MethodType mt = transformHelperType(whichtm);
 301             if (!mt.erase().equals(mh.type().erase())) //FIXME: remove after testing
 302                 throw newInternalError("types differ too much: "+mt+" != "+mh.type());
 303             mh = mh.asType(mt);
 304             return transformHelpers[whichtm] = mh;
 305         }
 306 
 307         private final MethodType transformHelperType(int whichtm) {
 308             Method tm = transformMethods().get(whichtm);
 309             ArrayList<Class<?>> args = new ArrayList<>();
 310             ArrayList<Class<?>> fields = new ArrayList<>();
 311             Collections.addAll(args, tm.getParameterTypes());
 312             fields.addAll(fieldTypes());
 313             List<Class<?>> helperArgs = deriveTransformHelperArguments(tm, whichtm, args, fields);
 314             return MethodType.methodType(tm.getReturnType(), helperArgs);
 315         }
 316 
 317         // Hooks for subclasses:
 318 
 319         /** Given a key, derive the list of field types, which all instances of this species must store. */
 320         protected abstract List<Class<?>> deriveFieldTypes(K key);
 321 
 322         /**
 323          * Given the index of a method in the transforms list, supply a factory
 324          * method that takes the arguments of the transform, plus the local fields,
 325          * and produces a value of the required type.
 326          * You can override this to return null or throw if there are no transforms.
 327          * This method exists so that the transforms can be "grown" lazily.
 328          * This is necessary if the transform *adds* a field to an instance,
 329          * which sometimtes requires the creation, on the fly, of an extended species.
 330          * This method is only called once for any particular parameter.
 331          * The species caches the result in a private array.
 332          *
 333          * @param transform the transform being implemented
 334          * @param whichtm the index of that transform in the original list of transforms
 335          * @return the method handle which creates a new result from a mix of transform arguments and field values
 336          */
 337         protected abstract MethodHandle deriveTransformHelper(Method transform, int whichtm);
 338 
 339         /**
 340          * During code generation, this method is called once per transform to determine
 341          * what is the mix of arguments to hand to the transform-helper.  The bytecode
 342          * which marshals these arguments is open-coded in the species-specific transform.
 343          * The two lists are of opaque objects, which you shouldn't do anything with besides
 344          * reordering them into the output list.  (They are both mutable, to make editing
 345          * easier.)  The imputed types of the args correspond to the transform's parameter
 346          * list, while the imputed types of the fields correspond to the species field types.
 347          * After code generation, this method may be called occasionally by error-checking code.
 348          *
 349          * @param transform the transform being implemented
 350          * @param whichtm the index of that transform in the original list of transforms
 351          * @param args a list of opaque objects representing the incoming transform arguments
 352          * @param fields a list of opaque objects representing the field values of the receiver
 353          * @param <X> the common element type of the various lists
 354          * @return a new list
 355          */
 356         protected abstract <X> List<X> deriveTransformHelperArguments(Method transform, int whichtm,
 357                                                                       List<X> args, List<X> fields);
 358 
 359         /** Given a key, generate the name of the class which implements the species for that key.
 360          * This algorithm must be stable.
 361          * @return class name, which by default is {@code outer().topClass().getName() + "$Species_" + deriveTypeString(key)}
 362          */
 363         protected String deriveClassName() {
 364             return topClass.getName() + "$Species_" + deriveTypeString();
 365         }
 366 
 367         /** Default implementation collects basic type characters,
 368          * plus possibly type names, if some types don't correspond
 369          * to basic types.
 370          * @return a string suitable for use in a class name
 371          */
 372         protected String deriveTypeString() {
 373             List<Class<?>> types = fieldTypes();
 374             StringBuilder buf = new StringBuilder();
 375             StringBuilder end = new StringBuilder();
 376             for (Class<?> type : types) {
 377                 BasicType basicType = BasicType.basicType(type);
 378                 if (basicType.basicTypeClass() == type) {
 379                     buf.append(basicType.basicTypeChar());
 380                 } else {
 381                     buf.append('V');
 382                     end.append(classSig(type));
 383                 }
 384             }
 385             String typeString;
 386             if (end.length() > 0) {
 387                 typeString = BytecodeName.toBytecodeName(buf.append("_").append(end).toString());
 388             } else {
 389                 typeString = buf.toString();
 390             }
 391             return LambdaForm.shortenSignature(typeString);
 392         }
 393 
 394         /**
 395          * Report what immediate super-class to use for the concrete class of this species.
 396          * Normally this is {@code topClass}, but if that is an interface, the factory must override.
 397          * The super-class must provide a constructor which takes the {@code baseConstructorType} arguments, if any.
 398          * This hook also allows the code generator to use more than one canned supertype for species.
 399          * @return the super-class of the class to be generated
 400          */
 401         protected Class<? extends T> deriveSuperClass() {
 402             final Class<T> topc = topClass();
 403             if (!topClassIsSuper) {
 404                 try {
 405                     final Constructor<T> con = reflectConstructor(topc, baseConstructorType().parameterArray());
 406                     if (!topc.isInterface() && !Modifier.isPrivate(con.getModifiers())) {
 407                         topClassIsSuper = true;
 408                     }
 409                 } catch (Exception|InternalError ex) {
 410                     // fall through...
 411                 }
 412                 if (!topClassIsSuper) {
 413                     throw newInternalError("must override if the top class cannot serve as a super class");
 414                 }
 415             }
 416             return topc;
 417         }
 418     }
 419 
 420     protected abstract S newSpeciesData(K key);
 421 
 422     protected K topSpeciesKey() {
 423         return null;  // null means don't report a top species
 424     }
 425 
 426     /** Given a key, add an argument of a basic type to the key, if possible.
 427      * @param key key to start with
 428      * @param type type to add
 429      * @return a new key with the extra argument
 430      */
 431     protected abstract K addFieldTypeToKey(K key, Class<?> type);
 432 
 433     /** Returns a method handle which operates on {@code topClass}
 434      * and returns a {@code SpeciesData}.
 435      * The method handle type is {@code (T)S}, which means the
 436      * method handle takes the top-type for this specializer
 437      * and returns the species class subtype.
 438      * @return a method handle that extracts a species data
 439      */
 440     protected final MethodHandle speciesHandle() {
 441         throw new AssertionError("NYI");
 442     }
 443 
 444     /** Given a species class return the {@code speciesData}. */
 445     protected final S species(T instance) {
 446         // FIXME: use the virtual method on the instance
 447         throw new AssertionError("NYI");
 448     }
 449 
 450     /**
 451      * Hook for implementations to report statically generated species classes.
 452      * Use with care.
 453      */
 454     protected List<Class<? extends T>> predeclaredSpeciesCode() {
 455         ArrayList<Class<? extends T>> results = new ArrayList<>();
 456         for (Class<?> c : topClass().getDeclaredClasses()) {
 457             if (topClass().isAssignableFrom(c)) {
 458                 results.add(c.asSubclass(topClass()));
 459             }
 460         }
 461         return results;
 462     }
 463 
 464     /** Code generation support for instances.
 465      * Subclasses can modify the behavior.
 466      */
 467     public class Factory {
 468         /**
 469          * Get a concrete subclass of the top class for a given combination of bound types.
 470          *
 471          * @param speciesData the species requiring the class, not yet linked
 472          * @return a linked version of the same species
 473          */
 474         private S loadSpecies(S speciesData) {
 475             String className = speciesData.deriveClassName();
 476             assert(className.indexOf('/') < 0) : className;
 477             Class<?> salvage = null;
 478             try {
 479                 salvage = BootLoader.loadClassOrNull(className);
 480                 if (TRACE_RESOLVE && salvage != null) {
 481                     System.out.println("[SPECIES_RESOLVE] " + className + " (salvage)");
 482                 }
 483             } catch (Error ex) {
 484                 if (TRACE_RESOLVE) {
 485                     System.out.println("[SPECIES_RESOLVE] " + className + " (Error) " + ex.getMessage());
 486                 }
 487             }
 488             final Class<? extends T> speciesCode;
 489             if (salvage != null) {
 490                 speciesCode = salvage.asSubclass(topClass());
 491                 factory.linkSpeciesDataToCode(speciesData, speciesCode);
 492                 factory.linkCodeToSpeciesData(speciesCode, speciesData, true);
 493             } else {
 494                 // Not pregenerated, generate the class
 495                 try {
 496                     speciesCode = generateConcreteSpeciesCode(className, speciesData);
 497                     // This operation causes a lot of churn:
 498                     linkSpeciesDataToCode(speciesData, speciesCode);
 499                     // This operation commits the relation, but causes little churn:
 500                     linkCodeToSpeciesData(speciesCode, speciesData, false);
 501                 } catch (Error ex) {
 502                     if (TRACE_RESOLVE) {
 503                         System.out.println("[SPECIES_RESOLVE] " + className + " (Error #2)" );
 504                     }
 505                     // We can get here if there is a race condition loading a class.
 506                     // Or maybe we are out of resources.  Back out of the CHM.get and retry.
 507                     throw ex;
 508                 }
 509             }
 510             final S result = factory.loadSpeciesDataFromCode(speciesCode);
 511             if (result == null || result != speciesData && !result.key().equals(speciesData.key()) || !result.isResolved())
 512                 throw newInternalError("bad species class linkage for "+className+": "+result+" != "+speciesData);
 513             return result;
 514         }
 515 
 516         // Extra CHM introduced by JDK-8131129: Attempt to define a duplicate BMH$Species class
 517         // FIXME: Figure out if we still need it, now that species bootstrap doesn't go through <clinit>
 518         private static final boolean USE_CLASS_CACHE = false;
 519         private final ConcurrentMap<K, Class<? extends T>> classCache = new ConcurrentHashMap<>();
 520         private S loadSpecies(S speciesData, boolean useClassCache) {
 521             if (useClassCache) {
 522                 // CHM.computeIfAbsent ensures generateConcreteSpeciesCode is called
 523                 // only once per key.
 524                 final Class<? extends T> speciesCode = classCache.computeIfAbsent(
 525                         speciesData.key(),
 526                         new Function<K, Class<? extends T>>() {
 527                             @Override
 528                             public Class<? extends T> apply(K key) {
 529                                 return loadSpecies(speciesData).speciesCode();
 530                             }
 531                         });
 532                 return factory.loadSpeciesDataFromCode(speciesCode);
 533             }
 534             return loadSpecies(speciesData);
 535         }
 536 
 537         /**
 538          * Generate a concrete subclass of the top class for a given combination of bound types.
 539          *
 540          * A concrete species subclass roughly matches the following schema:
 541          *
 542          * <pre>
 543          * class Species_[[types]] extends [[T]] {
 544          *     final [[S]] speciesData() { return ... }
 545          *     static [[T]] make([[fields]]) { return ... }
 546          *     [[fields]]
 547          *     final [[T]] transform([[args]]) { return ... }
 548          * }
 549          * </pre>
 550          *
 551          * The {@code [[types]]} signature is precisely the key for the species.
 552          *
 553          * The {@code [[fields]]} section consists of one field definition per character in
 554          * the type signature, adhering to the naming schema described in the definition of
 555          * {@link #chooseFieldName}.
 556          *
 557          * For example, a concrete species for two reference and one integral bound values
 558          * have a shape like the following:
 559          *
 560          * <pre>
 561          * class TopClass { ... private static
 562          * final class Species_LLI extends TopClass {
 563          *     final Object argL0;
 564          *     final Object argL1;
 565          *     final int argI2;
 566          *     private Species_LLI(CT ctarg, ..., Object argL0, Object argL1, int argI2) {
 567          *         super(ctarg, ...);
 568          *         this.argL0 = argL0;
 569          *         this.argL1 = argL1;
 570          *         this.argI2 = argI2;
 571          *     }
 572          *     final SpeciesData speciesData() { return BMH_SPECIES; }
 573          *     &#64;Stable static SpeciesData BMH_SPECIES; // injected afterwards
 574          *     static TopClass make(CT ctarg, ..., Object argL0, Object argL1, int argI2) {
 575          *         return new Species_LLI(ctarg, ..., argL0, argL1, argI2);
 576          *     }
 577          *     final TopClass copyWith(CT ctarg, ...) {
 578          *         return new Species_LLI(ctarg, ..., argL0, argL1, argI2);
 579          *     }
 580          *     // two transforms, for the sake of illustration:
 581          *     final TopClass copyWithExtendL(CT ctarg, ..., Object narg) {
 582          *         return BMH_SPECIES.transform(L_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 583          *     }
 584          *     final TopClass copyWithExtendI(CT ctarg, ..., int narg) {
 585          *         return BMH_SPECIES.transform(I_TYPE).invokeBasic(ctarg, ..., argL0, argL1, argI2, narg);
 586          *     }
 587          * }
 588          * </pre>
 589          *
 590          * @param speciesData what species we are generating
 591          * @return the generated concrete TopClass class
 592          */
 593         Class<? extends T> generateConcreteSpeciesCode(String className, SpeciesData<T,K> speciesData) {
 594             byte[] classFile = generateConcreteSpeciesCodeFile(className, speciesData);
 595 
 596             // load class
 597             InvokerBytecodeGenerator.maybeDump(classBCName(className), classFile);
 598             Class<?> speciesCode;
 599             ProtectionDomain pd = null; // FIXME: should it be this?  topClass.getProtectionDomain();
 600             try {
 601                 //speciesCode = IMPL_LOOKUP.in(topClass()).defineClass(classFile);
 602                 speciesCode = UNSAFE.defineClass(className, classFile, 0, classFile.length,
 603                                       topClass.getClassLoader(), pd);
 604             } catch (Exception ex) {
 605                 throw newInternalError(ex);
 606             }
 607 
 608             return speciesCode.asSubclass(topClass());
 609         }
 610 
 611         // These are named like constants because there is only one per specialization scheme:
 612         private final String SPECIES_DATA = classBCName(metaType);
 613         private final String SPECIES_DATA_SIG = classSig(SPECIES_DATA);
 614         private final String SPECIES_DATA_NAME = sdAccessor.getName();
 615         private final int    SPECIES_DATA_MODS = sdAccessor.getModifiers();
 616         private final List<String> TRANSFORM_NAMES;  // derived from transformMethods
 617         private final List<MethodType> TRANSFORM_TYPES;
 618         private final List<Integer> TRANSFORM_MODS;
 619         {
 620             // Tear apart transformMethods to get the names, types, and modifiers.
 621             List<String> tns = new ArrayList<>();
 622             List<MethodType> tts = new ArrayList<>();
 623             List<Integer> tms = new ArrayList<>();
 624             for (Method tm : transformMethods) {
 625                 tns.add(tm.getName());
 626                 final MethodType tt = MethodType.methodType(tm.getReturnType(), tm.getParameterTypes());
 627                 tts.add(tt);
 628                 tms.add(tm.getModifiers());
 629             }
 630             TRANSFORM_NAMES = List.of(tns.toArray(new String[0]));
 631             TRANSFORM_TYPES = List.of(tts.toArray(new MethodType[0]));
 632             TRANSFORM_MODS = List.of(tms.toArray(new Integer[0]));
 633         }
 634         private static final int ACC_PPP = ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED;
 635 
 636         /*non-publc*/ byte[] generateConcreteSpeciesCodeFile(String className0, SpeciesData<T,K> speciesData) {
 637             final String className = classBCName(className0);
 638             final String superClassName = classBCName(speciesData.deriveSuperClass());
 639 
 640             final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
 641             final int NOT_ACC_PUBLIC = 0;  // not ACC_PUBLIC
 642             cw.visit(V1_6, NOT_ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, superClassName, null);
 643 
 644             final String sourceFile = className.substring(className.lastIndexOf('.')+1);
 645             cw.visitSource(sourceFile, null);
 646 
 647             // emit static types and BMH_SPECIES fields
 648             FieldVisitor fw = cw.visitField(NOT_ACC_PUBLIC + ACC_STATIC, sdFieldName, SPECIES_DATA_SIG, null, null);
 649             fw.visitAnnotation(STABLE_SIG, true);
 650             fw.visitEnd();
 651 
 652             // handy holder for dealing with groups of typed values (ctor arguments and fields)
 653             class Var {
 654                 final int index;
 655                 final String name;
 656                 final Class<?> type;
 657                 final String desc;
 658                 final BasicType basicType;
 659                 final int slotIndex;
 660                 Var(int index, int slotIndex) {
 661                     this.index = index;
 662                     this.slotIndex = slotIndex;
 663                     name = null; type = null; desc = null;
 664                     basicType = BasicType.V_TYPE;
 665                 }
 666                 Var(String name, Class<?> type, Var prev) {
 667                     int slotIndex = prev.nextSlotIndex();
 668                     int index = prev.nextIndex();
 669                     if (name == null)  name = "x";
 670                     if (name.endsWith("#"))
 671                         name = name.substring(0, name.length()-1) + index;
 672                     assert(!type.equals(void.class));
 673                     String desc = classSig(type);
 674                     BasicType basicType = BasicType.basicType(type);
 675                     this.index = index;
 676                     this.name = name;
 677                     this.type = type;
 678                     this.desc = desc;
 679                     this.basicType = basicType;
 680                     this.slotIndex = slotIndex;
 681                 }
 682                 Var lastOf(List<Var> vars) {
 683                     int n = vars.size();
 684                     return (n == 0 ? this : vars.get(n-1));
 685                 }
 686                 <X> List<Var> fromTypes(List<X> types) {
 687                     Var prev = this;
 688                     ArrayList<Var> result = new ArrayList<>(types.size());
 689                     int i = 0;
 690                     for (X x : types) {
 691                         String vn = name;
 692                         Class<?> vt;
 693                         if (x instanceof Class) {
 694                             vt = (Class<?>) x;
 695                             // make the names friendlier if debugging
 696                             assert((vn = vn + "_" + (i++)) != null);
 697                         } else {
 698                             @SuppressWarnings("unchecked")
 699                             Var v = (Var) x;
 700                             vn = v.name;
 701                             vt = v.type;
 702                         }
 703                         prev = new Var(vn, vt, prev);
 704                         result.add(prev);
 705                     }
 706                     return result;
 707                 }
 708 
 709                 int slotSize() { return basicType.basicTypeSlots(); }
 710                 int nextIndex() { return index + (slotSize() == 0 ? 0 : 1); }
 711                 int nextSlotIndex() { return slotIndex >= 0 ? slotIndex + slotSize() : slotIndex; }
 712                 boolean isInHeap() { return slotIndex < 0; }
 713                 void emitVarInstruction(int asmop, MethodVisitor mv) {
 714                     if (asmop == ALOAD)
 715                         asmop = typeLoadOp(basicType.basicTypeChar());
 716                     else
 717                         throw new AssertionError("bad op="+asmop+" for desc="+desc);
 718                     mv.visitVarInsn(asmop, slotIndex);
 719                 }
 720                 public void emitFieldInsn(int asmop, MethodVisitor mv) {
 721                     mv.visitFieldInsn(asmop, className, name, desc);
 722                 }
 723             }
 724 
 725             final Var NO_THIS = new Var(0, 0),
 726                     AFTER_THIS = new Var(0, 1),
 727                     IN_HEAP = new Var(0, -1);
 728 
 729             // figure out the field types
 730             final List<Class<?>> fieldTypes = speciesData.fieldTypes();
 731             final List<Var> fields = new ArrayList<>(fieldTypes.size());
 732             {
 733                 Var nextF = IN_HEAP;
 734                 for (Class<?> ft : fieldTypes) {
 735                     String fn = chooseFieldName(ft, nextF.nextIndex());
 736                     nextF = new Var(fn, ft, nextF);
 737                     fields.add(nextF);
 738                 }
 739             }
 740 
 741             // emit bound argument fields
 742             for (Var field : fields) {
 743                 cw.visitField(ACC_FINAL, field.name, field.desc, null, null).visitEnd();
 744             }
 745 
 746             MethodVisitor mv;
 747 
 748             // emit implementation of speciesData()
 749             mv = cw.visitMethod((SPECIES_DATA_MODS & ACC_PPP) | ACC_FINAL,
 750                     SPECIES_DATA_NAME, "()" + SPECIES_DATA_SIG, null, null);
 751             mv.visitCode();
 752             mv.visitFieldInsn(GETSTATIC, className, sdFieldName, SPECIES_DATA_SIG);
 753             mv.visitInsn(ARETURN);
 754             mv.visitMaxs(0, 0);
 755             mv.visitEnd();
 756 
 757             // figure out the constructor arguments
 758             MethodType superCtorType = ClassSpecializer.this.baseConstructorType();
 759             MethodType thisCtorType = superCtorType.appendParameterTypes(fieldTypes);
 760 
 761             // emit constructor
 762             {
 763                 mv = cw.visitMethod(ACC_PRIVATE,
 764                         "<init>", methodSig(thisCtorType), null, null);
 765                 mv.visitCode();
 766                 mv.visitVarInsn(ALOAD, 0); // this
 767 
 768                 final List<Var> ctorArgs = AFTER_THIS.fromTypes(superCtorType.parameterList());
 769                 for (Var ca : ctorArgs) {
 770                     ca.emitVarInstruction(ALOAD, mv);
 771                 }
 772 
 773                 // super(ca...)
 774                 mv.visitMethodInsn(INVOKESPECIAL, superClassName,
 775                         "<init>", methodSig(superCtorType), false);
 776 
 777                 // store down fields
 778                 Var lastFV = AFTER_THIS.lastOf(ctorArgs);
 779                 for (Var f : fields) {
 780                     // this.argL1 = argL1
 781                     mv.visitVarInsn(ALOAD, 0);  // this
 782                     lastFV = new Var(f.name, f.type, lastFV);
 783                     lastFV.emitVarInstruction(ALOAD, mv);
 784                     f.emitFieldInsn(PUTFIELD, mv);
 785                 }
 786 
 787                 mv.visitInsn(RETURN);
 788                 mv.visitMaxs(0, 0);
 789                 mv.visitEnd();
 790             }
 791 
 792             // emit make()  ...factory method wrapping constructor
 793             {
 794                 MethodType ftryType = thisCtorType.changeReturnType(topClass());
 795                 mv = cw.visitMethod(NOT_ACC_PUBLIC + ACC_STATIC,
 796                         "make", methodSig(ftryType), null, null);
 797                 mv.visitCode();
 798                 // make instance
 799                 mv.visitTypeInsn(NEW, className);
 800                 mv.visitInsn(DUP);
 801                 // load factory method arguments:  ctarg... and arg...
 802                 for (Var v : NO_THIS.fromTypes(ftryType.parameterList())) {
 803                     v.emitVarInstruction(ALOAD, mv);
 804                 }
 805 
 806                 // finally, invoke the constructor and return
 807                 mv.visitMethodInsn(INVOKESPECIAL, className,
 808                         "<init>", methodSig(thisCtorType), false);
 809                 mv.visitInsn(ARETURN);
 810                 mv.visitMaxs(0, 0);
 811                 mv.visitEnd();
 812             }
 813 
 814             // For each transform, emit the customized override of the transform method.
 815             // This method mixes together some incoming arguments (from the transform's
 816             // static type signature) with the field types themselves, and passes
 817             // the resulting mish-mosh of values to a method handle produced by
 818             // the species itself.  (Typically this method handle is the factory
 819             // method of this species or a related one.)
 820             for (int whichtm = 0; whichtm < TRANSFORM_NAMES.size(); whichtm++) {
 821                 final String     TNAME = TRANSFORM_NAMES.get(whichtm);
 822                 final MethodType TTYPE = TRANSFORM_TYPES.get(whichtm);
 823                 final int        TMODS = TRANSFORM_MODS.get(whichtm);
 824                 mv = cw.visitMethod((TMODS & ACC_PPP) | ACC_FINAL,
 825                         TNAME, TTYPE.toMethodDescriptorString(), null, E_THROWABLE);
 826                 mv.visitCode();
 827                 // return a call to the corresponding "transform helper", something like this:
 828                 //   MY_SPECIES.transformHelper(whichtm).invokeBasic(ctarg, ..., argL0, ..., xarg)
 829                 mv.visitFieldInsn(GETSTATIC, className,
 830                         sdFieldName, SPECIES_DATA_SIG);
 831                 emitIntConstant(whichtm, mv);
 832                 mv.visitMethodInsn(INVOKEVIRTUAL, SPECIES_DATA,
 833                         "transformHelper", "(I)" + MH_SIG, false);
 834 
 835                 List<Var> targs = AFTER_THIS.fromTypes(TTYPE.parameterList());
 836                 List<Var> tfields = new ArrayList<>(fields);
 837                 // mix them up and load them for the transform helper:
 838                 List<Var> helperArgs = speciesData.deriveTransformHelperArguments(transformMethods.get(whichtm), whichtm, targs, tfields);
 839                 List<Class<?>> helperTypes = new ArrayList<>(helperArgs.size());
 840                 for (Var ha : helperArgs) {
 841                     helperTypes.add(ha.basicType.basicTypeClass());
 842                     if (ha.isInHeap()) {
 843                         assert(tfields.contains(ha));
 844                         mv.visitVarInsn(ALOAD, 0);
 845                         ha.emitFieldInsn(GETFIELD, mv);
 846                     } else {
 847                         assert(targs.contains(ha));
 848                         ha.emitVarInstruction(ALOAD, mv);
 849                     }
 850                 }
 851 
 852                 // jump into the helper (which is probably a factory method)
 853                 final Class<?> rtype = TTYPE.returnType();
 854                 final BasicType rbt = BasicType.basicType(rtype);
 855                 MethodType invokeBasicType = MethodType.methodType(rbt.basicTypeClass(), helperTypes);
 856                 mv.visitMethodInsn(INVOKEVIRTUAL, MH,
 857                         "invokeBasic", methodSig(invokeBasicType), false);
 858                 if (rbt == BasicType.L_TYPE) {
 859                     mv.visitTypeInsn(CHECKCAST, classBCName(rtype));
 860                     mv.visitInsn(ARETURN);
 861                 } else {
 862                     throw newInternalError("NYI: transform of type "+rtype);
 863                 }
 864                 mv.visitMaxs(0, 0);
 865                 mv.visitEnd();
 866             }
 867 
 868             cw.visitEnd();
 869 
 870             return cw.toByteArray();
 871         }
 872 
 873         private int typeLoadOp(char t) {
 874             switch (t) {
 875             case 'L': return ALOAD;
 876             case 'I': return ILOAD;
 877             case 'J': return LLOAD;
 878             case 'F': return FLOAD;
 879             case 'D': return DLOAD;
 880             default : throw newInternalError("unrecognized type " + t);
 881             }
 882         }
 883 
 884         private void emitIntConstant(int con, MethodVisitor mv) {
 885             if (ICONST_M1 - ICONST_0 <= con && con <= ICONST_5 - ICONST_0)
 886                 mv.visitInsn(ICONST_0 + con);
 887             else if (con == (byte) con)
 888                 mv.visitIntInsn(BIPUSH, con);
 889             else if (con == (short) con)
 890                 mv.visitIntInsn(SIPUSH, con);
 891             else {
 892                 mv.visitLdcInsn(con);
 893             }
 894 
 895         }
 896 
 897         //
 898         // Getter MH generation.
 899         //
 900 
 901         private MethodHandle findGetter(Class<?> speciesCode, List<Class<?>> types, int index) {
 902             Class<?> fieldType = types.get(index);
 903             String fieldName = chooseFieldName(fieldType, index);
 904             try {
 905                 return IMPL_LOOKUP.findGetter(speciesCode, fieldName, fieldType);
 906             } catch (NoSuchFieldException | IllegalAccessException e) {
 907                 throw newInternalError(e);
 908             }
 909         }
 910 
 911         private MethodHandle[] findGetters(Class<?> speciesCode, List<Class<?>> types) {
 912             MethodHandle[] mhs = new MethodHandle[types.size()];
 913             for (int i = 0; i < mhs.length; ++i) {
 914                 mhs[i] = findGetter(speciesCode, types, i);
 915                 assert(mhs[i].internalMemberName().getDeclaringClass() == speciesCode);
 916             }
 917             return mhs;
 918         }
 919 
 920         private MethodHandle[] findFactories(Class<? extends T> speciesCode, List<Class<?>> types) {
 921             MethodHandle[] mhs = new MethodHandle[1];
 922             mhs[0] = findFactory(speciesCode, types);
 923             return mhs;
 924         }
 925 
 926         LambdaForm.NamedFunction[] makeNominalGetters(List<Class<?>> types, MethodHandle[] getters) {
 927             LambdaForm.NamedFunction[] nfs = new LambdaForm.NamedFunction[types.size()];
 928             for (int i = 0; i < nfs.length; ++i) {
 929                 nfs[i] = new LambdaForm.NamedFunction(getters[i]);
 930             }
 931             return nfs;
 932         }
 933 
 934         //
 935         // Auxiliary methods.
 936         //
 937 
 938         protected K loadSpeciesKeyFromPredefinedClass(Class<? extends T> speciesCode) {
 939             throw new UnsupportedOperationException("must override if there are predefined classes");
 940         }
 941 
 942         protected void linkSpeciesDataToCode(SpeciesData<T,K> speciesData, Class<? extends T> speciesCode) {
 943             speciesData.speciesCode = speciesCode.asSubclass(topClass);
 944             speciesData.sdField = reflectSDField(speciesCode);
 945             assert(!MethodHandleNatives.moduleSystemInitialized() ||
 946                     // annotation parser can't create proxy classes until module system is fully initialzed
 947                     speciesData.sdField.getDeclaredAnnotation(Stable.class) != null);
 948             final List<Class<?>> types = speciesData.fieldTypes;
 949             speciesData.factories = this.findFactories(speciesCode, types);
 950             speciesData.getters = this.findGetters(speciesCode, types);
 951             speciesData.nominalGetters = this.makeNominalGetters(types, speciesData.getters);
 952         }
 953 
 954         private Field reflectSDField(Class<? extends T> speciesCode) {
 955             final Field field = reflectField(speciesCode, sdFieldName);
 956             assert(field.getType() == metaType);
 957             assert(Modifier.isStatic(field.getModifiers()));
 958             return field;
 959         }
 960 
 961         protected S loadSpeciesDataFromCode(Class<? extends T> speciesCode) {
 962             if (speciesCode == topClass())  return topSpecies;
 963             try {
 964                 Field sdField = reflectSDField(speciesCode);
 965                 UNSAFE.loadFence();
 966                 final S result = metaType.cast(sdField.get(null));
 967                 if (result.outer() != ClassSpecializer.this)  throw newInternalError("wrong class");
 968                 return result;
 969             } catch (ReflectiveOperationException ex) {
 970                 throw newInternalError(ex);
 971             }
 972         }
 973 
 974         protected void linkCodeToSpeciesData(Class<? extends T> speciesCode, SpeciesData<T,K> speciesData, boolean salvage) {
 975             // FIXME: remove this special case hack
 976             if (speciesCode == topClass() && speciesData.key().equals(topSpeciesKey()))  return;
 977             try {
 978                 Field sdField = speciesData.sdField;
 979                 assert(sdField.get(null) == null ||
 980                        salvage && sdField.get(null).equals(speciesData));
 981                 UNSAFE.storeFence();
 982                 sdField.set(null, speciesData);
 983                 UNSAFE.storeFence();
 984             } catch (ReflectiveOperationException ex) {
 985                 throw newInternalError(ex);
 986             }
 987         }
 988 
 989         /**
 990          * Field names in concrete species classes adhere to this pattern:
 991          * type + index, where type is a single character (L, I, J, F, D).
 992          * The factory subclass can customize this.
 993          * The name is purely cosmetic, since it applies to a private field.
 994          */
 995         protected String chooseFieldName(Class<?> type, int index) {
 996             BasicType bt = BasicType.basicType(type);
 997             return "" + bt.basicTypeChar() + index;
 998         }
 999 
1000         MethodHandle findFactory(Class<? extends T> speciesCode, List<Class<?>> types) {
1001             final MethodType type = baseConstructorType().changeReturnType(topClass()).appendParameterTypes(types);
1002             try {
1003                 return IMPL_LOOKUP.findStatic(speciesCode, "make", type);
1004             } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | TypeNotPresentException e) {
1005                 throw newInternalError(e);
1006             }
1007         }
1008     }
1009 
1010     /** Hook that virtualizes the Factory class, allowing subclasses to extend it. */
1011     protected Factory makeFactory() {
1012         return new Factory();
1013     }
1014 
1015 
1016     // Other misc helpers:
1017     private static final String JLO = "java/lang/Object";
1018     private static final String JLO_SIG = "L" + JLO + ";";
1019     private static final String MH = "java/lang/invoke/MethodHandle";
1020     private static final String MH_SIG = "L" + MH + ";";
1021     private static final String V_MH_SIG = "()" + MH_SIG;
1022     private static final String STABLE_SIG = classSig(Stable.class);
1023     private static final String INT_SIG = "()I";
1024     private static final String[] E_THROWABLE = new String[] { "java/lang/Throwable" };
1025     private static final List<Class<?>> NO_TYPES = Collections.nCopies(0, null);
1026     static {
1027         assert(JLO_SIG.equals(classSig(Object.class)));
1028         assert(MH.equals(classBCName(MethodHandle.class)));
1029     }
1030 
1031     private static String methodSig(MethodType mt) {
1032         return mt.toMethodDescriptorString();
1033     }
1034     private static String classSig(Class<?> cls) {
1035         if (cls.isPrimitive() || cls.isArray())
1036             return MethodType.methodType(cls).toMethodDescriptorString().substring(2);
1037         return classSig(classBCName(cls));
1038     }
1039     private static String classSig(String bcName) {
1040         assert(bcName.indexOf('.') < 0);
1041         assert(!bcName.endsWith(";"));
1042         assert(!bcName.startsWith("["));
1043         return "L" + bcName + ";";
1044     }
1045     private static String classBCName(Class<?> cls) {
1046         return classBCName(className(cls));
1047     }
1048     private static String classBCName(String str) {
1049         assert(str.indexOf('/') < 0) : str;
1050         return str.replace('.', '/');
1051     }
1052     private static String className(Class<?> cls) {
1053         assert(!cls.isArray() && !cls.isPrimitive());
1054         return cls.getName();
1055     }
1056 }