|
| 1 | +package openperipheral.interfaces.oc.asm; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import li.cil.oc.api.machine.Arguments; |
| 8 | +import li.cil.oc.api.machine.Callback; |
| 9 | +import li.cil.oc.api.machine.Context; |
| 10 | +import openperipheral.adapter.IMethodExecutor; |
| 11 | +import openperipheral.adapter.WrappedEntityBase; |
| 12 | + |
| 13 | +import org.objectweb.asm.*; |
| 14 | +import org.objectweb.asm.commons.Method; |
| 15 | + |
| 16 | +import com.google.common.collect.Maps; |
| 17 | + |
| 18 | +public class EnvironmentCodeGenerator { |
| 19 | + |
| 20 | + private static final String TARGET_FIELD = "target"; |
| 21 | + |
| 22 | + private static final Type BASE_TYPE = Type.getType(EnvironmentBase.class); |
| 23 | + |
| 24 | + public static final Type EXECUTOR_TYPE = Type.getType(IMethodExecutor.class); |
| 25 | + |
| 26 | + public static final Type EXECUTORS_TYPE = Type.getType(IMethodExecutor[].class); |
| 27 | + |
| 28 | + private static final Type OBJECT_TYPE = Type.getType(Object.class); |
| 29 | + |
| 30 | + private static final Type OBJECTS_TYPE = Type.getType(Object[].class); |
| 31 | + |
| 32 | + private static final Type CONTEXT_TYPE = Type.getType(Context.class); |
| 33 | + |
| 34 | + private static final Type ARGUMENTS_TYPE = Type.getType(Arguments.class); |
| 35 | + |
| 36 | + private static final Type CALLBACK_TYPE = Type.getType(Callback.class); |
| 37 | + |
| 38 | + public static final Type SUPER_CTOR_TYPE = Type.getMethodType(Type.VOID_TYPE, OBJECT_TYPE); |
| 39 | + |
| 40 | + public static final Type GET_METHOD_TYPE = Type.getMethodType(EXECUTOR_TYPE, Type.INT_TYPE); |
| 41 | + |
| 42 | + public static final Type CALL_TYPE = Type.getMethodType(OBJECTS_TYPE, OBJECT_TYPE, EXECUTOR_TYPE, CONTEXT_TYPE, ARGUMENTS_TYPE); |
| 43 | + |
| 44 | + public static final Type WRAP_TYPE = Type.getMethodType(OBJECTS_TYPE, CONTEXT_TYPE, ARGUMENTS_TYPE); |
| 45 | + |
| 46 | + private static void visitIntConst(MethodVisitor mv, int value) { |
| 47 | + switch (value) { |
| 48 | + case 0: |
| 49 | + case 1: |
| 50 | + case 2: |
| 51 | + case 3: |
| 52 | + case 4: |
| 53 | + case 5: |
| 54 | + mv.visitInsn(Opcodes.ICONST_0 + value); |
| 55 | + break; |
| 56 | + default: |
| 57 | + mv.visitLdcInsn(value); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("deprecation") |
| 63 | + public byte[] generate(String clsName, Class<?> targetClass, Set<Class<?>> exposedInterfaces, WrappedEntityBase methods) { |
| 64 | + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 65 | + |
| 66 | + writer.visit(Opcodes.V1_6, |
| 67 | + Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER, |
| 68 | + clsName, null, BASE_TYPE.getInternalName(), getInterfaces(exposedInterfaces)); |
| 69 | + |
| 70 | + Type targetType = Type.getType(targetClass); |
| 71 | + |
| 72 | + writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, TARGET_FIELD, targetType.getDescriptor(), null, null); |
| 73 | + writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, EnvironmentBase.METHODS_FIELD, EXECUTORS_TYPE.getDescriptor(), null, null); |
| 74 | + |
| 75 | + { |
| 76 | + final Type ctorType = Type.getMethodType(Type.VOID_TYPE, targetType); |
| 77 | + |
| 78 | + MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>", ctorType.getDescriptor(), null, null); |
| 79 | + init.visitCode(); |
| 80 | + init.visitVarInsn(Opcodes.ALOAD, 0); |
| 81 | + init.visitVarInsn(Opcodes.ALOAD, 1); |
| 82 | + init.visitInsn(Opcodes.DUP2); |
| 83 | + init.visitMethodInsn(Opcodes.INVOKESPECIAL, BASE_TYPE.getInternalName(), "<init>", SUPER_CTOR_TYPE.getDescriptor()); |
| 84 | + init.visitFieldInsn(Opcodes.PUTFIELD, clsName, TARGET_FIELD, targetType.getDescriptor()); |
| 85 | + init.visitInsn(Opcodes.RETURN); |
| 86 | + |
| 87 | + init.visitMaxs(0, 0); |
| 88 | + |
| 89 | + init.visitEnd(); |
| 90 | + } |
| 91 | + |
| 92 | + final Map<Method, Type> exposedMethods = getExposedMethods(exposedInterfaces); |
| 93 | + for (Map.Entry<Method, Type> e : exposedMethods.entrySet()) { |
| 94 | + final Method method = e.getKey(); |
| 95 | + final Type intf = e.getValue(); |
| 96 | + MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, method.getName(), method.getDescriptor(), null, null); |
| 97 | + |
| 98 | + mv.visitCode(); |
| 99 | + |
| 100 | + mv.visitVarInsn(Opcodes.ALOAD, 0); |
| 101 | + mv.visitFieldInsn(Opcodes.GETFIELD, clsName, TARGET_FIELD, targetType.getDescriptor()); |
| 102 | + |
| 103 | + Type[] args = method.getArgumentTypes(); |
| 104 | + for (int i = 0; i < args.length; i++) |
| 105 | + mv.visitVarInsn(args[i].getOpcode(Opcodes.ILOAD), i + 1); |
| 106 | + |
| 107 | + mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, intf.getInternalName(), method.getName(), method.getDescriptor()); |
| 108 | + Type returnType = method.getReturnType(); |
| 109 | + mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN)); |
| 110 | + |
| 111 | + mv.visitMaxs(0, 0); |
| 112 | + mv.visitEnd(); |
| 113 | + } |
| 114 | + |
| 115 | + String[] names = methods.getMethodNames(); |
| 116 | + |
| 117 | + for (int i = 0; i < names.length; i++) { |
| 118 | + IMethodExecutor executor = methods.getMethod(i); |
| 119 | + |
| 120 | + String name = names[i]; |
| 121 | + String methodName = name.replaceAll("[^A-Za-z0-9_]", "_") + "$" + Integer.toString(i); |
| 122 | + |
| 123 | + MethodVisitor wrap = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, methodName, WRAP_TYPE.getDescriptor(), null, null); |
| 124 | + |
| 125 | + AnnotationVisitor av = wrap.visitAnnotation(CALLBACK_TYPE.getDescriptor(), true); |
| 126 | + av.visit("value", name); |
| 127 | + av.visit("direct", executor.isAsynchronous()); |
| 128 | + av.visit("doc", executor.description().signature()); // TODO: convert to expected format |
| 129 | + av.visitEnd(); |
| 130 | + // TODO: getter/setter |
| 131 | + |
| 132 | + av.visitEnd(); |
| 133 | + |
| 134 | + wrap.visitVarInsn(Opcodes.ALOAD, 0); // this |
| 135 | + wrap.visitFieldInsn(Opcodes.GETFIELD, clsName, TARGET_FIELD, targetType.getDescriptor()); |
| 136 | + |
| 137 | + wrap.visitFieldInsn(Opcodes.GETSTATIC, clsName, EnvironmentBase.METHODS_FIELD, EXECUTORS_TYPE.getDescriptor()); |
| 138 | + visitIntConst(wrap, i); |
| 139 | + wrap.visitInsn(Opcodes.AALOAD); // executor |
| 140 | + |
| 141 | + wrap.visitVarInsn(Opcodes.ALOAD, 1); // context |
| 142 | + wrap.visitVarInsn(Opcodes.ALOAD, 2); // args |
| 143 | + wrap.visitMethodInsn(Opcodes.INVOKESTATIC, BASE_TYPE.getInternalName(), "call", CALL_TYPE.getDescriptor()); |
| 144 | + wrap.visitInsn(Opcodes.ARETURN); |
| 145 | + |
| 146 | + wrap.visitMaxs(0, 0); |
| 147 | + |
| 148 | + wrap.visitEnd(); |
| 149 | + } |
| 150 | + |
| 151 | + writer.visitEnd(); |
| 152 | + |
| 153 | + return writer.toByteArray(); |
| 154 | + } |
| 155 | + |
| 156 | + private static String[] getInterfaces(Set<Class<?>> exposedInterfaces) { |
| 157 | + String[] result = new String[exposedInterfaces.size()]; |
| 158 | + int i = 0; |
| 159 | + for (Class<?> cls : exposedInterfaces) |
| 160 | + result[i++] = Type.getInternalName(cls); |
| 161 | + |
| 162 | + return result; |
| 163 | + } |
| 164 | + |
| 165 | + private static Map<Method, Type> getExposedMethods(Collection<Class<?>> exposedInterfaces) { |
| 166 | + Map<Method, Type> result = Maps.newHashMap(); |
| 167 | + |
| 168 | + for (Class<?> intf : exposedInterfaces) { |
| 169 | + Type intfType = Type.getType(intf); |
| 170 | + |
| 171 | + for (java.lang.reflect.Method m : intf.getMethods()) |
| 172 | + result.put(Method.getMethod(m), intfType); |
| 173 | + } |
| 174 | + |
| 175 | + return result; |
| 176 | + } |
| 177 | +} |
0 commit comments