|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package java.lang.invoke; |
|
|
|
import jdk.internal.org.objectweb.asm.ClassWriter; |
|
import jdk.internal.org.objectweb.asm.Label; |
|
import jdk.internal.org.objectweb.asm.MethodVisitor; |
|
import jdk.internal.org.objectweb.asm.Opcodes; |
|
import jdk.internal.org.objectweb.asm.Type; |
|
import sun.invoke.util.VerifyAccess; |
|
import sun.invoke.util.VerifyType; |
|
import sun.invoke.util.Wrapper; |
|
import sun.reflect.misc.ReflectUtil; |
|
|
|
import java.io.File; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.lang.reflect.Modifier; |
|
import java.util.ArrayList; |
|
import java.util.Arrays; |
|
import java.util.HashMap; |
|
import java.util.stream.Stream; |
|
|
|
import static java.lang.invoke.LambdaForm.BasicType; |
|
import static java.lang.invoke.LambdaForm.BasicType.*; |
|
import static java.lang.invoke.LambdaForm.*; |
|
import static java.lang.invoke.MethodHandleNatives.Constants.*; |
|
import static java.lang.invoke.MethodHandleStatics.*; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
class InvokerBytecodeGenerator { |
|
|
|
private static final String MH = "java/lang/invoke/MethodHandle"; |
|
private static final String MHI = "java/lang/invoke/MethodHandleImpl"; |
|
private static final String LF = "java/lang/invoke/LambdaForm"; |
|
private static final String LFN = "java/lang/invoke/LambdaForm$Name"; |
|
private static final String CLS = "java/lang/Class"; |
|
private static final String OBJ = "java/lang/Object"; |
|
private static final String OBJARY = "[Ljava/lang/Object;"; |
|
|
|
private static final String LOOP_CLAUSES = MHI + "$LoopClauses"; |
|
private static final String MHARY2 = "[[L" + MH + ";"; |
|
|
|
private static final String LF_SIG = "L" + LF + ";"; |
|
private static final String LFN_SIG = "L" + LFN + ";"; |
|
private static final String LL_SIG = "(L" + OBJ + ";)L" + OBJ + ";"; |
|
private static final String LLV_SIG = "(L" + OBJ + ";L" + OBJ + ";)V"; |
|
private static final String CLASS_PREFIX = LF + "$"; |
|
private static final String SOURCE_PREFIX = "LambdaForm$"; |
|
|
|
|
|
static final String INVOKER_SUPER_NAME = OBJ; |
|
|
|
|
|
private final String className; |
|
|
|
private final LambdaForm lambdaForm; |
|
private final String invokerName; |
|
private final MethodType invokerType; |
|
|
|
/** Info about local variables in compiled lambda form */ |
|
private int[] localsMap; |
|
private Class<?>[] localClasses; |
|
|
|
|
|
private ClassWriter cw; |
|
private MethodVisitor mv; |
|
|
|
|
|
private Class<?> lastClass; |
|
private String lastInternalName; |
|
|
|
private static final MemberName.Factory MEMBERNAME_FACTORY = MemberName.getFactory(); |
|
private static final Class<?> HOST_CLASS = LambdaForm.class; |
|
|
|
|
|
private InvokerBytecodeGenerator(LambdaForm lambdaForm, int localsMapSize, |
|
String className, String invokerName, MethodType invokerType) { |
|
int p = invokerName.indexOf('.'); |
|
if (p > -1) { |
|
className = invokerName.substring(0, p); |
|
invokerName = invokerName.substring(p + 1); |
|
} |
|
if (DUMP_CLASS_FILES) { |
|
className = makeDumpableClassName(className); |
|
} |
|
this.className = className; |
|
this.lambdaForm = lambdaForm; |
|
this.invokerName = invokerName; |
|
this.invokerType = invokerType; |
|
this.localsMap = new int[localsMapSize+1]; |
|
this.localClasses = new Class<?>[localsMapSize+1]; |
|
} |
|
|
|
|
|
private InvokerBytecodeGenerator(String className, String invokerName, MethodType invokerType) { |
|
this(null, invokerType.parameterCount(), |
|
className, invokerName, invokerType); |
|
|
|
for (int i = 0; i < localsMap.length; i++) { |
|
localsMap[i] = invokerType.parameterSlotCount() - invokerType.parameterSlotDepth(i); |
|
} |
|
} |
|
|
|
|
|
private InvokerBytecodeGenerator(String className, LambdaForm form, MethodType invokerType) { |
|
this(className, form.lambdaName(), form, invokerType); |
|
} |
|
|
|
|
|
InvokerBytecodeGenerator(String className, String invokerName, |
|
LambdaForm form, MethodType invokerType) { |
|
this(form, form.names.length, |
|
className, invokerName, invokerType); |
|
|
|
Name[] names = form.names; |
|
for (int i = 0, index = 0; i < localsMap.length; i++) { |
|
localsMap[i] = index; |
|
if (i < names.length) { |
|
BasicType type = names[i].type(); |
|
index += type.basicTypeSlots(); |
|
} |
|
} |
|
} |
|
|
|
|
|
private static final HashMap<String,Integer> DUMP_CLASS_FILES_COUNTERS; |
|
|
|
private static final File DUMP_CLASS_FILES_DIR; |
|
|
|
static { |
|
if (DUMP_CLASS_FILES) { |
|
DUMP_CLASS_FILES_COUNTERS = new HashMap<>(); |
|
try { |
|
File dumpDir = new File("DUMP_CLASS_FILES"); |
|
if (!dumpDir.exists()) { |
|
dumpDir.mkdirs(); |
|
} |
|
DUMP_CLASS_FILES_DIR = dumpDir; |
|
System.out.println("Dumping class files to "+DUMP_CLASS_FILES_DIR+"/..."); |
|
} catch (Exception e) { |
|
throw newInternalError(e); |
|
} |
|
} else { |
|
DUMP_CLASS_FILES_COUNTERS = null; |
|
DUMP_CLASS_FILES_DIR = null; |
|
} |
|
} |
|
|
|
private void maybeDump(final byte[] classFile) { |
|
if (DUMP_CLASS_FILES) { |
|
maybeDump(CLASS_PREFIX + className, classFile); |
|
} |
|
} |
|
|
|
|
|
static void maybeDump(final String className, final byte[] classFile) { |
|
if (DUMP_CLASS_FILES) { |
|
java.security.AccessController.doPrivileged( |
|
new java.security.PrivilegedAction<>() { |
|
public Void run() { |
|
try { |
|
String dumpName = className.replace('.','/'); |
|
File dumpFile = new File(DUMP_CLASS_FILES_DIR, dumpName+".class"); |
|
System.out.println("dump: " + dumpFile); |
|
dumpFile.getParentFile().mkdirs(); |
|
FileOutputStream file = new FileOutputStream(dumpFile); |
|
file.write(classFile); |
|
file.close(); |
|
return null; |
|
} catch (IOException ex) { |
|
throw newInternalError(ex); |
|
} |
|
} |
|
}); |
|
} |
|
} |
|
|
|
private static String makeDumpableClassName(String className) { |
|
Integer ctr; |
|
synchronized (DUMP_CLASS_FILES_COUNTERS) { |
|
ctr = DUMP_CLASS_FILES_COUNTERS.get(className); |
|
if (ctr == null) ctr = 0; |
|
DUMP_CLASS_FILES_COUNTERS.put(className, ctr+1); |
|
} |
|
String sfx = ctr.toString(); |
|
while (sfx.length() < 3) |
|
sfx = "0"+sfx; |
|
className += sfx; |
|
return className; |
|
} |
|
|
|
class CpPatch { |
|
final int index; |
|
final Object value; |
|
CpPatch(int index, Object value) { |
|
this.index = index; |
|
this.value = value; |
|
} |
|
public String toString() { |
|
return "CpPatch/index="+index+",value="+value; |
|
} |
|
} |
|
|
|
private final ArrayList<CpPatch> cpPatches = new ArrayList<>(); |
|
|
|
private int cph = 0; |
|
|
|
String constantPlaceholder(Object arg) { |
|
String cpPlaceholder = "CONSTANT_PLACEHOLDER_" + cph++; |
|
if (DUMP_CLASS_FILES) cpPlaceholder += " <<" + debugString(arg) + ">>"; |
|
// TODO check if arg is already in the constant pool |
|
|
|
int index = cw.newConst((Object) cpPlaceholder); |
|
cpPatches.add(new CpPatch(index, arg)); |
|
return cpPlaceholder; |
|
} |
|
|
|
Object[] cpPatches(byte[] classFile) { |
|
int size = getConstantPoolSize(classFile); |
|
Object[] res = new Object[size]; |
|
for (CpPatch p : cpPatches) { |
|
if (p.index >= size) |
|
throw new InternalError("in cpool["+size+"]: "+p+"\n"+Arrays.toString(Arrays.copyOf(classFile, 20))); |
|
res[p.index] = p.value; |
|
} |
|
return res; |
|
} |
|
|
|
private static String debugString(Object arg) { |
|
if (arg instanceof MethodHandle) { |
|
MethodHandle mh = (MethodHandle) arg; |
|
MemberName member = mh.internalMemberName(); |
|
if (member != null) |
|
return member.toString(); |
|
return mh.debugString(); |
|
} |
|
return arg.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static int getConstantPoolSize(byte[] classFile) { |
|
// The first few bytes: |
|
// u4 magic; |
|
// u2 minor_version; |
|
// u2 major_version; |
|
|
|
return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private MemberName loadMethod(byte[] classFile) { |
|
Class<?> invokerClass = loadAndInitializeInvokerClass(classFile, cpPatches(classFile)); |
|
return resolveInvokerMember(invokerClass, invokerName, invokerType); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private static Class<?> loadAndInitializeInvokerClass(byte[] classBytes, Object[] patches) { |
|
Class<?> invokerClass = UNSAFE.defineAnonymousClass(HOST_CLASS, classBytes, patches); |
|
UNSAFE.ensureClassInitialized(invokerClass); |
|
return invokerClass; |
|
} |
|
|
|
private static MemberName resolveInvokerMember(Class<?> invokerClass, String name, MethodType type) { |
|
MemberName member = new MemberName(invokerClass, name, type, REF_invokeStatic); |
|
try { |
|
member = MEMBERNAME_FACTORY.resolveOrFail(REF_invokeStatic, member, HOST_CLASS, ReflectiveOperationException.class); |
|
} catch (ReflectiveOperationException e) { |
|
throw newInternalError(e); |
|
} |
|
return member; |
|
} |
|
|
|
|
|
|
|
*/ |
|
private ClassWriter classFilePrologue() { |
|
final int NOT_ACC_PUBLIC = 0; |
|
cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); |
|
cw.visit(Opcodes.V1_8, NOT_ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, |
|
CLASS_PREFIX + className, null, INVOKER_SUPER_NAME, null); |
|
cw.visitSource(SOURCE_PREFIX + className, null); |
|
return cw; |
|
} |
|
|
|
private void methodPrologue() { |
|
String invokerDesc = invokerType.toMethodDescriptorString(); |
|
mv = cw.visitMethod(Opcodes.ACC_STATIC, invokerName, invokerDesc, null, null); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void methodEpilogue() { |
|
mv.visitMaxs(0, 0); |
|
mv.visitEnd(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitConst(Object con) { |
|
if (con == null) { |
|
mv.visitInsn(Opcodes.ACONST_NULL); |
|
return; |
|
} |
|
if (con instanceof Integer) { |
|
emitIconstInsn((int) con); |
|
return; |
|
} |
|
if (con instanceof Byte) { |
|
emitIconstInsn((byte)con); |
|
return; |
|
} |
|
if (con instanceof Short) { |
|
emitIconstInsn((short)con); |
|
return; |
|
} |
|
if (con instanceof Character) { |
|
emitIconstInsn((char)con); |
|
return; |
|
} |
|
if (con instanceof Long) { |
|
long x = (long) con; |
|
short sx = (short)x; |
|
if (x == sx) { |
|
if (sx >= 0 && sx <= 1) { |
|
mv.visitInsn(Opcodes.LCONST_0 + (int) sx); |
|
} else { |
|
emitIconstInsn((int) x); |
|
mv.visitInsn(Opcodes.I2L); |
|
} |
|
return; |
|
} |
|
} |
|
if (con instanceof Float) { |
|
float x = (float) con; |
|
short sx = (short)x; |
|
if (x == sx) { |
|
if (sx >= 0 && sx <= 2) { |
|
mv.visitInsn(Opcodes.FCONST_0 + (int) sx); |
|
} else { |
|
emitIconstInsn((int) x); |
|
mv.visitInsn(Opcodes.I2F); |
|
} |
|
return; |
|
} |
|
} |
|
if (con instanceof Double) { |
|
double x = (double) con; |
|
short sx = (short)x; |
|
if (x == sx) { |
|
if (sx >= 0 && sx <= 1) { |
|
mv.visitInsn(Opcodes.DCONST_0 + (int) sx); |
|
} else { |
|
emitIconstInsn((int) x); |
|
mv.visitInsn(Opcodes.I2D); |
|
} |
|
return; |
|
} |
|
} |
|
if (con instanceof Boolean) { |
|
emitIconstInsn((boolean) con ? 1 : 0); |
|
return; |
|
} |
|
|
|
mv.visitLdcInsn(con); |
|
} |
|
|
|
private void emitIconstInsn(final int cst) { |
|
if (cst >= -1 && cst <= 5) { |
|
mv.visitInsn(Opcodes.ICONST_0 + cst); |
|
} else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) { |
|
mv.visitIntInsn(Opcodes.BIPUSH, cst); |
|
} else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) { |
|
mv.visitIntInsn(Opcodes.SIPUSH, cst); |
|
} else { |
|
mv.visitLdcInsn(cst); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitLoadInsn(BasicType type, int index) { |
|
int opcode = loadInsnOpcode(type); |
|
mv.visitVarInsn(opcode, localsMap[index]); |
|
} |
|
|
|
private int loadInsnOpcode(BasicType type) throws InternalError { |
|
switch (type) { |
|
case I_TYPE: return Opcodes.ILOAD; |
|
case J_TYPE: return Opcodes.LLOAD; |
|
case F_TYPE: return Opcodes.FLOAD; |
|
case D_TYPE: return Opcodes.DLOAD; |
|
case L_TYPE: return Opcodes.ALOAD; |
|
default: |
|
throw new InternalError("unknown type: " + type); |
|
} |
|
} |
|
private void emitAloadInsn(int index) { |
|
emitLoadInsn(L_TYPE, index); |
|
} |
|
|
|
private void emitStoreInsn(BasicType type, int index) { |
|
int opcode = storeInsnOpcode(type); |
|
mv.visitVarInsn(opcode, localsMap[index]); |
|
} |
|
|
|
private int storeInsnOpcode(BasicType type) throws InternalError { |
|
switch (type) { |
|
case I_TYPE: return Opcodes.ISTORE; |
|
case J_TYPE: return Opcodes.LSTORE; |
|
case F_TYPE: return Opcodes.FSTORE; |
|
case D_TYPE: return Opcodes.DSTORE; |
|
case L_TYPE: return Opcodes.ASTORE; |
|
default: |
|
throw new InternalError("unknown type: " + type); |
|
} |
|
} |
|
private void emitAstoreInsn(int index) { |
|
emitStoreInsn(L_TYPE, index); |
|
} |
|
|
|
private byte arrayTypeCode(Wrapper elementType) { |
|
switch (elementType) { |
|
case BOOLEAN: return Opcodes.T_BOOLEAN; |
|
case BYTE: return Opcodes.T_BYTE; |
|
case CHAR: return Opcodes.T_CHAR; |
|
case SHORT: return Opcodes.T_SHORT; |
|
case INT: return Opcodes.T_INT; |
|
case LONG: return Opcodes.T_LONG; |
|
case FLOAT: return Opcodes.T_FLOAT; |
|
case DOUBLE: return Opcodes.T_DOUBLE; |
|
case OBJECT: return 0; |
|
default: throw new InternalError(); |
|
} |
|
} |
|
|
|
private int arrayInsnOpcode(byte tcode, int aaop) throws InternalError { |
|
assert(aaop == Opcodes.AASTORE || aaop == Opcodes.AALOAD); |
|
int xas; |
|
switch (tcode) { |
|
case Opcodes.T_BOOLEAN: xas = Opcodes.BASTORE; break; |
|
case Opcodes.T_BYTE: xas = Opcodes.BASTORE; break; |
|
case Opcodes.T_CHAR: xas = Opcodes.CASTORE; break; |
|
case Opcodes.T_SHORT: xas = Opcodes.SASTORE; break; |
|
case Opcodes.T_INT: xas = Opcodes.IASTORE; break; |
|
case Opcodes.T_LONG: xas = Opcodes.LASTORE; break; |
|
case Opcodes.T_FLOAT: xas = Opcodes.FASTORE; break; |
|
case Opcodes.T_DOUBLE: xas = Opcodes.DASTORE; break; |
|
case 0: xas = Opcodes.AASTORE; break; |
|
default: throw new InternalError(); |
|
} |
|
return xas - Opcodes.AASTORE + aaop; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void emitBoxing(Wrapper wrapper) { |
|
String owner = "java/lang/" + wrapper.wrapperType().getSimpleName(); |
|
String name = "valueOf"; |
|
String desc = "(" + wrapper.basicTypeChar() + ")L" + owner + ";"; |
|
mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, desc, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void emitUnboxing(Wrapper wrapper) { |
|
String owner = "java/lang/" + wrapper.wrapperType().getSimpleName(); |
|
String name = wrapper.primitiveSimpleName() + "Value"; |
|
String desc = "()" + wrapper.basicTypeChar(); |
|
emitReferenceCast(wrapper.wrapperType(), null); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void emitImplicitConversion(BasicType ptype, Class<?> pclass, Object arg) { |
|
assert(basicType(pclass) == ptype); |
|
if (pclass == ptype.basicTypeClass() && ptype != L_TYPE) |
|
return; |
|
switch (ptype) { |
|
case L_TYPE: |
|
if (VerifyType.isNullConversion(Object.class, pclass, false)) { |
|
if (PROFILE_LEVEL > 0) |
|
emitReferenceCast(Object.class, arg); |
|
return; |
|
} |
|
emitReferenceCast(pclass, arg); |
|
return; |
|
case I_TYPE: |
|
if (!VerifyType.isNullConversion(int.class, pclass, false)) |
|
emitPrimCast(ptype.basicTypeWrapper(), Wrapper.forPrimitiveType(pclass)); |
|
return; |
|
} |
|
throw newInternalError("bad implicit conversion: tc="+ptype+": "+pclass); |
|
} |
|
|
|
|
|
private boolean assertStaticType(Class<?> cls, Name n) { |
|
int local = n.index(); |
|
Class<?> aclass = localClasses[local]; |
|
if (aclass != null && (aclass == cls || cls.isAssignableFrom(aclass))) { |
|
return true; |
|
} else if (aclass == null || aclass.isAssignableFrom(cls)) { |
|
localClasses[local] = cls; |
|
} |
|
return false; |
|
} |
|
|
|
private void emitReferenceCast(Class<?> cls, Object arg) { |
|
Name writeBack = null; |
|
if (arg instanceof Name) { |
|
Name n = (Name) arg; |
|
if (lambdaForm.useCount(n) > 1) { |
|
|
|
writeBack = n; |
|
if (assertStaticType(cls, n)) { |
|
return; |
|
} |
|
} |
|
} |
|
if (isStaticallyNameable(cls)) { |
|
String sig = getInternalName(cls); |
|
mv.visitTypeInsn(Opcodes.CHECKCAST, sig); |
|
} else { |
|
mv.visitLdcInsn(constantPlaceholder(cls)); |
|
mv.visitTypeInsn(Opcodes.CHECKCAST, CLS); |
|
mv.visitInsn(Opcodes.SWAP); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CLS, "cast", LL_SIG, false); |
|
if (Object[].class.isAssignableFrom(cls)) |
|
mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY); |
|
else if (PROFILE_LEVEL > 0) |
|
mv.visitTypeInsn(Opcodes.CHECKCAST, OBJ); |
|
} |
|
if (writeBack != null) { |
|
mv.visitInsn(Opcodes.DUP); |
|
emitAstoreInsn(writeBack.index()); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitReturnInsn(BasicType type) { |
|
int opcode; |
|
switch (type) { |
|
case I_TYPE: opcode = Opcodes.IRETURN; break; |
|
case J_TYPE: opcode = Opcodes.LRETURN; break; |
|
case F_TYPE: opcode = Opcodes.FRETURN; break; |
|
case D_TYPE: opcode = Opcodes.DRETURN; break; |
|
case L_TYPE: opcode = Opcodes.ARETURN; break; |
|
case V_TYPE: opcode = Opcodes.RETURN; break; |
|
default: |
|
throw new InternalError("unknown return type: " + type); |
|
} |
|
mv.visitInsn(opcode); |
|
} |
|
|
|
private String getInternalName(Class<?> c) { |
|
if (c == Object.class) return OBJ; |
|
else if (c == Object[].class) return OBJARY; |
|
else if (c == Class.class) return CLS; |
|
else if (c == MethodHandle.class) return MH; |
|
assert(VerifyAccess.isTypeVisible(c, Object.class)) : c.getName(); |
|
|
|
if (c == lastClass) { |
|
return lastInternalName; |
|
} |
|
lastClass = c; |
|
return lastInternalName = c.getName().replace('.', '/'); |
|
} |
|
|
|
private static MemberName resolveFrom(String name, MethodType type, Class<?> holder) { |
|
MemberName member = new MemberName(holder, name, type, REF_invokeStatic); |
|
MemberName resolvedMember = MemberName.getFactory().resolveOrNull(REF_invokeStatic, member, holder); |
|
if (TRACE_RESOLVE) { |
|
System.out.println("[LF_RESOLVE] " + holder.getName() + " " + name + " " + |
|
shortenSignature(basicTypeSignature(type)) + (resolvedMember != null ? " (success)" : " (fail)") ); |
|
} |
|
return resolvedMember; |
|
} |
|
|
|
private static MemberName lookupPregenerated(LambdaForm form, MethodType invokerType) { |
|
if (form.customized != null) { |
|
|
|
return null; |
|
} |
|
String name = form.kind.methodName; |
|
switch (form.kind) { |
|
case BOUND_REINVOKER: { |
|
name = name + "_" + BoundMethodHandle.speciesDataFor(form).key(); |
|
return resolveFrom(name, invokerType, DelegatingMethodHandle.Holder.class); |
|
} |
|
case DELEGATE: return resolveFrom(name, invokerType, DelegatingMethodHandle.Holder.class); |
|
case ZERO: |
|
case IDENTITY: { |
|
name = name + "_" + form.returnType().basicTypeChar(); |
|
return resolveFrom(name, invokerType, LambdaForm.Holder.class); |
|
} |
|
case EXACT_INVOKER: |
|
case EXACT_LINKER: |
|
case LINK_TO_CALL_SITE: |
|
case LINK_TO_TARGET_METHOD: |
|
case GENERIC_INVOKER: |
|
case GENERIC_LINKER: return resolveFrom(name, invokerType.basicType(), Invokers.Holder.class); |
|
case GET_OBJECT: |
|
case GET_BOOLEAN: |
|
case GET_BYTE: |
|
case GET_CHAR: |
|
case GET_SHORT: |
|
case GET_INT: |
|
case GET_LONG: |
|
case GET_FLOAT: |
|
case GET_DOUBLE: |
|
case PUT_OBJECT: |
|
case PUT_BOOLEAN: |
|
case PUT_BYTE: |
|
case PUT_CHAR: |
|
case PUT_SHORT: |
|
case PUT_INT: |
|
case PUT_LONG: |
|
case PUT_FLOAT: |
|
case PUT_DOUBLE: |
|
case DIRECT_NEW_INVOKE_SPECIAL: |
|
case DIRECT_INVOKE_INTERFACE: |
|
case DIRECT_INVOKE_SPECIAL: |
|
case DIRECT_INVOKE_SPECIAL_IFC: |
|
case DIRECT_INVOKE_STATIC: |
|
case DIRECT_INVOKE_STATIC_INIT: |
|
case DIRECT_INVOKE_VIRTUAL: return resolveFrom(name, invokerType, DirectMethodHandle.Holder.class); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static MemberName generateCustomizedCode(LambdaForm form, MethodType invokerType) { |
|
MemberName pregenerated = lookupPregenerated(form, invokerType); |
|
if (pregenerated != null) return pregenerated; |
|
|
|
InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("MH", form, invokerType); |
|
return g.loadMethod(g.generateCustomizedCodeBytes()); |
|
} |
|
|
|
|
|
private boolean checkActualReceiver() { |
|
|
|
mv.visitInsn(Opcodes.DUP); |
|
mv.visitVarInsn(Opcodes.ALOAD, localsMap[0]); |
|
mv.visitMethodInsn(Opcodes.INVOKESTATIC, MHI, "assertSame", LLV_SIG, false); |
|
return true; |
|
} |
|
|
|
static String className(String cn) { |
|
assert checkClassName(cn): "Class not found: " + cn; |
|
return cn; |
|
} |
|
|
|
static boolean checkClassName(String cn) { |
|
Type tp = Type.getType(cn); |
|
|
|
if (tp.getSort() != Type.OBJECT) { |
|
return false; |
|
} |
|
try { |
|
Class<?> c = Class.forName(tp.getClassName(), false, null); |
|
return true; |
|
} catch (ClassNotFoundException e) { |
|
return false; |
|
} |
|
} |
|
|
|
static final String LF_HIDDEN_SIG = className("Ljava/lang/invoke/LambdaForm$Hidden;"); |
|
static final String LF_COMPILED_SIG = className("Ljava/lang/invoke/LambdaForm$Compiled;"); |
|
static final String FORCEINLINE_SIG = className("Ljdk/internal/vm/annotation/ForceInline;"); |
|
static final String DONTINLINE_SIG = className("Ljdk/internal/vm/annotation/DontInline;"); |
|
static final String INJECTEDPROFILE_SIG = className("Ljava/lang/invoke/InjectedProfile;"); |
|
|
|
|
|
|
|
*/ |
|
private byte[] generateCustomizedCodeBytes() { |
|
classFilePrologue(); |
|
addMethod(); |
|
bogusMethod(lambdaForm); |
|
|
|
final byte[] classFile = toByteArray(); |
|
maybeDump(classFile); |
|
return classFile; |
|
} |
|
|
|
void setClassWriter(ClassWriter cw) { |
|
this.cw = cw; |
|
} |
|
|
|
void addMethod() { |
|
methodPrologue(); |
|
|
|
|
|
mv.visitAnnotation(LF_HIDDEN_SIG, true); |
|
|
|
|
|
mv.visitAnnotation(LF_COMPILED_SIG, true); |
|
|
|
if (lambdaForm.forceInline) { |
|
|
|
mv.visitAnnotation(FORCEINLINE_SIG, true); |
|
} else { |
|
mv.visitAnnotation(DONTINLINE_SIG, true); |
|
} |
|
|
|
constantPlaceholder(lambdaForm); |
|
|
|
if (lambdaForm.customized != null) { |
|
// Since LambdaForm is customized for a particular MethodHandle, it's safe to substitute |
|
// receiver MethodHandle (at slot #0) with an embedded constant and use it instead. |
|
// It enables more efficient code generation in some situations, since embedded constants |
|
|
|
mv.visitLdcInsn(constantPlaceholder(lambdaForm.customized)); |
|
mv.visitTypeInsn(Opcodes.CHECKCAST, MH); |
|
assert(checkActualReceiver()); |
|
mv.visitVarInsn(Opcodes.ASTORE, localsMap[0]); |
|
} |
|
|
|
// iterate over the form's names, generating bytecode instructions for each |
|
|
|
Name onStack = null; |
|
for (int i = lambdaForm.arity; i < lambdaForm.names.length; i++) { |
|
Name name = lambdaForm.names[i]; |
|
|
|
emitStoreResult(onStack); |
|
onStack = name; |
|
MethodHandleImpl.Intrinsic intr = name.function.intrinsicName(); |
|
switch (intr) { |
|
case SELECT_ALTERNATIVE: |
|
assert lambdaForm.isSelectAlternative(i); |
|
if (PROFILE_GWT) { |
|
assert(name.arguments[0] instanceof Name && |
|
((Name)name.arguments[0]).refersTo(MethodHandleImpl.class, "profileBoolean")); |
|
mv.visitAnnotation(INJECTEDPROFILE_SIG, true); |
|
} |
|
onStack = emitSelectAlternative(name, lambdaForm.names[i+1]); |
|
i++; |
|
continue; |
|
case GUARD_WITH_CATCH: |
|
assert lambdaForm.isGuardWithCatch(i); |
|
onStack = emitGuardWithCatch(i); |
|
i += 2; |
|
continue; |
|
case TRY_FINALLY: |
|
assert lambdaForm.isTryFinally(i); |
|
onStack = emitTryFinally(i); |
|
i += 2; |
|
continue; |
|
case LOOP: |
|
assert lambdaForm.isLoop(i); |
|
onStack = emitLoop(i); |
|
i += 2; |
|
continue; |
|
case NEW_ARRAY: |
|
Class<?> rtype = name.function.methodType().returnType(); |
|
if (isStaticallyNameable(rtype)) { |
|
emitNewArray(name); |
|
continue; |
|
} |
|
break; |
|
case ARRAY_LOAD: |
|
emitArrayLoad(name); |
|
continue; |
|
case ARRAY_STORE: |
|
emitArrayStore(name); |
|
continue; |
|
case ARRAY_LENGTH: |
|
emitArrayLength(name); |
|
continue; |
|
case IDENTITY: |
|
assert(name.arguments.length == 1); |
|
emitPushArguments(name, 0); |
|
continue; |
|
case ZERO: |
|
assert(name.arguments.length == 0); |
|
emitConst(name.type.basicTypeWrapper().zero()); |
|
continue; |
|
case NONE: |
|
|
|
break; |
|
default: |
|
throw newInternalError("Unknown intrinsic: "+intr); |
|
} |
|
|
|
MemberName member = name.function.member(); |
|
if (isStaticallyInvocable(member)) { |
|
emitStaticInvoke(member, name); |
|
} else { |
|
emitInvoke(name); |
|
} |
|
} |
|
|
|
|
|
emitReturn(onStack); |
|
|
|
methodEpilogue(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private byte[] toByteArray() { |
|
try { |
|
return cw.toByteArray(); |
|
} catch (RuntimeException e) { |
|
throw new BytecodeGenerationException(e); |
|
} |
|
} |
|
|
|
@SuppressWarnings("serial") |
|
static final class BytecodeGenerationException extends RuntimeException { |
|
BytecodeGenerationException(Exception cause) { |
|
super(cause); |
|
} |
|
} |
|
|
|
void emitArrayLoad(Name name) { emitArrayOp(name, Opcodes.AALOAD); } |
|
void emitArrayStore(Name name) { emitArrayOp(name, Opcodes.AASTORE); } |
|
void emitArrayLength(Name name) { emitArrayOp(name, Opcodes.ARRAYLENGTH); } |
|
|
|
void emitArrayOp(Name name, int arrayOpcode) { |
|
assert arrayOpcode == Opcodes.AALOAD || arrayOpcode == Opcodes.AASTORE || arrayOpcode == Opcodes.ARRAYLENGTH; |
|
Class<?> elementType = name.function.methodType().parameterType(0).getComponentType(); |
|
assert elementType != null; |
|
emitPushArguments(name, 0); |
|
if (arrayOpcode != Opcodes.ARRAYLENGTH && elementType.isPrimitive()) { |
|
Wrapper w = Wrapper.forPrimitiveType(elementType); |
|
arrayOpcode = arrayInsnOpcode(arrayTypeCode(w), arrayOpcode); |
|
} |
|
mv.visitInsn(arrayOpcode); |
|
} |
|
|
|
|
|
|
|
*/ |
|
void emitInvoke(Name name) { |
|
assert(!name.isLinkerMethodInvoke()); |
|
if (true) { |
|
|
|
MethodHandle target = name.function.resolvedHandle(); |
|
assert(target != null) : name.exprString(); |
|
mv.visitLdcInsn(constantPlaceholder(target)); |
|
emitReferenceCast(MethodHandle.class, target); |
|
} else { |
|
|
|
emitAloadInsn(0); |
|
emitReferenceCast(MethodHandle.class, null); |
|
mv.visitFieldInsn(Opcodes.GETFIELD, MH, "form", LF_SIG); |
|
mv.visitFieldInsn(Opcodes.GETFIELD, LF, "names", LFN_SIG); |
|
// TODO more to come |
|
} |
|
|
|
|
|
emitPushArguments(name, 0); |
|
|
|
|
|
MethodType type = name.function.methodType(); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false); |
|
} |
|
|
|
private static Class<?>[] STATICALLY_INVOCABLE_PACKAGES = { |
|
|
|
java.lang.Object.class, |
|
java.util.Arrays.class, |
|
jdk.internal.misc.Unsafe.class |
|
//MethodHandle.class already covered |
|
}; |
|
|
|
static boolean isStaticallyInvocable(NamedFunction ... functions) { |
|
for (NamedFunction nf : functions) { |
|
if (!isStaticallyInvocable(nf.member())) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
static boolean isStaticallyInvocable(Name name) { |
|
return isStaticallyInvocable(name.function.member()); |
|
} |
|
|
|
static boolean isStaticallyInvocable(MemberName member) { |
|
if (member == null) return false; |
|
if (member.isConstructor()) return false; |
|
Class<?> cls = member.getDeclaringClass(); |
|
// Fast-path non-private members declared by MethodHandles, which is a common |
|
|
|
if (MethodHandle.class.isAssignableFrom(cls) && !member.isPrivate()) { |
|
assert(isStaticallyInvocableType(member.getMethodOrFieldType())); |
|
return true; |
|
} |
|
if (cls.isArray() || cls.isPrimitive()) |
|
return false; |
|
if (cls.isAnonymousClass() || cls.isLocalClass()) |
|
return false; |
|
if (cls.getClassLoader() != MethodHandle.class.getClassLoader()) |
|
return false; |
|
if (ReflectUtil.isVMAnonymousClass(cls)) |
|
return false; |
|
if (!isStaticallyInvocableType(member.getMethodOrFieldType())) |
|
return false; |
|
if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls)) |
|
return true; |
|
if (member.isPublic() && isStaticallyNameable(cls)) |
|
return true; |
|
return false; |
|
} |
|
|
|
private static boolean isStaticallyInvocableType(MethodType mtype) { |
|
if (!isStaticallyNameable(mtype.returnType())) |
|
return false; |
|
for (Class<?> ptype : mtype.parameterArray()) |
|
if (!isStaticallyNameable(ptype)) |
|
return false; |
|
return true; |
|
} |
|
|
|
static boolean isStaticallyNameable(Class<?> cls) { |
|
if (cls == Object.class) |
|
return true; |
|
if (MethodHandle.class.isAssignableFrom(cls)) { |
|
assert(!ReflectUtil.isVMAnonymousClass(cls)); |
|
return true; |
|
} |
|
while (cls.isArray()) |
|
cls = cls.getComponentType(); |
|
if (cls.isPrimitive()) |
|
return true; |
|
if (ReflectUtil.isVMAnonymousClass(cls)) |
|
return false; |
|
|
|
if (cls.getClassLoader() != Object.class.getClassLoader()) |
|
return false; |
|
if (VerifyAccess.isSamePackage(MethodHandle.class, cls)) |
|
return true; |
|
if (!Modifier.isPublic(cls.getModifiers())) |
|
return false; |
|
for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) { |
|
if (VerifyAccess.isSamePackage(pkgcls, cls)) |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
void emitStaticInvoke(Name name) { |
|
emitStaticInvoke(name.function.member(), name); |
|
} |
|
|
|
|
|
|
|
*/ |
|
void emitStaticInvoke(MemberName member, Name name) { |
|
assert(member.equals(name.function.member())); |
|
Class<?> defc = member.getDeclaringClass(); |
|
String cname = getInternalName(defc); |
|
String mname = member.getName(); |
|
String mtype; |
|
byte refKind = member.getReferenceKind(); |
|
if (refKind == REF_invokeSpecial) { |
|
|
|
assert(member.canBeStaticallyBound()) : member; |
|
refKind = REF_invokeVirtual; |
|
} |
|
|
|
assert(!(member.getDeclaringClass().isInterface() && refKind == REF_invokeVirtual)); |
|
|
|
|
|
emitPushArguments(name, 0); |
|
|
|
|
|
if (member.isMethod()) { |
|
mtype = member.getMethodType().toMethodDescriptorString(); |
|
mv.visitMethodInsn(refKindOpcode(refKind), cname, mname, mtype, |
|
member.getDeclaringClass().isInterface()); |
|
} else { |
|
mtype = MethodType.toFieldDescriptorString(member.getFieldType()); |
|
mv.visitFieldInsn(refKindOpcode(refKind), cname, mname, mtype); |
|
} |
|
|
|
if (name.type == L_TYPE) { |
|
Class<?> rtype = member.getInvocationType().returnType(); |
|
assert(!rtype.isPrimitive()); |
|
if (rtype != Object.class && !rtype.isInterface()) { |
|
assertStaticType(rtype, name); |
|
} |
|
} |
|
} |
|
|
|
void emitNewArray(Name name) throws InternalError { |
|
Class<?> rtype = name.function.methodType().returnType(); |
|
if (name.arguments.length == 0) { |
|
|
|
Object emptyArray; |
|
try { |
|
emptyArray = name.function.resolvedHandle().invoke(); |
|
} catch (Throwable ex) { |
|
throw uncaughtException(ex); |
|
} |
|
assert(java.lang.reflect.Array.getLength(emptyArray) == 0); |
|
assert(emptyArray.getClass() == rtype); |
|
mv.visitLdcInsn(constantPlaceholder(emptyArray)); |
|
emitReferenceCast(rtype, emptyArray); |
|
return; |
|
} |
|
Class<?> arrayElementType = rtype.getComponentType(); |
|
assert(arrayElementType != null); |
|
emitIconstInsn(name.arguments.length); |
|
int xas = Opcodes.AASTORE; |
|
if (!arrayElementType.isPrimitive()) { |
|
mv.visitTypeInsn(Opcodes.ANEWARRAY, getInternalName(arrayElementType)); |
|
} else { |
|
byte tc = arrayTypeCode(Wrapper.forPrimitiveType(arrayElementType)); |
|
xas = arrayInsnOpcode(tc, xas); |
|
mv.visitIntInsn(Opcodes.NEWARRAY, tc); |
|
} |
|
|
|
for (int i = 0; i < name.arguments.length; i++) { |
|
mv.visitInsn(Opcodes.DUP); |
|
emitIconstInsn(i); |
|
emitPushArgument(name, i); |
|
mv.visitInsn(xas); |
|
} |
|
|
|
assertStaticType(rtype, name); |
|
} |
|
int refKindOpcode(byte refKind) { |
|
switch (refKind) { |
|
case REF_invokeVirtual: return Opcodes.INVOKEVIRTUAL; |
|
case REF_invokeStatic: return Opcodes.INVOKESTATIC; |
|
case REF_invokeSpecial: return Opcodes.INVOKESPECIAL; |
|
case REF_invokeInterface: return Opcodes.INVOKEINTERFACE; |
|
case REF_getField: return Opcodes.GETFIELD; |
|
case REF_putField: return Opcodes.PUTFIELD; |
|
case REF_getStatic: return Opcodes.GETSTATIC; |
|
case REF_putStatic: return Opcodes.PUTSTATIC; |
|
} |
|
throw new InternalError("refKind="+refKind); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Name emitSelectAlternative(Name selectAlternativeName, Name invokeBasicName) { |
|
assert isStaticallyInvocable(invokeBasicName); |
|
|
|
Name receiver = (Name) invokeBasicName.arguments[0]; |
|
|
|
Label L_fallback = new Label(); |
|
Label L_done = new Label(); |
|
|
|
|
|
emitPushArgument(selectAlternativeName, 0); |
|
|
|
|
|
mv.visitJumpInsn(Opcodes.IFEQ, L_fallback); |
|
|
|
|
|
Class<?>[] preForkClasses = localClasses.clone(); |
|
emitPushArgument(selectAlternativeName, 1); |
|
emitAstoreInsn(receiver.index()); |
|
emitStaticInvoke(invokeBasicName); |
|
|
|
|
|
mv.visitJumpInsn(Opcodes.GOTO, L_done); |
|
|
|
|
|
mv.visitLabel(L_fallback); |
|
|
|
|
|
System.arraycopy(preForkClasses, 0, localClasses, 0, preForkClasses.length); |
|
emitPushArgument(selectAlternativeName, 2); |
|
emitAstoreInsn(receiver.index()); |
|
emitStaticInvoke(invokeBasicName); |
|
|
|
|
|
mv.visitLabel(L_done); |
|
|
|
System.arraycopy(preForkClasses, 0, localClasses, 0, preForkClasses.length); |
|
|
|
return invokeBasicName; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Name emitGuardWithCatch(int pos) { |
|
Name args = lambdaForm.names[pos]; |
|
Name invoker = lambdaForm.names[pos+1]; |
|
Name result = lambdaForm.names[pos+2]; |
|
|
|
Label L_startBlock = new Label(); |
|
Label L_endBlock = new Label(); |
|
Label L_handler = new Label(); |
|
Label L_done = new Label(); |
|
|
|
Class<?> returnType = result.function.resolvedHandle().type().returnType(); |
|
MethodType type = args.function.resolvedHandle().type() |
|
.dropParameterTypes(0,1) |
|
.changeReturnType(returnType); |
|
|
|
mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_handler, "java/lang/Throwable"); |
|
|
|
|
|
mv.visitLabel(L_startBlock); |
|
|
|
emitPushArgument(invoker, 0); |
|
emitPushArguments(args, 1); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false); |
|
mv.visitLabel(L_endBlock); |
|
mv.visitJumpInsn(Opcodes.GOTO, L_done); |
|
|
|
|
|
mv.visitLabel(L_handler); |
|
|
|
|
|
mv.visitInsn(Opcodes.DUP); |
|
|
|
emitPushArgument(invoker, 1); |
|
mv.visitInsn(Opcodes.SWAP); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "isInstance", "(Ljava/lang/Object;)Z", false); |
|
Label L_rethrow = new Label(); |
|
mv.visitJumpInsn(Opcodes.IFEQ, L_rethrow); |
|
|
|
// Invoke catcher |
|
|
|
emitPushArgument(invoker, 2); |
|
mv.visitInsn(Opcodes.SWAP); |
|
emitPushArguments(args, 1); |
|
MethodType catcherType = type.insertParameterTypes(0, Throwable.class); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", catcherType.basicType().toMethodDescriptorString(), false); |
|
mv.visitJumpInsn(Opcodes.GOTO, L_done); |
|
|
|
mv.visitLabel(L_rethrow); |
|
mv.visitInsn(Opcodes.ATHROW); |
|
|
|
mv.visitLabel(L_done); |
|
|
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Name emitTryFinally(int pos) { |
|
Name args = lambdaForm.names[pos]; |
|
Name invoker = lambdaForm.names[pos+1]; |
|
Name result = lambdaForm.names[pos+2]; |
|
|
|
Label lFrom = new Label(); |
|
Label lTo = new Label(); |
|
Label lCatch = new Label(); |
|
Label lDone = new Label(); |
|
|
|
Class<?> returnType = result.function.resolvedHandle().type().returnType(); |
|
boolean isNonVoid = returnType != void.class; |
|
MethodType type = args.function.resolvedHandle().type() |
|
.dropParameterTypes(0,1) |
|
.changeReturnType(returnType); |
|
MethodType cleanupType = type.insertParameterTypes(0, Throwable.class); |
|
if (isNonVoid) { |
|
cleanupType = cleanupType.insertParameterTypes(1, returnType); |
|
} |
|
String cleanupDesc = cleanupType.basicType().toMethodDescriptorString(); |
|
|
|
|
|
mv.visitTryCatchBlock(lFrom, lTo, lCatch, "java/lang/Throwable"); |
|
|
|
|
|
mv.visitLabel(lFrom); |
|
emitPushArgument(invoker, 0); |
|
emitPushArguments(args, 1); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.basicType().toMethodDescriptorString(), false); |
|
mv.visitLabel(lTo); |
|
|
|
// FINALLY_NORMAL: |
|
emitPushArgument(invoker, 1); |
|
if (isNonVoid) { |
|
mv.visitInsn(Opcodes.SWAP); |
|
} |
|
mv.visitInsn(Opcodes.ACONST_NULL); |
|
if (isNonVoid) { |
|
mv.visitInsn(Opcodes.SWAP); |
|
} |
|
emitPushArguments(args, 1); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false); |
|
mv.visitJumpInsn(Opcodes.GOTO, lDone); |
|
|
|
|
|
mv.visitLabel(lCatch); |
|
mv.visitInsn(Opcodes.DUP); |
|
|
|
// FINALLY_EXCEPTIONAL: |
|
emitPushArgument(invoker, 1); |
|
mv.visitInsn(Opcodes.SWAP); |
|
if (isNonVoid) { |
|
emitZero(BasicType.basicType(returnType)); |
|
} |
|
emitPushArguments(args, 1); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", cleanupDesc, false); |
|
if (isNonVoid) { |
|
mv.visitInsn(Opcodes.POP); |
|
} |
|
mv.visitInsn(Opcodes.ATHROW); |
|
|
|
|
|
mv.visitLabel(lDone); |
|
|
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Name emitLoop(int pos) { |
|
Name args = lambdaForm.names[pos]; |
|
Name invoker = lambdaForm.names[pos+1]; |
|
Name result = lambdaForm.names[pos+2]; |
|
|
|
// extract clause and loop-local state types |
|
|
|
BasicType[] loopClauseTypes = (BasicType[]) invoker.arguments[0]; |
|
Class<?>[] loopLocalStateTypes = Stream.of(loopClauseTypes). |
|
filter(bt -> bt != BasicType.V_TYPE).map(BasicType::basicTypeClass).toArray(Class<?>[]::new); |
|
Class<?>[] localTypes = new Class<?>[loopLocalStateTypes.length + 1]; |
|
localTypes[0] = MethodHandleImpl.LoopClauses.class; |
|
System.arraycopy(loopLocalStateTypes, 0, localTypes, 1, loopLocalStateTypes.length); |
|
|
|
final int clauseDataIndex = extendLocalsMap(localTypes); |
|
final int firstLoopStateIndex = clauseDataIndex + 1; |
|
|
|
Class<?> returnType = result.function.resolvedHandle().type().returnType(); |
|
MethodType loopType = args.function.resolvedHandle().type() |
|
.dropParameterTypes(0,1) |
|
.changeReturnType(returnType); |
|
MethodType loopHandleType = loopType.insertParameterTypes(0, loopLocalStateTypes); |
|
MethodType predType = loopHandleType.changeReturnType(boolean.class); |
|
MethodType finiType = loopHandleType; |
|
|
|
final int nClauses = loopClauseTypes.length; |
|
|
|
|
|
final int inits = 1; |
|
final int steps = 2; |
|
final int preds = 3; |
|
final int finis = 4; |
|
|
|
Label lLoop = new Label(); |
|
Label lDone = new Label(); |
|
Label lNext; |
|
|
|
|
|
emitPushArgument(MethodHandleImpl.LoopClauses.class, invoker.arguments[1]); |
|
mv.visitFieldInsn(Opcodes.GETFIELD, LOOP_CLAUSES, "clauses", MHARY2); |
|
emitAstoreInsn(clauseDataIndex); |
|
|
|
|
|
for (int c = 0, state = 0; c < nClauses; ++c) { |
|
MethodType cInitType = loopType.changeReturnType(loopClauseTypes[c].basicTypeClass()); |
|
emitLoopHandleInvoke(invoker, inits, c, args, false, cInitType, loopLocalStateTypes, clauseDataIndex, |
|
firstLoopStateIndex); |
|
if (cInitType.returnType() != void.class) { |
|
emitStoreInsn(BasicType.basicType(cInitType.returnType()), firstLoopStateIndex + state); |
|
++state; |
|
} |
|
} |
|
|
|
|
|
mv.visitLabel(lLoop); |
|
|
|
for (int c = 0, state = 0; c < nClauses; ++c) { |
|
lNext = new Label(); |
|
|
|
MethodType stepType = loopHandleType.changeReturnType(loopClauseTypes[c].basicTypeClass()); |
|
boolean isVoid = stepType.returnType() == void.class; |
|
|
|
|
|
emitLoopHandleInvoke(invoker, steps, c, args, true, stepType, loopLocalStateTypes, clauseDataIndex, |
|
firstLoopStateIndex); |
|
if (!isVoid) { |
|
emitStoreInsn(BasicType.basicType(stepType.returnType()), firstLoopStateIndex + state); |
|
++state; |
|
} |
|
|
|
|
|
emitLoopHandleInvoke(invoker, preds, c, args, true, predType, loopLocalStateTypes, clauseDataIndex, |
|
firstLoopStateIndex); |
|
mv.visitJumpInsn(Opcodes.IFNE, lNext); |
|
|
|
|
|
emitLoopHandleInvoke(invoker, finis, c, args, true, finiType, loopLocalStateTypes, clauseDataIndex, |
|
firstLoopStateIndex); |
|
mv.visitJumpInsn(Opcodes.GOTO, lDone); |
|
|
|
|
|
mv.visitLabel(lNext); |
|
} |
|
|
|
mv.visitJumpInsn(Opcodes.GOTO, lLoop); |
|
|
|
|
|
mv.visitLabel(lDone); |
|
|
|
return result; |
|
} |
|
|
|
private int extendLocalsMap(Class<?>[] types) { |
|
int firstSlot = localsMap.length - 1; |
|
localsMap = Arrays.copyOf(localsMap, localsMap.length + types.length); |
|
localClasses = Arrays.copyOf(localClasses, localClasses.length + types.length); |
|
System.arraycopy(types, 0, localClasses, firstSlot, types.length); |
|
int index = localsMap[firstSlot - 1] + 1; |
|
int lastSlots = 0; |
|
for (int i = 0; i < types.length; ++i) { |
|
localsMap[firstSlot + i] = index; |
|
lastSlots = BasicType.basicType(localClasses[firstSlot + i]).basicTypeSlots(); |
|
index += lastSlots; |
|
} |
|
localsMap[localsMap.length - 1] = index - lastSlots; |
|
return firstSlot; |
|
} |
|
|
|
private void emitLoopHandleInvoke(Name holder, int handles, int clause, Name args, boolean pushLocalState, |
|
MethodType type, Class<?>[] loopLocalStateTypes, int clauseDataSlot, |
|
int firstLoopStateSlot) { |
|
|
|
emitPushClauseArray(clauseDataSlot, handles); |
|
emitIconstInsn(clause); |
|
mv.visitInsn(Opcodes.AALOAD); |
|
|
|
if (pushLocalState) { |
|
for (int s = 0; s < loopLocalStateTypes.length; ++s) { |
|
emitLoadInsn(BasicType.basicType(loopLocalStateTypes[s]), firstLoopStateSlot + s); |
|
} |
|
} |
|
|
|
emitPushArguments(args, 1); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", type.toMethodDescriptorString(), false); |
|
} |
|
|
|
private void emitPushClauseArray(int clauseDataSlot, int which) { |
|
emitAloadInsn(clauseDataSlot); |
|
emitIconstInsn(which - 1); |
|
mv.visitInsn(Opcodes.AALOAD); |
|
} |
|
|
|
private void emitZero(BasicType type) { |
|
switch (type) { |
|
case I_TYPE: mv.visitInsn(Opcodes.ICONST_0); break; |
|
case J_TYPE: mv.visitInsn(Opcodes.LCONST_0); break; |
|
case F_TYPE: mv.visitInsn(Opcodes.FCONST_0); break; |
|
case D_TYPE: mv.visitInsn(Opcodes.DCONST_0); break; |
|
case L_TYPE: mv.visitInsn(Opcodes.ACONST_NULL); break; |
|
default: throw new InternalError("unknown type: " + type); |
|
} |
|
} |
|
|
|
private void emitPushArguments(Name args, int start) { |
|
MethodType type = args.function.methodType(); |
|
for (int i = start; i < args.arguments.length; i++) { |
|
emitPushArgument(type.parameterType(i), args.arguments[i]); |
|
} |
|
} |
|
|
|
private void emitPushArgument(Name name, int paramIndex) { |
|
Object arg = name.arguments[paramIndex]; |
|
Class<?> ptype = name.function.methodType().parameterType(paramIndex); |
|
emitPushArgument(ptype, arg); |
|
} |
|
|
|
private void emitPushArgument(Class<?> ptype, Object arg) { |
|
BasicType bptype = basicType(ptype); |
|
if (arg instanceof Name) { |
|
Name n = (Name) arg; |
|
emitLoadInsn(n.type, n.index()); |
|
emitImplicitConversion(n.type, ptype, n); |
|
} else if ((arg == null || arg instanceof String) && bptype == L_TYPE) { |
|
emitConst(arg); |
|
} else { |
|
if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) { |
|
emitConst(arg); |
|
} else { |
|
mv.visitLdcInsn(constantPlaceholder(arg)); |
|
emitImplicitConversion(L_TYPE, ptype, arg); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitStoreResult(Name name) { |
|
if (name != null && name.type != V_TYPE) { |
|
|
|
emitStoreInsn(name.type, name.index()); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitReturn(Name onStack) { |
|
|
|
Class<?> rclass = invokerType.returnType(); |
|
BasicType rtype = lambdaForm.returnType(); |
|
assert(rtype == basicType(rclass)); |
|
if (rtype == V_TYPE) { |
|
|
|
mv.visitInsn(Opcodes.RETURN); |
|
// it doesn't matter what rclass is; the JVM will discard any value |
|
} else { |
|
LambdaForm.Name rn = lambdaForm.names[lambdaForm.result]; |
|
|
|
|
|
if (rn != onStack) { |
|
emitLoadInsn(rtype, lambdaForm.result); |
|
} |
|
|
|
emitImplicitConversion(rtype, rclass, rn); |
|
|
|
|
|
emitReturnInsn(rtype); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
private void emitPrimCast(Wrapper from, Wrapper to) { |
|
// Here's how. |
|
// - indicates forbidden |
|
// <-> indicates implicit |
|
// to ----> boolean byte short char int long float double |
|
// from boolean <-> - - - - - - - |
|
// byte - <-> i2s i2c <-> i2l i2f i2d |
|
// short - i2b <-> i2c <-> i2l i2f i2d |
|
// char - i2b i2s <-> <-> i2l i2f i2d |
|
// int - i2b i2s i2c <-> i2l i2f i2d |
|
// long - l2i,i2b l2i,i2s l2i,i2c l2i <-> l2f l2d |
|
// float - f2i,i2b f2i,i2s f2i,i2c f2i f2l <-> f2d |
|
|
|
if (from == to) { |
|
|
|
return; |
|
} |
|
if (from.isSubwordOrInt()) { |
|
|
|
emitI2X(to); |
|
} else { |
|
|
|
if (to.isSubwordOrInt()) { |
|
|
|
emitX2I(from); |
|
if (to.bitWidth() < 32) { |
|
|
|
emitI2X(to); |
|
} |
|
} else { |
|
|
|
boolean error = false; |
|
switch (from) { |
|
case LONG: |
|
switch (to) { |
|
case FLOAT: mv.visitInsn(Opcodes.L2F); break; |
|
case DOUBLE: mv.visitInsn(Opcodes.L2D); break; |
|
default: error = true; break; |
|
} |
|
break; |
|
case FLOAT: |
|
switch (to) { |
|
case LONG : mv.visitInsn(Opcodes.F2L); break; |
|
case DOUBLE: mv.visitInsn(Opcodes.F2D); break; |
|
default: error = true; break; |
|
} |
|
break; |
|
case DOUBLE: |
|
switch (to) { |
|
case LONG : mv.visitInsn(Opcodes.D2L); break; |
|
case FLOAT: mv.visitInsn(Opcodes.D2F); break; |
|
default: error = true; break; |
|
} |
|
break; |
|
default: |
|
error = true; |
|
break; |
|
} |
|
if (error) { |
|
throw new IllegalStateException("unhandled prim cast: " + from + "2" + to); |
|
} |
|
} |
|
} |
|
} |
|
|
|
private void emitI2X(Wrapper type) { |
|
switch (type) { |
|
case BYTE: mv.visitInsn(Opcodes.I2B); break; |
|
case SHORT: mv.visitInsn(Opcodes.I2S); break; |
|
case CHAR: mv.visitInsn(Opcodes.I2C); break; |
|
case INT: break; |
|
case LONG: mv.visitInsn(Opcodes.I2L); break; |
|
case FLOAT: mv.visitInsn(Opcodes.I2F); break; |
|
case DOUBLE: mv.visitInsn(Opcodes.I2D); break; |
|
case BOOLEAN: |
|
|
|
mv.visitInsn(Opcodes.ICONST_1); |
|
mv.visitInsn(Opcodes.IAND); |
|
break; |
|
default: throw new InternalError("unknown type: " + type); |
|
} |
|
} |
|
|
|
private void emitX2I(Wrapper type) { |
|
switch (type) { |
|
case LONG: mv.visitInsn(Opcodes.L2I); break; |
|
case FLOAT: mv.visitInsn(Opcodes.F2I); break; |
|
case DOUBLE: mv.visitInsn(Opcodes.D2I); break; |
|
default: throw new InternalError("unknown type: " + type); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
static MemberName generateLambdaFormInterpreterEntryPoint(MethodType mt) { |
|
assert(isValidSignature(basicTypeSignature(mt))); |
|
String name = "interpret_"+basicTypeChar(mt.returnType()); |
|
MethodType type = mt; |
|
type = type.changeParameterType(0, MethodHandle.class); |
|
InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("LFI", name, type); |
|
return g.loadMethod(g.generateLambdaFormInterpreterEntryPointBytes()); |
|
} |
|
|
|
private byte[] generateLambdaFormInterpreterEntryPointBytes() { |
|
classFilePrologue(); |
|
methodPrologue(); |
|
|
|
|
|
mv.visitAnnotation(LF_HIDDEN_SIG, true); |
|
|
|
|
|
mv.visitAnnotation(DONTINLINE_SIG, true); |
|
|
|
|
|
emitIconstInsn(invokerType.parameterCount()); |
|
mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); |
|
|
|
|
|
for (int i = 0; i < invokerType.parameterCount(); i++) { |
|
Class<?> ptype = invokerType.parameterType(i); |
|
mv.visitInsn(Opcodes.DUP); |
|
emitIconstInsn(i); |
|
emitLoadInsn(basicType(ptype), i); |
|
|
|
if (ptype.isPrimitive()) { |
|
emitBoxing(Wrapper.forPrimitiveType(ptype)); |
|
} |
|
mv.visitInsn(Opcodes.AASTORE); |
|
} |
|
|
|
emitAloadInsn(0); |
|
mv.visitFieldInsn(Opcodes.GETFIELD, MH, "form", "Ljava/lang/invoke/LambdaForm;"); |
|
mv.visitInsn(Opcodes.SWAP); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, LF, "interpretWithArguments", "([Ljava/lang/Object;)Ljava/lang/Object;", false); |
|
|
|
|
|
Class<?> rtype = invokerType.returnType(); |
|
if (rtype.isPrimitive() && rtype != void.class) { |
|
emitUnboxing(Wrapper.forPrimitiveType(rtype)); |
|
} |
|
|
|
|
|
emitReturnInsn(basicType(rtype)); |
|
|
|
methodEpilogue(); |
|
bogusMethod(invokerType); |
|
|
|
final byte[] classFile = cw.toByteArray(); |
|
maybeDump(classFile); |
|
return classFile; |
|
} |
|
|
|
|
|
|
|
*/ |
|
static MemberName generateNamedFunctionInvoker(MethodTypeForm typeForm) { |
|
MethodType invokerType = NamedFunction.INVOKER_METHOD_TYPE; |
|
String invokerName = "invoke_" + shortenSignature(basicTypeSignature(typeForm.erasedType())); |
|
InvokerBytecodeGenerator g = new InvokerBytecodeGenerator("NFI", invokerName, invokerType); |
|
return g.loadMethod(g.generateNamedFunctionInvokerImpl(typeForm)); |
|
} |
|
|
|
private byte[] generateNamedFunctionInvokerImpl(MethodTypeForm typeForm) { |
|
MethodType dstType = typeForm.erasedType(); |
|
classFilePrologue(); |
|
methodPrologue(); |
|
|
|
|
|
mv.visitAnnotation(LF_HIDDEN_SIG, true); |
|
|
|
|
|
mv.visitAnnotation(FORCEINLINE_SIG, true); |
|
|
|
|
|
emitAloadInsn(0); |
|
|
|
|
|
for (int i = 0; i < dstType.parameterCount(); i++) { |
|
emitAloadInsn(1); |
|
emitIconstInsn(i); |
|
mv.visitInsn(Opcodes.AALOAD); |
|
|
|
|
|
Class<?> dptype = dstType.parameterType(i); |
|
if (dptype.isPrimitive()) { |
|
Wrapper dstWrapper = Wrapper.forBasicType(dptype); |
|
Wrapper srcWrapper = dstWrapper.isSubwordOrInt() ? Wrapper.INT : dstWrapper; |
|
emitUnboxing(srcWrapper); |
|
emitPrimCast(srcWrapper, dstWrapper); |
|
} |
|
} |
|
|
|
|
|
String targetDesc = dstType.basicType().toMethodDescriptorString(); |
|
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, MH, "invokeBasic", targetDesc, false); |
|
|
|
|
|
Class<?> rtype = dstType.returnType(); |
|
if (rtype != void.class && rtype.isPrimitive()) { |
|
Wrapper srcWrapper = Wrapper.forBasicType(rtype); |
|
Wrapper dstWrapper = srcWrapper.isSubwordOrInt() ? Wrapper.INT : srcWrapper; |
|
|
|
emitPrimCast(srcWrapper, dstWrapper); |
|
emitBoxing(dstWrapper); |
|
} |
|
|
|
|
|
if (rtype == void.class) { |
|
mv.visitInsn(Opcodes.ACONST_NULL); |
|
} |
|
emitReturnInsn(L_TYPE); |
|
|
|
methodEpilogue(); |
|
bogusMethod(dstType); |
|
|
|
final byte[] classFile = cw.toByteArray(); |
|
maybeDump(classFile); |
|
return classFile; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void bogusMethod(Object os) { |
|
if (DUMP_CLASS_FILES) { |
|
mv = cw.visitMethod(Opcodes.ACC_STATIC, "dummy", "()V", null, null); |
|
mv.visitLdcInsn(os.toString()); |
|
mv.visitInsn(Opcodes.POP); |
|
mv.visitInsn(Opcodes.RETURN); |
|
mv.visitMaxs(0, 0); |
|
mv.visitEnd(); |
|
} |
|
} |
|
} |