jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import static jdk.internal.org.objectweb.asm.Opcodes.*; jaroslav@1646: import static java.lang.invoke.LambdaForm.basicTypes; jaroslav@1646: import static java.lang.invoke.MethodHandleNatives.Constants.REF_invokeStatic; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: jaroslav@1646: import java.lang.invoke.LambdaForm.Name; jaroslav@1646: import java.lang.invoke.LambdaForm.NamedFunction; jaroslav@1646: import java.lang.invoke.MethodHandles.Lookup; jaroslav@1646: import java.lang.reflect.Field; jaroslav@1646: import java.util.Arrays; jaroslav@1646: import java.util.HashMap; jaroslav@1646: jaroslav@1646: import sun.invoke.util.ValueConversions; jaroslav@1646: import sun.invoke.util.Wrapper; jaroslav@1646: jaroslav@1646: import jdk.internal.org.objectweb.asm.ClassWriter; jaroslav@1646: import jdk.internal.org.objectweb.asm.MethodVisitor; jaroslav@1646: import jdk.internal.org.objectweb.asm.Type; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * The flavor of method handle which emulates an invoke instruction jaroslav@1646: * on a predetermined argument. The JVM dispatches to the correct method jaroslav@1646: * when the handle is created, not when it is invoked. jaroslav@1646: * jaroslav@1646: * All bound arguments are encapsulated in dedicated species. jaroslav@1646: */ jaroslav@1646: /* non-public */ abstract class BoundMethodHandle extends MethodHandle { jaroslav@1646: jaroslav@1646: /* non-public */ BoundMethodHandle(MethodType type, LambdaForm form) { jaroslav@1646: super(type, form); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // BMH API and internals jaroslav@1646: // jaroslav@1646: jaroslav@1646: static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) { jaroslav@1646: // for some type signatures, there exist pre-defined concrete BMH classes jaroslav@1646: try { jaroslav@1646: switch (xtype) { jaroslav@1646: case 'L': jaroslav@1646: if (true) return bindSingle(type, form, x); // Use known fast path. jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x); jaroslav@1646: case 'I': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x)); jaroslav@1646: case 'J': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x); jaroslav@1646: case 'F': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x); jaroslav@1646: case 'D': jaroslav@1646: return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x); jaroslav@1646: default : throw new InternalError("unexpected xtype: " + xtype); jaroslav@1646: } jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) { jaroslav@1646: return new Species_L(type, form, x); jaroslav@1646: } jaroslav@1646: jaroslav@1646: MethodHandle cloneExtend(MethodType type, LambdaForm form, char xtype, Object x) { jaroslav@1646: try { jaroslav@1646: switch (xtype) { jaroslav@1646: case 'L': return cloneExtendL(type, form, x); jaroslav@1646: case 'I': return cloneExtendI(type, form, ValueConversions.widenSubword(x)); jaroslav@1646: case 'J': return cloneExtendJ(type, form, (long) x); jaroslav@1646: case 'F': return cloneExtendF(type, form, (float) x); jaroslav@1646: case 'D': return cloneExtendD(type, form, (double) x); jaroslav@1646: } jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: throw new InternalError("unexpected type: " + xtype); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle bindArgument(int pos, char basicType, Object value) { jaroslav@1646: MethodType type = type().dropParameterTypes(pos, pos+1); jaroslav@1646: LambdaForm form = internalForm().bind(1+pos, speciesData()); jaroslav@1646: return cloneExtend(type, form, basicType, value); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle dropArguments(MethodType srcType, int pos, int drops) { jaroslav@1646: LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos+drops)); jaroslav@1646: try { jaroslav@1646: return clone(srcType, form); jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: MethodHandle permuteArguments(MethodType newType, int[] reorder) { jaroslav@1646: try { jaroslav@1646: return clone(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList()))); jaroslav@1646: } catch (Throwable t) { jaroslav@1646: throw newInternalError(t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final String EXTENSION_TYPES = "LIJFD"; jaroslav@1646: static final byte INDEX_L = 0, INDEX_I = 1, INDEX_J = 2, INDEX_F = 3, INDEX_D = 4; jaroslav@1646: static byte extensionIndex(char type) { jaroslav@1646: int i = EXTENSION_TYPES.indexOf(type); jaroslav@1646: if (i < 0) throw new InternalError(); jaroslav@1646: return (byte) i; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a jaroslav@1646: * static field containing this value, and they must accordingly implement this method. jaroslav@1646: */ jaroslav@1646: protected abstract SpeciesData speciesData(); jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: final Object internalProperties() { jaroslav@1646: return "/BMH="+internalValues(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: @Override jaroslav@1646: final Object internalValues() { jaroslav@1646: Object[] boundValues = new Object[speciesData().fieldCount()]; jaroslav@1646: for (int i = 0; i < boundValues.length; ++i) { jaroslav@1646: boundValues[i] = arg(i); jaroslav@1646: } jaroslav@1646: return Arrays.asList(boundValues); jaroslav@1646: } jaroslav@1646: jaroslav@1646: public final Object arg(int i) { jaroslav@1646: try { jaroslav@1646: switch (speciesData().fieldType(i)) { jaroslav@1646: case 'L': return argL(i); jaroslav@1646: case 'I': return argI(i); jaroslav@1646: case 'F': return argF(i); jaroslav@1646: case 'D': return argD(i); jaroslav@1646: case 'J': return argJ(i); jaroslav@1646: } jaroslav@1646: } catch (Throwable ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: throw new InternalError("unexpected type: " + speciesData().types+"."+i); jaroslav@1646: } jaroslav@1646: public final Object argL(int i) throws Throwable { return speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final int argI(int i) throws Throwable { return (int) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final float argF(int i) throws Throwable { return (float) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final double argD(int i) throws Throwable { return (double) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: public final long argJ(int i) throws Throwable { return (long) speciesData().getters[i].invokeBasic(this); } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // cloning API jaroslav@1646: // jaroslav@1646: jaroslav@1646: public abstract BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable; jaroslav@1646: public abstract BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable; jaroslav@1646: jaroslav@1646: // The following is a grossly irregular hack: jaroslav@1646: @Override MethodHandle reinvokerTarget() { jaroslav@1646: try { jaroslav@1646: return (MethodHandle) argL(0); jaroslav@1646: } catch (Throwable ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // concrete BMH classes required to close bootstrap loops jaroslav@1646: // jaroslav@1646: jaroslav@1646: private // make it private to force users to access the enclosing class first jaroslav@1646: static final class Species_L extends BoundMethodHandle { jaroslav@1646: final Object argL0; jaroslav@1646: public Species_L(MethodType mt, LambdaForm lf, Object argL0) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argL0 = argL0; jaroslav@1646: } jaroslav@1646: // The following is a grossly irregular hack: jaroslav@1646: @Override MethodHandle reinvokerTarget() { return (MethodHandle) argL0; } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("L", Species_L.class); jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_L(mt, lf, argL0); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /* jaroslav@1646: static final class Species_LL extends BoundMethodHandle { jaroslav@1646: final Object argL0; jaroslav@1646: final Object argL1; jaroslav@1646: public Species_LL(MethodType mt, LambdaForm lf, Object argL0, Object argL1) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argL0 = argL0; jaroslav@1646: this.argL1 = argL1; jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LL", Species_LL.class); jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_LL(mt, lf, argL0, argL1); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, argL1, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final class Species_JL extends BoundMethodHandle { jaroslav@1646: final long argJ0; jaroslav@1646: final Object argL1; jaroslav@1646: public Species_JL(MethodType mt, LambdaForm lf, long argJ0, Object argL1) { jaroslav@1646: super(mt, lf); jaroslav@1646: this.argJ0 = argJ0; jaroslav@1646: this.argL1 = argL1; jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public SpeciesData speciesData() { jaroslav@1646: return SPECIES_DATA; jaroslav@1646: } jaroslav@1646: public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("JL", Species_JL.class); jaroslav@1646: @Override public final long argJ0() { return argJ0; } jaroslav@1646: @Override public final Object argL1() { return argL1; } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) throws Throwable { jaroslav@1646: return new Species_JL(mt, lf, argJ0, argL1); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: @Override jaroslav@1646: public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) throws Throwable { jaroslav@1646: return (BoundMethodHandle) SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argJ0, argL1, narg); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: */ jaroslav@1646: jaroslav@1646: // jaroslav@1646: // BMH species meta-data jaroslav@1646: // jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Meta-data wrapper for concrete BMH classes. jaroslav@1646: */ jaroslav@1646: static class SpeciesData { jaroslav@1646: final String types; jaroslav@1646: final Class clazz; jaroslav@1646: // Bootstrapping requires circular relations MH -> BMH -> SpeciesData -> MH jaroslav@1646: // Therefore, we need a non-final link in the chain. Use array elements. jaroslav@1646: final MethodHandle[] constructor; jaroslav@1646: final MethodHandle[] getters; jaroslav@1646: final SpeciesData[] extensions; jaroslav@1646: jaroslav@1646: public int fieldCount() { jaroslav@1646: return types.length(); jaroslav@1646: } jaroslav@1646: public char fieldType(int i) { jaroslav@1646: return types.charAt(i); jaroslav@1646: } jaroslav@1646: jaroslav@1646: public String toString() { jaroslav@1646: return "SpeciesData["+(isPlaceholder() ? "" : clazz.getSimpleName())+":"+types+"]"; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Return a {@link LambdaForm.Name} containing a {@link LambdaForm.NamedFunction} that jaroslav@1646: * represents a MH bound to a generic invoker, which in turn forwards to the corresponding jaroslav@1646: * getter. jaroslav@1646: */ jaroslav@1646: Name getterName(Name mhName, int i) { jaroslav@1646: MethodHandle mh = getters[i]; jaroslav@1646: assert(mh != null) : this+"."+i; jaroslav@1646: return new Name(mh, mhName); jaroslav@1646: } jaroslav@1646: jaroslav@1646: NamedFunction getterFunction(int i) { jaroslav@1646: return new NamedFunction(getters[i]); jaroslav@1646: } jaroslav@1646: jaroslav@1646: static final SpeciesData EMPTY = new SpeciesData("", BoundMethodHandle.class); jaroslav@1646: jaroslav@1646: private SpeciesData(String types, Class clazz) { jaroslav@1646: this.types = types; jaroslav@1646: this.clazz = clazz; jaroslav@1646: if (!INIT_DONE) { jaroslav@1646: this.constructor = new MethodHandle[1]; jaroslav@1646: this.getters = new MethodHandle[types.length()]; jaroslav@1646: } else { jaroslav@1646: this.constructor = Factory.makeCtors(clazz, types, null); jaroslav@1646: this.getters = Factory.makeGetters(clazz, types, null); jaroslav@1646: } jaroslav@1646: this.extensions = new SpeciesData[EXTENSION_TYPES.length()]; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private void initForBootstrap() { jaroslav@1646: assert(!INIT_DONE); jaroslav@1646: if (constructor[0] == null) { jaroslav@1646: Factory.makeCtors(clazz, types, this.constructor); jaroslav@1646: Factory.makeGetters(clazz, types, this.getters); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: private SpeciesData(String types) { jaroslav@1646: // Placeholder only. jaroslav@1646: this.types = types; jaroslav@1646: this.clazz = null; jaroslav@1646: this.constructor = null; jaroslav@1646: this.getters = null; jaroslav@1646: this.extensions = null; jaroslav@1646: } jaroslav@1646: private boolean isPlaceholder() { return clazz == null; } jaroslav@1646: jaroslav@1646: private static final HashMap CACHE = new HashMap<>(); jaroslav@1646: static { CACHE.put("", EMPTY); } // make bootstrap predictable jaroslav@1646: private static final boolean INIT_DONE; // set after finishes... jaroslav@1646: jaroslav@1646: SpeciesData extendWithType(char type) { jaroslav@1646: int i = extensionIndex(type); jaroslav@1646: SpeciesData d = extensions[i]; jaroslav@1646: if (d != null) return d; jaroslav@1646: extensions[i] = d = get(types+type); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: SpeciesData extendWithIndex(byte index) { jaroslav@1646: SpeciesData d = extensions[index]; jaroslav@1646: if (d != null) return d; jaroslav@1646: extensions[index] = d = get(types+EXTENSION_TYPES.charAt(index)); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static SpeciesData get(String types) { jaroslav@1646: // Acquire cache lock for query. jaroslav@1646: SpeciesData d = lookupCache(types); jaroslav@1646: if (!d.isPlaceholder()) jaroslav@1646: return d; jaroslav@1646: synchronized (d) { jaroslav@1646: // Use synch. on the placeholder to prevent multiple instantiation of one species. jaroslav@1646: // Creating this class forces a recursive call to getForClass. jaroslav@1646: if (lookupCache(types).isPlaceholder()) jaroslav@1646: Factory.generateConcreteBMHClass(types); jaroslav@1646: } jaroslav@1646: // Reacquire cache lock. jaroslav@1646: d = lookupCache(types); jaroslav@1646: // Class loading must have upgraded the cache. jaroslav@1646: assert(d != null && !d.isPlaceholder()); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: static SpeciesData getForClass(String types, Class clazz) { jaroslav@1646: // clazz is a new class which is initializing its SPECIES_DATA field jaroslav@1646: return updateCache(types, new SpeciesData(types, clazz)); jaroslav@1646: } jaroslav@1646: private static synchronized SpeciesData lookupCache(String types) { jaroslav@1646: SpeciesData d = CACHE.get(types); jaroslav@1646: if (d != null) return d; jaroslav@1646: d = new SpeciesData(types); jaroslav@1646: assert(d.isPlaceholder()); jaroslav@1646: CACHE.put(types, d); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: private static synchronized SpeciesData updateCache(String types, SpeciesData d) { jaroslav@1646: SpeciesData d2; jaroslav@1646: assert((d2 = CACHE.get(types)) == null || d2.isPlaceholder()); jaroslav@1646: assert(!d.isPlaceholder()); jaroslav@1646: CACHE.put(types, d); jaroslav@1646: return d; jaroslav@1646: } jaroslav@1646: jaroslav@1646: static { jaroslav@1646: // pre-fill the BMH speciesdata cache with BMH's inner classes jaroslav@1646: final Class rootCls = BoundMethodHandle.class; jaroslav@1646: SpeciesData d0 = BoundMethodHandle.SPECIES_DATA; // trigger class init jaroslav@1646: assert(d0 == null || d0 == lookupCache("")) : d0; jaroslav@1646: try { jaroslav@1646: for (Class c : rootCls.getDeclaredClasses()) { jaroslav@1646: if (rootCls.isAssignableFrom(c)) { jaroslav@1646: final Class cbmh = c.asSubclass(BoundMethodHandle.class); jaroslav@1646: SpeciesData d = Factory.speciesDataFromConcreteBMHClass(cbmh); jaroslav@1646: assert(d != null) : cbmh.getName(); jaroslav@1646: assert(d.clazz == cbmh); jaroslav@1646: assert(d == lookupCache(d.types)); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: } catch (Throwable e) { jaroslav@1646: throw newInternalError(e); jaroslav@1646: } jaroslav@1646: jaroslav@1646: for (SpeciesData d : CACHE.values()) { jaroslav@1646: d.initForBootstrap(); jaroslav@1646: } jaroslav@1646: // Note: Do not simplify this, because INIT_DONE must not be jaroslav@1646: // a compile-time constant during bootstrapping. jaroslav@1646: INIT_DONE = Boolean.TRUE; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static SpeciesData getSpeciesData(String types) { jaroslav@1646: return SpeciesData.get(types); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Generation of concrete BMH classes. jaroslav@1646: * jaroslav@1646: * A concrete BMH species is fit for binding a number of values adhering to a jaroslav@1646: * given type pattern. Reference types are erased. jaroslav@1646: * jaroslav@1646: * BMH species are cached by type pattern. jaroslav@1646: * jaroslav@1646: * A BMH species has a number of fields with the concrete (possibly erased) types of jaroslav@1646: * bound values. Setters are provided as an API in BMH. Getters are exposed as MHs, jaroslav@1646: * which can be included as names in lambda forms. jaroslav@1646: */ jaroslav@1646: static class Factory { jaroslav@1646: jaroslav@1646: static final String JLO_SIG = "Ljava/lang/Object;"; jaroslav@1646: static final String JLS_SIG = "Ljava/lang/String;"; jaroslav@1646: static final String JLC_SIG = "Ljava/lang/Class;"; jaroslav@1646: static final String MH = "java/lang/invoke/MethodHandle"; jaroslav@1646: static final String MH_SIG = "L"+MH+";"; jaroslav@1646: static final String BMH = "java/lang/invoke/BoundMethodHandle"; jaroslav@1646: static final String BMH_SIG = "L"+BMH+";"; jaroslav@1646: static final String SPECIES_DATA = "java/lang/invoke/BoundMethodHandle$SpeciesData"; jaroslav@1646: static final String SPECIES_DATA_SIG = "L"+SPECIES_DATA+";"; jaroslav@1646: jaroslav@1646: static final String SPECIES_PREFIX_NAME = "Species_"; jaroslav@1646: static final String SPECIES_PREFIX_PATH = BMH + "$" + SPECIES_PREFIX_NAME; jaroslav@1646: jaroslav@1646: static final String BMHSPECIES_DATA_EWI_SIG = "(B)" + SPECIES_DATA_SIG; jaroslav@1646: static final String BMHSPECIES_DATA_GFC_SIG = "(" + JLS_SIG + JLC_SIG + ")" + SPECIES_DATA_SIG; jaroslav@1646: static final String MYSPECIES_DATA_SIG = "()" + SPECIES_DATA_SIG; jaroslav@1646: static final String VOID_SIG = "()V"; jaroslav@1646: jaroslav@1646: static final String SIG_INCIPIT = "(Ljava/lang/invoke/MethodType;Ljava/lang/invoke/LambdaForm;"; jaroslav@1646: jaroslav@1646: static final Class[] TYPES = new Class[] { Object.class, int.class, long.class, float.class, double.class }; jaroslav@1646: jaroslav@1646: static final String[] E_THROWABLE = new String[] { "java/lang/Throwable" }; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Generate a concrete subclass of BMH for a given combination of bound types. jaroslav@1646: * jaroslav@1646: * A concrete BMH species adheres to the following schema: jaroslav@1646: * jaroslav@1646: *
jaroslav@1646:          * class Species_[[types]] extends BoundMethodHandle {
jaroslav@1646:          *     [[fields]]
jaroslav@1646:          *     final SpeciesData speciesData() { return SpeciesData.get("[[types]]"); }
jaroslav@1646:          * }
jaroslav@1646:          * 
jaroslav@1646: * jaroslav@1646: * The {@code [[types]]} signature is precisely the string that is passed to this jaroslav@1646: * method. jaroslav@1646: * jaroslav@1646: * The {@code [[fields]]} section consists of one field definition per character in jaroslav@1646: * the type signature, adhering to the naming schema described in the definition of jaroslav@1646: * {@link #makeFieldName}. jaroslav@1646: * jaroslav@1646: * For example, a concrete BMH species for two reference and one integral bound values jaroslav@1646: * would have the following shape: jaroslav@1646: * jaroslav@1646: *
jaroslav@1646:          * class BoundMethodHandle { ... private static
jaroslav@1646:          * final class Species_LLI extends BoundMethodHandle {
jaroslav@1646:          *     final Object argL0;
jaroslav@1646:          *     final Object argL1;
jaroslav@1646:          *     final int argI2;
jaroslav@1646:          *     public Species_LLI(MethodType mt, LambdaForm lf, Object argL0, Object argL1, int argI2) {
jaroslav@1646:          *         super(mt, lf);
jaroslav@1646:          *         this.argL0 = argL0;
jaroslav@1646:          *         this.argL1 = argL1;
jaroslav@1646:          *         this.argI2 = argI2;
jaroslav@1646:          *     }
jaroslav@1646:          *     public final SpeciesData speciesData() { return SPECIES_DATA; }
jaroslav@1646:          *     public static final SpeciesData SPECIES_DATA = SpeciesData.getForClass("LLI", Species_LLI.class);
jaroslav@1646:          *     public final BoundMethodHandle clone(MethodType mt, LambdaForm lf) {
jaroslav@1646:          *         return SPECIES_DATA.constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2);
jaroslav@1646:          *     }
jaroslav@1646:          *     public final BoundMethodHandle cloneExtendL(MethodType mt, LambdaForm lf, Object narg) {
jaroslav@1646:          *         return SPECIES_DATA.extendWithIndex(INDEX_L).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
jaroslav@1646:          *     }
jaroslav@1646:          *     public final BoundMethodHandle cloneExtendI(MethodType mt, LambdaForm lf, int narg) {
jaroslav@1646:          *         return SPECIES_DATA.extendWithIndex(INDEX_I).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
jaroslav@1646:          *     }
jaroslav@1646:          *     public final BoundMethodHandle cloneExtendJ(MethodType mt, LambdaForm lf, long narg) {
jaroslav@1646:          *         return SPECIES_DATA.extendWithIndex(INDEX_J).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
jaroslav@1646:          *     }
jaroslav@1646:          *     public final BoundMethodHandle cloneExtendF(MethodType mt, LambdaForm lf, float narg) {
jaroslav@1646:          *         return SPECIES_DATA.extendWithIndex(INDEX_F).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
jaroslav@1646:          *     }
jaroslav@1646:          *     public final BoundMethodHandle cloneExtendD(MethodType mt, LambdaForm lf, double narg) {
jaroslav@1646:          *         return SPECIES_DATA.extendWithIndex(INDEX_D).constructor[0].invokeBasic(mt, lf, argL0, argL1, argI2, narg);
jaroslav@1646:          *     }
jaroslav@1646:          * }
jaroslav@1646:          * 
jaroslav@1646: * jaroslav@1646: * @param types the type signature, wherein reference types are erased to 'L' jaroslav@1646: * @return the generated concrete BMH class jaroslav@1646: */ jaroslav@1646: static Class generateConcreteBMHClass(String types) { jaroslav@1646: final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); jaroslav@1646: jaroslav@1646: final String className = SPECIES_PREFIX_PATH + types; jaroslav@1646: final String sourceFile = SPECIES_PREFIX_NAME + types; jaroslav@1646: cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, BMH, null); jaroslav@1646: cw.visitSource(sourceFile, null); jaroslav@1646: jaroslav@1646: // emit static types and SPECIES_DATA fields jaroslav@1646: cw.visitField(ACC_PUBLIC + ACC_STATIC, "SPECIES_DATA", SPECIES_DATA_SIG, null, null).visitEnd(); jaroslav@1646: jaroslav@1646: // emit bound argument fields jaroslav@1646: for (int i = 0; i < types.length(); ++i) { jaroslav@1646: final char t = types.charAt(i); jaroslav@1646: final String fieldName = makeFieldName(types, i); jaroslav@1646: final String fieldDesc = t == 'L' ? JLO_SIG : String.valueOf(t); jaroslav@1646: cw.visitField(ACC_FINAL, fieldName, fieldDesc, null, null).visitEnd(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: MethodVisitor mv; jaroslav@1646: jaroslav@1646: // emit constructor jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC, "", makeSignature(types, true), null, null); jaroslav@1646: mv.visitCode(); jaroslav@1646: mv.visitVarInsn(ALOAD, 0); jaroslav@1646: mv.visitVarInsn(ALOAD, 1); jaroslav@1646: mv.visitVarInsn(ALOAD, 2); jaroslav@1646: jaroslav@1646: mv.visitMethodInsn(INVOKESPECIAL, BMH, "", makeSignature("", true)); jaroslav@1646: jaroslav@1646: for (int i = 0, j = 0; i < types.length(); ++i, ++j) { jaroslav@1646: // i counts the arguments, j counts corresponding argument slots jaroslav@1646: char t = types.charAt(i); jaroslav@1646: mv.visitVarInsn(ALOAD, 0); jaroslav@1646: mv.visitVarInsn(typeLoadOp(t), j + 3); // parameters start at 3 jaroslav@1646: mv.visitFieldInsn(PUTFIELD, className, makeFieldName(types, i), typeSig(t)); jaroslav@1646: if (t == 'J' || t == 'D') { jaroslav@1646: ++j; // adjust argument register access jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: mv.visitInsn(RETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: jaroslav@1646: // emit implementation of reinvokerTarget() jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "reinvokerTarget", "()" + MH_SIG, null, null); jaroslav@1646: mv.visitCode(); jaroslav@1646: mv.visitVarInsn(ALOAD, 0); jaroslav@1646: mv.visitFieldInsn(GETFIELD, className, "argL0", JLO_SIG); jaroslav@1646: mv.visitTypeInsn(CHECKCAST, MH); jaroslav@1646: mv.visitInsn(ARETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: jaroslav@1646: // emit implementation of speciesData() jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "speciesData", MYSPECIES_DATA_SIG, null, null); jaroslav@1646: mv.visitCode(); jaroslav@1646: mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); jaroslav@1646: mv.visitInsn(ARETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: jaroslav@1646: // emit clone() jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "clone", makeSignature("", false), null, E_THROWABLE); jaroslav@1646: mv.visitCode(); jaroslav@1646: // return speciesData().constructor[0].invokeBasic(mt, lf, argL0, ...) jaroslav@1646: // obtain constructor jaroslav@1646: mv.visitVarInsn(ALOAD, 0); jaroslav@1646: mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); jaroslav@1646: mv.visitFieldInsn(GETFIELD, SPECIES_DATA, "constructor", "[" + MH_SIG); jaroslav@1646: mv.visitInsn(ICONST_0); jaroslav@1646: mv.visitInsn(AALOAD); jaroslav@1646: // load mt, lf jaroslav@1646: mv.visitVarInsn(ALOAD, 1); jaroslav@1646: mv.visitVarInsn(ALOAD, 2); jaroslav@1646: // put fields on the stack jaroslav@1646: emitPushFields(types, className, mv); jaroslav@1646: // finally, invoke the constructor and return jaroslav@1646: mv.visitMethodInsn(INVOKEVIRTUAL, MH, "invokeBasic", makeSignature(types, false)); jaroslav@1646: mv.visitInsn(ARETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: jaroslav@1646: // for each type, emit cloneExtendT() jaroslav@1646: for (Class c : TYPES) { jaroslav@1646: char t = Wrapper.basicTypeChar(c); jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "cloneExtend" + t, makeSignature(String.valueOf(t), false), null, E_THROWABLE); jaroslav@1646: mv.visitCode(); jaroslav@1646: // return SPECIES_DATA.extendWithIndex(extensionIndex(t)).constructor[0].invokeBasic(mt, lf, argL0, ..., narg) jaroslav@1646: // obtain constructor jaroslav@1646: mv.visitFieldInsn(GETSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); jaroslav@1646: int iconstInsn = ICONST_0 + extensionIndex(t); jaroslav@1646: assert(iconstInsn <= ICONST_5); jaroslav@1646: mv.visitInsn(iconstInsn); jaroslav@1646: mv.visitMethodInsn(INVOKEVIRTUAL, SPECIES_DATA, "extendWithIndex", BMHSPECIES_DATA_EWI_SIG); jaroslav@1646: mv.visitFieldInsn(GETFIELD, SPECIES_DATA, "constructor", "[" + MH_SIG); jaroslav@1646: mv.visitInsn(ICONST_0); jaroslav@1646: mv.visitInsn(AALOAD); jaroslav@1646: // load mt, lf jaroslav@1646: mv.visitVarInsn(ALOAD, 1); jaroslav@1646: mv.visitVarInsn(ALOAD, 2); jaroslav@1646: // put fields on the stack jaroslav@1646: emitPushFields(types, className, mv); jaroslav@1646: // put narg on stack jaroslav@1646: mv.visitVarInsn(typeLoadOp(t), 3); jaroslav@1646: // finally, invoke the constructor and return jaroslav@1646: mv.visitMethodInsn(INVOKEVIRTUAL, MH, "invokeBasic", makeSignature(types + t, false)); jaroslav@1646: mv.visitInsn(ARETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // emit class initializer jaroslav@1646: mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "", VOID_SIG, null, null); jaroslav@1646: mv.visitCode(); jaroslav@1646: mv.visitLdcInsn(types); jaroslav@1646: mv.visitLdcInsn(Type.getObjectType(className)); jaroslav@1646: mv.visitMethodInsn(INVOKESTATIC, SPECIES_DATA, "getForClass", BMHSPECIES_DATA_GFC_SIG); jaroslav@1646: mv.visitFieldInsn(PUTSTATIC, className, "SPECIES_DATA", SPECIES_DATA_SIG); jaroslav@1646: mv.visitInsn(RETURN); jaroslav@1646: mv.visitMaxs(0, 0); jaroslav@1646: mv.visitEnd(); jaroslav@1646: jaroslav@1646: cw.visitEnd(); jaroslav@1646: jaroslav@1646: // load class jaroslav@1646: final byte[] classFile = cw.toByteArray(); jaroslav@1646: InvokerBytecodeGenerator.maybeDump(className, classFile); jaroslav@1646: Class bmhClass = jaroslav@1646: //UNSAFE.defineAnonymousClass(BoundMethodHandle.class, classFile, null).asSubclass(BoundMethodHandle.class); jaroslav@1646: UNSAFE.defineClass(className, classFile, 0, classFile.length, jaroslav@1646: BoundMethodHandle.class.getClassLoader(), null) jaroslav@1646: .asSubclass(BoundMethodHandle.class); jaroslav@1646: UNSAFE.ensureClassInitialized(bmhClass); jaroslav@1646: jaroslav@1646: return bmhClass; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static int typeLoadOp(char t) { jaroslav@1646: switch (t) { jaroslav@1646: case 'L': return ALOAD; jaroslav@1646: case 'I': return ILOAD; jaroslav@1646: case 'J': return LLOAD; jaroslav@1646: case 'F': return FLOAD; jaroslav@1646: case 'D': return DLOAD; jaroslav@1646: default : throw new InternalError("unrecognized type " + t); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static void emitPushFields(String types, String className, MethodVisitor mv) { jaroslav@1646: for (int i = 0; i < types.length(); ++i) { jaroslav@1646: char tc = types.charAt(i); jaroslav@1646: mv.visitVarInsn(ALOAD, 0); jaroslav@1646: mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc)); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static String typeSig(char t) { jaroslav@1646: return t == 'L' ? JLO_SIG : String.valueOf(t); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // Getter MH generation. jaroslav@1646: // jaroslav@1646: jaroslav@1646: private static MethodHandle makeGetter(Class cbmhClass, String types, int index) { jaroslav@1646: String fieldName = makeFieldName(types, index); jaroslav@1646: Class fieldType = Wrapper.forBasicType(types.charAt(index)).primitiveType(); jaroslav@1646: try { jaroslav@1646: return LOOKUP.findGetter(cbmhClass, fieldName, fieldType); jaroslav@1646: } catch (NoSuchFieldException | IllegalAccessException e) { jaroslav@1646: throw newInternalError(e); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static MethodHandle[] makeGetters(Class cbmhClass, String types, MethodHandle[] mhs) { jaroslav@1646: if (mhs == null) mhs = new MethodHandle[types.length()]; jaroslav@1646: for (int i = 0; i < mhs.length; ++i) { jaroslav@1646: mhs[i] = makeGetter(cbmhClass, types, i); jaroslav@1646: assert(mhs[i].internalMemberName().getDeclaringClass() == cbmhClass); jaroslav@1646: } jaroslav@1646: return mhs; jaroslav@1646: } jaroslav@1646: jaroslav@1646: static MethodHandle[] makeCtors(Class cbmh, String types, MethodHandle mhs[]) { jaroslav@1646: if (mhs == null) mhs = new MethodHandle[1]; jaroslav@1646: mhs[0] = makeCbmhCtor(cbmh, types); jaroslav@1646: return mhs; jaroslav@1646: } jaroslav@1646: jaroslav@1646: // jaroslav@1646: // Auxiliary methods. jaroslav@1646: // jaroslav@1646: jaroslav@1646: static SpeciesData speciesDataFromConcreteBMHClass(Class cbmh) { jaroslav@1646: try { jaroslav@1646: Field F_SPECIES_DATA = cbmh.getDeclaredField("SPECIES_DATA"); jaroslav@1646: return (SpeciesData) F_SPECIES_DATA.get(null); jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Field names in concrete BMHs adhere to this pattern: jaroslav@1646: * arg + type + index jaroslav@1646: * where type is a single character (L, I, J, F, D). jaroslav@1646: */ jaroslav@1646: private static String makeFieldName(String types, int index) { jaroslav@1646: assert index >= 0 && index < types.length(); jaroslav@1646: return "arg" + types.charAt(index) + index; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static String makeSignature(String types, boolean ctor) { jaroslav@1646: StringBuilder buf = new StringBuilder(SIG_INCIPIT); jaroslav@1646: for (char c : types.toCharArray()) { jaroslav@1646: buf.append(typeSig(c)); jaroslav@1646: } jaroslav@1646: return buf.append(')').append(ctor ? "V" : BMH_SIG).toString(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: static MethodHandle makeCbmhCtor(Class cbmh, String types) { jaroslav@1646: try { jaroslav@1646: return linkConstructor(LOOKUP.findConstructor(cbmh, MethodType.fromMethodDescriptorString(makeSignature(types, true), null))); jaroslav@1646: } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | TypeNotPresentException e) { jaroslav@1646: throw newInternalError(e); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Wrap a constructor call in a {@link LambdaForm}. jaroslav@1646: * jaroslav@1646: * If constructors ({@code } methods) are called in LFs, problems might arise if the LFs jaroslav@1646: * are turned into bytecode, because the call to the allocator is routed through an MH, and the jaroslav@1646: * verifier cannot find a {@code NEW} instruction preceding the {@code INVOKESPECIAL} to jaroslav@1646: * {@code }. To avoid this, we add an indirection by invoking {@code } through jaroslav@1646: * {@link MethodHandle#linkToSpecial}. jaroslav@1646: * jaroslav@1646: * The last {@link LambdaForm.Name Name} in the argument's form is expected to be the {@code void} jaroslav@1646: * result of the {@code } invocation. This entry is replaced. jaroslav@1646: */ jaroslav@1646: private static MethodHandle linkConstructor(MethodHandle cmh) { jaroslav@1646: final LambdaForm lf = cmh.form; jaroslav@1646: final int initNameIndex = lf.names.length - 1; jaroslav@1646: final Name initName = lf.names[initNameIndex]; jaroslav@1646: final MemberName ctorMN = initName.function.member; jaroslav@1646: final MethodType ctorMT = ctorMN.getInvocationType(); jaroslav@1646: jaroslav@1646: // obtain function member (call target) jaroslav@1646: // linker method type replaces initial parameter (BMH species) with BMH to avoid naming a species (anonymous class!) jaroslav@1646: final MethodType linkerMT = ctorMT.changeParameterType(0, BoundMethodHandle.class).appendParameterTypes(MemberName.class); jaroslav@1646: MemberName linkerMN = new MemberName(MethodHandle.class, "linkToSpecial", linkerMT, REF_invokeStatic); jaroslav@1646: try { jaroslav@1646: linkerMN = MemberName.getFactory().resolveOrFail(REF_invokeStatic, linkerMN, null, NoSuchMethodException.class); jaroslav@1646: assert(linkerMN.isStatic()); jaroslav@1646: } catch (ReflectiveOperationException ex) { jaroslav@1646: throw newInternalError(ex); jaroslav@1646: } jaroslav@1646: // extend arguments array jaroslav@1646: Object[] newArgs = Arrays.copyOf(initName.arguments, initName.arguments.length + 1); jaroslav@1646: newArgs[newArgs.length - 1] = ctorMN; jaroslav@1646: // replace function jaroslav@1646: final NamedFunction nf = new NamedFunction(linkerMN); jaroslav@1646: final Name linkedCtor = new Name(nf, newArgs); jaroslav@1646: linkedCtor.initIndex(initNameIndex); jaroslav@1646: lf.names[initNameIndex] = linkedCtor; jaroslav@1646: return cmh; jaroslav@1646: } jaroslav@1646: jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static final Lookup LOOKUP = Lookup.IMPL_LOOKUP; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * All subclasses must provide such a value describing their type signature. jaroslav@1646: */ jaroslav@1646: static final SpeciesData SPECIES_DATA = SpeciesData.EMPTY; jaroslav@1646: }