Example usage for org.objectweb.asm Opcodes ILOAD

List of usage examples for org.objectweb.asm Opcodes ILOAD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ILOAD.

Prototype

int ILOAD

To view the source code for org.objectweb.asm Opcodes ILOAD.

Click Source Link

Usage

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateSkel() throws IOException {
    skelname = fullclassname + "_Skel";
    String skelclassname = classname + "_Skel";
    File file = new File(destination == null ? ""
            : destination + File.separator + skelname.replace('.', File.separatorChar) + ".class");
    if (verbose)/*from  w w w.  ja  va  2 s.  c o  m*/
        System.out.println("[Generating class " + skelname + "]");

    final ClassWriter skel = new ClassWriter(true);
    classInternalName = skelname.replace('.', '/');
    skel.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName,
            Type.getInternalName(Object.class), null,
            new String[] { Type.getType(Skeleton.class).getInternalName() });

    skel.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash",
            Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz)));

    skel.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations",
            Type.getDescriptor(Operation[].class), null, null);

    MethodVisitor clinit = skel.visitMethod(Opcodes.ACC_STATIC, "<clinit>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);

    fillOperationArray(clinit);
    clinit.visitInsn(Opcodes.RETURN);

    clinit.visitMaxs(-1, -1);

    // no arg public constructor
    MethodVisitor init = skel.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    init.visitInsn(Opcodes.RETURN);
    init.visitMaxs(-1, -1);

    /*
     * public Operation[] getOperations()
     * returns a clone of the operations array
     */
    MethodVisitor getOp = skel.visitMethod(Opcodes.ACC_PUBLIC, "getOperations",
            Type.getMethodDescriptor(Type.getType(Operation[].class), new Type[] {}), null, null);
    getOp.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations",
            Type.getDescriptor(Operation[].class));
    getOp.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "clone",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
    getOp.visitTypeInsn(Opcodes.CHECKCAST, typeArg(Operation[].class));
    getOp.visitInsn(Opcodes.ARETURN);
    getOp.visitMaxs(-1, -1);

    // public void dispatch(Remote, RemoteCall, int opnum, long hash)
    MethodVisitor dispatch = skel.visitMethod(Opcodes.ACC_PUBLIC, "dispatch",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Remote.class),
                    Type.getType(RemoteCall.class), Type.INT_TYPE, Type.LONG_TYPE }),
            null, new String[] { Type.getInternalName(Exception.class) });

    Variables var = new Variables();
    var.declare("this");
    var.declare("remoteobj");
    var.declare("remotecall");
    var.declare("opnum");
    var.declareWide("hash");

    /*
     * if opnum >= 0
     * XXX it is unclear why there is handling of negative opnums
     */
    dispatch.visitVarInsn(Opcodes.ILOAD, var.get("opnum"));
    Label nonNegativeOpnum = new Label();
    Label opnumSet = new Label();
    dispatch.visitJumpInsn(Opcodes.IFGE, nonNegativeOpnum);

    for (int i = 0; i < remotemethods.length; i++) {
        // assign opnum if hash matches supplied hash
        dispatch.visitVarInsn(Opcodes.LLOAD, var.get("hash"));
        dispatch.visitLdcInsn(new Long(remotemethods[i].hash));
        Label notIt = new Label();
        dispatch.visitInsn(Opcodes.LCMP);
        dispatch.visitJumpInsn(Opcodes.IFNE, notIt);

        // opnum = <opnum>
        dispatch.visitLdcInsn(new Integer(i));
        dispatch.visitVarInsn(Opcodes.ISTORE, var.get("opnum"));
        dispatch.visitJumpInsn(Opcodes.GOTO, opnumSet);
        dispatch.visitLabel(notIt);
    }

    // throw new SkeletonMismatchException
    Label mismatch = new Label();
    dispatch.visitJumpInsn(Opcodes.GOTO, mismatch);

    dispatch.visitLabel(nonNegativeOpnum);

    // if opnum is already set, check that the hash matches the interface
    dispatch.visitVarInsn(Opcodes.LLOAD, var.get("hash"));
    dispatch.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash",
            Type.LONG_TYPE.getDescriptor());
    dispatch.visitInsn(Opcodes.LCMP);
    dispatch.visitJumpInsn(Opcodes.IFEQ, opnumSet);

    dispatch.visitLabel(mismatch);
    dispatch.visitTypeInsn(Opcodes.NEW, typeArg(SkeletonMismatchException.class));
    dispatch.visitInsn(Opcodes.DUP);
    dispatch.visitLdcInsn("interface hash mismatch");
    dispatch.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(SkeletonMismatchException.class),
            "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    dispatch.visitInsn(Opcodes.ATHROW);

    // opnum has been set
    dispatch.visitLabel(opnumSet);

    dispatch.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj"));
    dispatch.visitTypeInsn(Opcodes.CHECKCAST, typeArg(clazz));
    dispatch.visitVarInsn(Opcodes.ASTORE, var.get("remoteobj"));

    Label deflt = new Label();
    Label[] methLabels = new Label[remotemethods.length];
    for (int i = 0; i < methLabels.length; i++)
        methLabels[i] = new Label();

    // switch on opnum
    dispatch.visitVarInsn(Opcodes.ILOAD, var.get("opnum"));
    dispatch.visitTableSwitchInsn(0, remotemethods.length - 1, deflt, methLabels);

    // Method dispatch
    for (int i = 0; i < remotemethods.length; i++) {
        dispatch.visitLabel(methLabels[i]);
        Method m = remotemethods[i].meth;
        generateMethodSkel(dispatch, m, var);
    }

    dispatch.visitLabel(deflt);
    dispatch.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
    dispatch.visitInsn(Opcodes.DUP);
    dispatch.visitLdcInsn("invalid method number");
    dispatch.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    dispatch.visitInsn(Opcodes.ATHROW);

    dispatch.visitMaxs(-1, -1);

    skel.visitEnd();
    byte[] classData = skel.toByteArray();
    if (!noWrite) {
        if (file.exists())
            file.delete();
        if (file.getParentFile() != null)
            file.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(classData);
        fos.flush();
        fos.close();
    }
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private static int loadOpcode(Class cls) {
    if (cls.equals(Void.TYPE))
        throw new IllegalArgumentException("can not load void");

    int loadcode;
    if (cls.equals(Boolean.TYPE))
        loadcode = Opcodes.ILOAD;
    else if (cls.equals(Byte.TYPE))
        loadcode = Opcodes.ILOAD;//  ww w  . j a v  a 2  s  .  c om
    else if (cls.equals(Character.TYPE))
        loadcode = Opcodes.ILOAD;
    else if (cls.equals(Short.TYPE))
        loadcode = Opcodes.ILOAD;
    else if (cls.equals(Integer.TYPE))
        loadcode = Opcodes.ILOAD;
    else if (cls.equals(Long.TYPE))
        loadcode = Opcodes.LLOAD;
    else if (cls.equals(Float.TYPE))
        loadcode = Opcodes.FLOAD;
    else if (cls.equals(Double.TYPE))
        loadcode = Opcodes.DLOAD;
    else
        loadcode = Opcodes.ALOAD;

    return loadcode;
}

From source file:io.awacs.plugin.springmvc.ServiceWrapper.java

License:Apache License

/**
 * origin method: (XXX)X//w  ww  .  ja  va  2 s  .c om
 * <p>
 * proxy method: (XXXLjavax/servlet/http/HttpServletRequest;)X
 * req -> localIndex - 1
 *
 * @param cn
 * @param origin
 * @return
 */
private MethodNode doProxy(ClassNode cn, MethodNode origin) {

    MethodNode newNode = copyMethod(origin);
    //move annotations
    hideAnnotations(origin);

    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();

    String returnType = origin.desc.substring(origin.desc.indexOf(')') + 1);

    int localIndex = copyParameters(origin, newNode, l0, l2);

    newNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Exception"));

    //localIndex -> elapsedTime
    newNode.localVariables.add(new LocalVariableNode("elapsedTime", "J", null, l0, l1, localIndex));

    //
    newNode.instructions.add(l0);

    // long elapsedTime = System.currentTimeMillis();
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    int paramCount = getParamCount(origin.desc, origin.access);

    //load?
    for (int i = 0; i < paramCount; i++) {
        LocalVariableNode node = (LocalVariableNode) newNode.localVariables.get(i);
        switch (node.desc) {
        case "J":
            newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, node.index));
            break;
        case "D":
            newNode.instructions.add(new VarInsnNode(Opcodes.DLOAD, node.index));
            break;
        case "F":
            newNode.instructions.add(new VarInsnNode(Opcodes.FLOAD, node.index));
            break;
        case "I":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "S":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "Z":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "B":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "C":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        default:
            newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, node.index));
            break;
        }
    }
    //
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, cn.name, origin.name, origin.desc));

    //
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.LSUB));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    //?
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    //SpringmvcPlugin.incrAccess(uri, elapsedTime);
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/springmvc/SpringmvcPlugin", "incrAccess", "(Ljava/lang/String;J)V"));
    //
    newNode.instructions.add(l1);
    switch (returnType) {
    case "J":
        newNode.instructions.add(new InsnNode(Opcodes.LRETURN));
        break;
    case "D":
        newNode.instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case "F":
        newNode.instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    case "I":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "S":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "C":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "B":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "Z":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    default:
        newNode.instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    }

    newNode.instructions.add(l2);
    newNode.instructions
            .add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }));
    newNode.instructions.add(new VarInsnNode(Opcodes.ASTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/springmvc/SpringmvcPlugin",
                    "incrFailure", "(Ljava/lang/String;Ljava/lang/Throwable;)V"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.ATHROW));
    newNode.maxLocals = origin.maxLocals + 3;
    newNode.maxStack = Math.max(localIndex + 2, 6);
    return newNode;
}

From source file:io.awacs.plugin.stacktrace.ClassTransformer.java

License:Apache License

/**
 * ?/*ww w.ja v a  2  s.  c o m*/
 * 1?try catch?                 try{
 * 2????        io.awacs.plugin.stacktrace.StackFrames.init();
 * 3????                io.awacs.plugin.stacktrace.StackFrames.push(className,methodName,0);
 * 4?                          Object val = methodName_origin_className(args);
 * 5?????                io.awacs.plugin.stacktrace.StackFrames.push(className,methodName,1);
 * 5???          List list = io.awacs.plugin.stacktrace.StackFrames.dump();
 * 7?????          io.awacs.plugin.stacktrace.StackTracePlugin.incrAccess(list);
 * 8?                          return val;
 * }catch(java.lang.Exception e){
 * 9??                io.awacs.plugin.stacktrace.StackTracePlugin.incrFailure(e);
 * 10?               throw e;
 * }
 */
private void transformTerminatedMethod(MethodNode origin, MethodNode proxy, ClassNode owner) {
    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();
    //try catch?
    proxy.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Exception"));
    proxy.instructions.add(l0);
    //?
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "init", "()V", false));
    proxy.instructions.add(new LdcInsnNode(owner.name.replaceAll("/", ".")));
    proxy.instructions.add(new LdcInsnNode(proxy.name));
    proxy.instructions.add(new LdcInsnNode(0));
    //
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "push", "(Ljava/lang/String;Ljava/lang/String;I)V", false));
    int varIndex = 0;//???
    //???,????this?
    if ((proxy.access & Opcodes.ACC_STATIC) != Opcodes.ACC_STATIC) {
        proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }
    List<String> parameters = resolveParameters(proxy.desc);
    //???
    for (String param : parameters) {
        VarInsnNode insnNode;
        switch (param) {
        case "J":
            insnNode = new VarInsnNode(Opcodes.LLOAD, varIndex);
            varIndex += 2;
            break;
        case "D":
            insnNode = new VarInsnNode(Opcodes.DLOAD, varIndex);
            varIndex += 2;
            break;
        case "F":
            insnNode = new VarInsnNode(Opcodes.FLOAD, varIndex++);
            break;
        case "I":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "S":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "Z":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "B":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "C":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        default:
            insnNode = new VarInsnNode(Opcodes.ALOAD, varIndex++);
            break;
        }
        proxy.instructions.add(insnNode);
    }
    //
    if ((origin.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
        proxy.instructions
                .add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner.name, origin.name, origin.desc, false));
    else
        proxy.instructions
                .add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, owner.name, origin.name, origin.desc, false));
    proxy.instructions.add(new LdcInsnNode(owner.name.replaceAll("/", ".")));
    proxy.instructions.add(new LdcInsnNode(proxy.name));
    proxy.instructions.add(new LdcInsnNode(1));
    //?
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "push", "(Ljava/lang/String;Ljava/lang/String;I)V", false));
    //??
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "dump", "()Ljava/util/List;", false));
    //???
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/stacktrace/StackTracePlugin", "incrAccess", "(Ljava/util/List;)V", false));
    proxy.instructions.add(l1);
    //
    String returnType = origin.desc.substring(origin.desc.indexOf(')') + 1);
    switch (returnType) {
    case "J":
        proxy.instructions.add(new InsnNode(Opcodes.LRETURN));
        break;
    case "D":
        proxy.instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case "F":
        proxy.instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    case "I":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "S":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "C":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "B":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "Z":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "V":
        proxy.instructions.add(new InsnNode(Opcodes.RETURN));
        break;
    default:
        proxy.instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    }
    proxy.instructions.add(l2);
    //?
    proxy.instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }));
    proxy.instructions.add(new VarInsnNode(Opcodes.ASTORE, varIndex));
    proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, varIndex));
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/stacktrace/StackTracePlugin", "incrFailure", "(Ljava/lang/Throwable;)V", false));
    proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, varIndex));
    proxy.instructions.add(new InsnNode(Opcodes.ATHROW));
    proxy.maxLocals = varIndex + 1;
    proxy.maxStack = Math.max(varIndex, 5);
}

From source file:jasy.lang.ASMCompiler.java

private static int getLoad(Class<?> type) {
    String typeDescriptor = Type.getDescriptor(type);
    switch (typeDescriptor) {
    case "V":
        return -1;
    case "J":
        return Opcodes.LLOAD;
    case "D":
        return Opcodes.DLOAD;
    case "F":
        return Opcodes.FLOAD;
    case "I":
    case "Z":
    case "B":
    case "C":
    case "S":
        return Opcodes.ILOAD;
    default://from   w  ww . j  ava  2 s . c om
        return Opcodes.ALOAD;
    }
}

From source file:jp.co.dgic.testing.common.virtualmock.asm.AbstractAsmMethodVisitor.java

License:Open Source License

protected int getLoadOpcodeByType(Type type) {
    if (type.equals(Type.BOOLEAN_TYPE))
        return Opcodes.ILOAD;
    if (type.equals(Type.BYTE_TYPE))
        return Opcodes.ILOAD;
    if (type.equals(Type.CHAR_TYPE))
        return Opcodes.ILOAD;
    if (type.equals(Type.SHORT_TYPE))
        return Opcodes.ILOAD;
    if (type.equals(Type.INT_TYPE))
        return Opcodes.ILOAD;
    if (type.equals(Type.LONG_TYPE))
        return Opcodes.LLOAD;
    if (type.equals(Type.DOUBLE_TYPE))
        return Opcodes.DLOAD;
    if (type.equals(Type.FLOAT_TYPE))
        return Opcodes.FLOAD;
    return Opcodes.ALOAD;
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

@Override
public void loadLocalVar(int localVar) {
    mv.visitVarInsn(Opcodes.ILOAD, localVar);
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void loadFTmpVd(int n, boolean isFloat) {
    int opcode = isFloat ? Opcodes.FLOAD : Opcodes.ILOAD;
    if (n == 0) {
        mv.visitVarInsn(opcode, LOCAL_TMP_VD0);
    } else if (n == 1) {
        mv.visitVarInsn(opcode, LOCAL_TMP_VD1);
    } else {// w w w. ja v a2 s.  co  m
        mv.visitVarInsn(opcode, LOCAL_TMP_VD2);
    }
}

From source file:lapin.comp.asm.ASMByteCodeGenerator.java

License:Open Source License

private void generateCall(CallableInfo ci, Env env) {
    /*//from w ww. j  a  va2s .co  m
     * local variables
     * <Callable#call>
     * 0: this
     * 1: args (list of arguments)
     * 2: env
     *
     * <Callable0#call0>
     * 0: this
     * 1: env
     *
     * <Callable1#call1>
     * 0: this
     * 1: arg0
     * 2: env
     *
     * <Callable2#call2>
     * 0: this
     * 1: arg0
     * 2: arg1
     * 3: env
     *
     * ...
     *
     */
    MethodVisitor mv = _cw.visitMethod(
            ci.mi.implCallable() ? Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL : Opcodes.ACC_FINAL, ci.mi.name(),
            Type.getMethodDescriptor(ci.retType, ci.paramTypes), null, null);

    // instruction list
    int len = ci.mi.instLen();
    // label
    Label label = null;

    // generate code
    for (int i = 0; i < len; i++) {
        Object inst = ci.mi.getInst(i);
        if (Logger.tracelevelp(env))
            Logger.trace("[asm:gen]" + i + ":\t~S", Lists.list(inst), env);

        // inst is symbol
        // -> convert tag (Symbol) to label (ASMe Label object)
        if (Data.isSymbol(inst)) {
            Symbol tag = Data.symbol(inst);
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label is null: ~S.", Lists.list(tag));
            } else if (l != label) {
                mv.visitLabel(l);
                label = l;
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S", Lists.list(tag, l), env);
            } else {
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S" + " (dup)", Lists.list(tag, l), env);
            }
            continue;
        }

        // inst must be the form of (id <arg1> <arg2> ....)
        Object id = Lists.car(inst);
        if (id == Insts.CONST) {
            /* push const on the stack. */
            Object obj = Lists.cadr(inst);
            String val = Data.string(constTable.get(obj));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_OBJECT.getDescriptor());
        } else if (id == Insts.VAR) {
            /* push var on the stack */
            Object var = Lists.cadr(inst);
            String val = Data.string(varTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_SYMBOL.getDescriptor());
        } else if (id == Insts.LAMBDA_LIST) {
            /* push lambdaList on the stack */
            Object var = Lists.cadr(inst);
            /* push _ll_<i> on the stack */
            String val = Data.string(llTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_LAMBDA_LIST.getDescriptor());
        } else if (id == Insts.ENV_GET) {
            /* env.get */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "get",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_SET) {
            /* env.set */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_BIND) {
            /* env.bind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_UNBIND) {
            /* env.unbind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbind",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_CHILD) {
            /* env.child */
            Object oldEnvVar = Lists.cadr(inst);
            Object newEnvVar = Lists.caddr(inst);
            Object oldSlot = Lists.cadr(oldEnvVar);
            Object newSlot = Lists.cadr(newEnvVar);
            int oldLocal = Data.fixnum(ci.localTable.get(oldSlot)).intValue();
            int newLocal = Data.fixnum(ci.localTable.get(newSlot)).intValue();
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tenv-child: local ~S -> ~S",
                        Lists.list(Data.toFixnum(oldLocal), Data.toFixnum(newLocal)), env);
            mv.visitVarInsn(Opcodes.ALOAD, oldLocal);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "child",
                    Type.getMethodDescriptor(TYPE_ENV, TYPE_NO_ARGS));
            mv.visitVarInsn(Opcodes.ASTORE, newLocal);
        } else if (id == Insts.CALL) {
            /* funcall */
            int nargs = Data.fixnum(Lists.cadr(inst)).intValue();
            String className = "lapin.eval.Funcall";
            String methodName = nargs < 0 ? "funcall" : "funcall" + nargs;
            Class rType = Object.class;
            Class[] pTypes;
            if (nargs < 0) {
                pTypes = new Class[] { Function.class, Object.class, // list of args
                        Env.class };
            } else {
                pTypes = new Class[nargs + 2];
                pTypes[0] = Function.class;
                for (int j = 0; j < nargs; j++) {
                    pTypes[j + 1] = Object.class;
                }
                pTypes[nargs + 1] = Env.class;
            }

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKESTATIC, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CALL_DIRECT) {
            /*
             * public class Foo
             *   extends CompiledExpr implements Callable2 {
             *  public Object call2(Object arg0, Object arg1, Env env) {
             *   ...
             *  }
             * }
             */
            MethodInfo mi = (MethodInfo) Lists.cadr(inst);
            String className = mi.classInfo().classname();
            int nargs = mi.nargs();
            boolean rest = mi.rest();
            String methodName = mi.name();
            Class rType = mi.retType();
            Class[] pTypes = mi.paramTypes();

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.COMPILED_EXPR) {
            /*
             * public class Foo extends CompiledExpr {
             *  static public Foo SELF;
             *  ...
             * }
             */
            String className = Data.string(Lists.cadr(inst));
            String fieldName = "SELF";
            String typeName = className;
            mv.visitFieldInsn(Opcodes.GETSTATIC, toInternalName(className), fieldName,
                    toTypeDescriptor(typeName));
        } else if (id == Insts.RETURN) {
            /* return */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class) || type.equals(short.class) || type.equals(byte.class)
                    || type.equals(char.class)) {
                mv.visitInsn(Opcodes.IRETURN);
            } else if (type.equals(long.class)) {
                mv.visitInsn(Opcodes.LRETURN);
            } else if (type.equals(float.class)) {
                mv.visitInsn(Opcodes.FRETURN);
            } else if (type.equals(double.class)) {
                mv.visitInsn(Opcodes.DRETURN);
            } else if (type.equals(void.class)) {
                //mv.visitInsn(Opcodes.RETURN);
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(type));
            } else {
                mv.visitInsn(Opcodes.ARETURN);
            }
        } else if (id == Insts.IFEQ) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFEQ, l);
        } else if (id == Insts.IFNE) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFNE, l);
        } else if (id == Insts.GOTO) {
            /* jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.GOTO, l);
        } else if (id == Insts.LOAD) {
            /* local -> stack */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ILOAD);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tload: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.STORE) {
            /* stack -> local */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ISTORE);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tstore: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.POP) {
            /* pop a value and discard it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.POP;
                break;
            case 2:
                op = Opcodes.POP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.DUP) {
            /* peek a value and duplicate it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.DUP;
                break;
            case 2:
                op = Opcodes.DUP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.PUSH) {
            /* push a constant */
            Object val = Lists.cadr(inst);
            if (Data.isJavaBoolean(val)) {
                if (Data.javaBoolean(val).booleanValue())
                    mv.visitInsn(Opcodes.ICONST_1);
                else
                    mv.visitInsn(Opcodes.ICONST_0);
            } else if (val instanceof Byte || val instanceof Short || val instanceof Integer) {
                int n = Data.javaNumber(val).intValue();
                if (n == -1)
                    mv.visitInsn(Opcodes.ICONST_M1);
                else if (n == 0)
                    mv.visitInsn(Opcodes.ICONST_0);
                else if (n == 1)
                    mv.visitInsn(Opcodes.ICONST_1);
                else if (n == 2)
                    mv.visitInsn(Opcodes.ICONST_2);
                else if (n == 3)
                    mv.visitInsn(Opcodes.ICONST_3);
                else if (n == 4)
                    mv.visitInsn(Opcodes.ICONST_4);
                else if (n == 5)
                    mv.visitInsn(Opcodes.ICONST_5);
                else if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (val instanceof Long) {
                long n = Data.javaNumber(val).longValue();
                if (n == 0L)
                    mv.visitInsn(Opcodes.LCONST_0);
                else if (n == 1L)
                    mv.visitInsn(Opcodes.LCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Float) {
                float n = Data.javaNumber(val).floatValue();
                if (n == 0.0f)
                    mv.visitInsn(Opcodes.FCONST_0);
                else if (n == 1.0f)
                    mv.visitInsn(Opcodes.FCONST_1);
                else if (n == 2.0f)
                    mv.visitInsn(Opcodes.FCONST_2);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Double) {
                double n = Data.javaNumber(val).doubleValue();
                if (n == 0.0)
                    mv.visitInsn(Opcodes.DCONST_0);
                else if (n == 1.0)
                    mv.visitInsn(Opcodes.DCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (Data.isCharacter(val)) {
                Character c = Data.character(val);
                int n = (int) c.charValue();
                if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (Data.isString(val)) {
                mv.visitLdcInsn(val);
            } else {
                throw new NotReachedException("cannot push: ~S.", Lists.list(val));
            }
        } else if (id == Insts.GET) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.PUT) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.INVOKE) {
            Method m = Data.javaMethod(Lists.cadr(inst));
            String methodName = m.getName();
            Class c = m.getDeclaringClass();
            String className = c.getName();
            Class rType = m.getReturnType();
            Class[] pTypes = m.getParameterTypes();
            if (rType.equals(void.class)) {
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(rType));
            }
            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            boolean isStatic = Classes.isStatic(m);
            boolean isInterface = c.isInterface();
            int op = isStatic ? Opcodes.INVOKESTATIC
                    : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;

            mv.visitMethodInsn(op, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CHECKCAST) {
            Class c = Data.javaClass(Lists.cadr(inst));
            Type t = Type.getType(c);
            mv.visitTypeInsn(Opcodes.CHECKCAST, t.getInternalName());
        } else if (id == Insts.THROW) {
            mv.visitInsn(Opcodes.ATHROW);
        } else if (id == Insts.CATCH) {
            Symbol tagS = Data.symbol(Lists.cadr(inst));
            Symbol tagE = Data.symbol(Lists.caddr(inst));
            Symbol tagH = Data.symbol(Lists.cadddr(inst));
            String className;
            if (Lists.isEnd(Lists.cddddr(inst))) {
                className = null;
            } else {
                Class c = Data.javaClass(Lists.car(Lists.cddddr(inst)));
                className = toInternalName(c.getName());
            }
            Label labelS = (Label) ci.labelTable.get(tagS);
            if (labelS == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagS));
            }
            Label labelE = (Label) ci.labelTable.get(tagE);
            if (labelE == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagE));
            }
            Label labelH = (Label) ci.labelTable.get(tagH);
            if (labelH == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagH));
            }
            mv.visitTryCatchBlock(labelS, labelE, labelH, className);
        }
        //else if (id == Insts.CATCH_FROM ||
        //         id == Insts.CATCH_TO ||
        //         id == Insts.CATCH_HANDLER) {
        //    /* nothing emitted */
        //    continue;
        //}
        else {
            throw new NotReachedException("unknown inst: ~S.", Lists.list(inst));
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:lombok.patcher.MethodLogistics.java

License:Open Source License

private static int loadOpcodeFor(String spec) {
    switch (spec.charAt(0)) {
    case 'D':
        return Opcodes.DLOAD;
    case 'J':
        return Opcodes.LLOAD;
    case 'F':
        return Opcodes.FLOAD;
    case 'I':
    case 'S':
    case 'B':
    case 'Z':
        return Opcodes.ILOAD;
    case 'V':
        throw new IllegalArgumentException("There's no load opcode for 'void'");
    case 'L':
    case '[':
        return Opcodes.ALOAD;
    }//from  ww  w. j  ava 2 s  . c  o m

    throw new IllegalStateException("Uhoh - bug - unrecognized JVM type: " + spec);
}