Example usage for org.objectweb.asm Opcodes SIPUSH

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

Introduction

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

Prototype

int SIPUSH

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

Click Source Link

Usage

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void pushIntConst(MethodVisitor v, int n) {
    if (ICONSTS.containsKey(n)) {
        v.visitInsn(ICONSTS.get(n));/*from ww w .  j  av  a  2  s .co m*/
    } else if (n >= Byte.MIN_VALUE && n <= Byte.MAX_VALUE) {
        v.visitIntInsn(Opcodes.BIPUSH, n);
    } else if (n >= Short.MIN_VALUE && n <= Short.MAX_VALUE) {
        v.visitIntInsn(Opcodes.SIPUSH, n);
    } else {
        v.visitLdcInsn(n);
    }
}

From source file:br.usp.each.saeg.badua.core.internal.instr.InstrSupport.java

License:Open Source License

/**
 * Generates the instruction to push the given int value on the stack.
 * Implementation taken from/*from   ww  w .  j  ava  2  s . com*/
 * {@link org.objectweb.asm.commons.GeneratorAdapter#push(int)}.
 *
 * @param mv
 *            visitor to emit the instruction
 * @param value
 *            the value to be pushed on the stack.
 */
public static void push(final MethodVisitor mv, final int value) {
    if (value >= -1 && value <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + value);
    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        mv.visitLdcInsn(Integer.valueOf(value));
    }
}

From source file:br.usp.each.saeg.badua.core.internal.instr.InstrSupport.java

License:Open Source License

/**
 * Generates the instruction to push the given int value on the stack.
 *
 * @param value//from w  ww  .  j  a va  2 s. com
 *            the value to be pushed on the stack.
 * @return the AbstractInsnNode that push the given int value on the stack.
 */
public static AbstractInsnNode push(final int value) {
    final AbstractInsnNode insn;
    if (value >= -1 && value <= 5) {
        insn = new InsnNode(Opcodes.ICONST_0 + value);
    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        insn = new IntInsnNode(Opcodes.BIPUSH, value);
    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        insn = new IntInsnNode(Opcodes.SIPUSH, value);
    } else {
        insn = new LdcInsnNode(value);
    }
    return insn;
}

From source file:bytecode.InstructionExporter.java

License:Apache License

/**
 * Outputs constants into stack operations. This will use different JVM
 * instructions depending on whether there are short-hands for the given
 * constant etc.//  www.  j  a  v a  2  s  . co m
 *
 * @param instruction Constant.
 * @return            <code>null</code>
 */
@Override
public Void visit(Constant instruction) {
    // NULL Pointer
    if (instruction.getConstant() == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
        // Integers
    } else if (instruction.getConstant() instanceof Integer) {
        Integer i = (Integer) instruction.getConstant();

        switch (i.intValue()) {
        case -1:
            mv.visitInsn(Opcodes.ICONST_M1);
            break;
        case 0:
            mv.visitInsn(Opcodes.ICONST_0);
            break;
        case 1:
            mv.visitInsn(Opcodes.ICONST_1);
            break;
        case 2:
            mv.visitInsn(Opcodes.ICONST_2);
            break;
        case 3:
            mv.visitInsn(Opcodes.ICONST_3);
            break;
        case 4:
            mv.visitInsn(Opcodes.ICONST_4);
            break;
        case 5:
            mv.visitInsn(Opcodes.ICONST_5);
            break;
        default:
            mv.visitLdcInsn(i);
        }
        // Longs
    } else if (instruction.getConstant() instanceof Long) {
        Long l = (Long) instruction.getConstant();

        if (l.longValue() == 0)
            mv.visitInsn(Opcodes.LCONST_0);
        else if (l.longValue() == 1)
            mv.visitInsn(Opcodes.ICONST_1);
        else
            mv.visitLdcInsn(l);
        // Floats
    } else if (instruction.getConstant() instanceof Float) {
        Float f = (Float) instruction.getConstant();

        // Should be safe since 0 and 1 are both exactly representable in FP.
        if (f.floatValue() == 0.0f)
            mv.visitInsn(Opcodes.FCONST_0);
        else if (f.floatValue() == 1.0f)
            mv.visitInsn(Opcodes.FCONST_1);
        else if (f.floatValue() == 2.0f)
            mv.visitInsn(Opcodes.FCONST_2);
        else
            mv.visitLdcInsn(f);
        // Doubles
    } else if (instruction.getConstant() instanceof Double) {
        Double d = (Double) instruction.getConstant();

        // Should be safe since 0 and 1 are both exactly representable in FP.
        if (d.doubleValue() == 0.0f)
            mv.visitInsn(Opcodes.DCONST_0);
        else if (d.doubleValue() == 1.0f)
            mv.visitInsn(Opcodes.DCONST_1);
        else
            mv.visitLdcInsn(d);
        // Byte Pushes
    } else if (instruction.getConstant() instanceof Byte) {
        Byte b = (Byte) instruction.getConstant();

        mv.visitIntInsn(Opcodes.BIPUSH, b.intValue());
        // Short Pushes
    } else if (instruction.getConstant() instanceof Short) {
        Short s = (Short) instruction.getConstant();

        mv.visitIntInsn(Opcodes.SIPUSH, s.intValue());
        // Otherwise a constant pool load.
    } else {
        mv.visitLdcInsn(instruction.getConstant());
    }

    return null;
}

From source file:bytecode.MethodImporter.java

License:Apache License

/**
 * Imports instructions with a single integer operand (byte push, short push
 * and allocation of primitive arrays).//from  w  ww . ja v  a  2 s. c om
 *
 * @param opcode  Opcode.
 * @param operand Integer operand.
 */
@Override
public void visitIntInsn(final int opcode, final int operand) {
    switch (opcode) {
    // Constants
    case Opcodes.BIPUSH:
        createConstant(new Byte((byte) operand));
        break;
    case Opcodes.SIPUSH:
        createConstant(new Short((short) operand));
        break;

    // New Array (Primitive)
    case Opcodes.NEWARRAY:
        Type type = null;

        switch (operand) {
        case Opcodes.T_BOOLEAN:
            type = Type.BOOL;
            break;
        case Opcodes.T_CHAR:
            type = Type.CHAR;
            break;
        case Opcodes.T_FLOAT:
            type = Type.FLOAT;
            break;
        case Opcodes.T_DOUBLE:
            type = Type.DOUBLE;
            break;
        case Opcodes.T_BYTE:
            type = Type.BYTE;
            break;
        case Opcodes.T_SHORT:
            type = Type.SHORT;
            break;
        case Opcodes.T_INT:
            type = Type.INT;
            break;
        case Opcodes.T_LONG:
            type = Type.LONG;
            break;
        }

        ordered.add(stack.push(new NewArray(type, stack.pop())));
        break;
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchClient(ClassNode node) {
    Logger.Info("Patching client (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        // I (I)V is where most of the interface is processed
        if (methodNode.name.equals("I") && methodNode.desc.equals("(I)V")) {
            // Show combat menu
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                if (insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode bipush = (IntInsnNode) insnNode;

                    if (bipush.operand == -9) {
                        AbstractInsnNode findNode = insnNode;
                        while (findNode.getOpcode() != Opcodes.IF_ICMPEQ)
                            findNode = findNode.getNext();

                        LabelNode label = ((JumpInsnNode) findNode).label;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_MENU", "Z"));
                        methodNode.instructions.insertBefore(insnNode, new JumpInsnNode(Opcodes.IFGT, label));
                        break;
                    }//from ww w. j  ava2 s  .c  om
                }
            }
        } else if (methodNode.name.equals("J") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Chat command patch
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 627) {
                        AbstractInsnNode jmpNode = insnNode;
                        while (jmpNode.getOpcode() != Opcodes.IFEQ)
                            jmpNode = jmpNode.getNext();

                        AbstractInsnNode insertNode = insnNode;
                        while (insertNode.getOpcode() != Opcodes.INVOKEVIRTUAL)
                            insertNode = insertNode.getPrevious();

                        JumpInsnNode jmp = (JumpInsnNode) jmpNode;
                        methodNode.instructions.insert(insertNode, new VarInsnNode(Opcodes.ASTORE, 2));
                        methodNode.instructions.insert(insertNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                                "Game/Client", "processChatCommand", "(Ljava/lang/String;)Ljava/lang/String;"));
                        methodNode.instructions.insert(insertNode, new VarInsnNode(Opcodes.ALOAD, 2));
                    }
                }
            }
        } else if (methodNode.name.equals("h") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Private chat command patch
                if (insnNode.getOpcode() == Opcodes.GETFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;
                    if (field.owner.equals("client") && field.name.equals("Ob")
                            && insnNode.getPrevious().getPrevious().getOpcode() != Opcodes.INVOKEVIRTUAL) {
                        insnNode = insnNode.getPrevious().getPrevious();
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.PUTFIELD, "client", "Ob", "Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode,
                                new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "processPrivateCommand",
                                        "(Ljava/lang/String;)Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.GETFIELD, "client", "Ob", "Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                        break;
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IIIIIIII)V")) {
            // Draw NPC hook
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 8));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 1));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 4));

            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "e", "Mb", "[Ljava/lang/String;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Tb", "[Lta;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 6));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode, new FieldInsnNode(Opcodes.GETFIELD, "ta", "t", "I"));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "Game/Client", "drawNPC", "(IIIILjava/lang/String;)V"));
        } else if (methodNode.name.equals("b") && methodNode.desc.equals("(IIIIIIII)V")) {
            // Draw Player hook
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 6));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 2));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "rg", "[Lta;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 8));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "ta", "C", "Ljava/lang/String;"));
            methodNode.instructions.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "Game/Client", "drawPlayer", "(IIIILjava/lang/String;)V"));
        } else if (methodNode.name.equals("b") && methodNode.desc.equals("(IIIIIII)V")) {
            // Draw Item hook
            // ILOAD 4 is item id
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 3));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 1));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 4));
            methodNode.instructions.insertBefore(insnNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "drawItem", "(IIIII)V"));
        } else if (methodNode.name.equals("L") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Right click bounds fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    AbstractInsnNode nextNode = insnNode.getNext();

                    if (call.operand == 510) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    } else if (call.operand == 315) {
                        call.operand = 334 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height_client", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    } else if (call.operand == -316) {
                        call.operand = 334 - (call.operand * -1);
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height_client", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(ZZ)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Friends chat mouse fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 489 || call.operand == 429) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                    if (call.operand == -430) {
                        call.operand = 512 - (call.operand * -1);
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("i") && methodNode.desc.equals("(I)V")) {
            AbstractInsnNode lastNode = methodNode.instructions.getLast().getPrevious();

            // Send combat style option
            LabelNode label = new LabelNode();

            methodNode.instructions.insert(lastNode, label);

            // Format
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(I)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.SIPUSH, 21294));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Write byte
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "ja", "c", "(II)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, -80));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "da", "f", "Lja;"));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Create Packet
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(II)V", false));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 29));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Skip combat packet if style is already controlled
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IF_ICMPLE, label));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));

            // Client init_game
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init_game", "()V", false));
        } else if (methodNode.name.equals("o") && methodNode.desc.equals("(I)V")) {
            // Client.init_login patch
            AbstractInsnNode findNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(findNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init_login", "()V", false));
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Camera view distance crash fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 15000) {
                        call.operand = 32767;
                    }
                }
            }

            // Client.init patch
            AbstractInsnNode findNode = methodNode.instructions.getFirst();
            methodNode.instructions.insertBefore(findNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(findNode,
                    new FieldInsnNode(Opcodes.PUTSTATIC, "Game/Client", "instance", "Ljava/lang/Object;"));
            methodNode.instructions.insertBefore(findNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init", "()V", false));
        } else if (methodNode.name.equals("G") && methodNode.desc.equals("(I)V")) {
            // TODO: This can be shortened, I'll fix it another time

            // NPC Dialogue keyboard
            AbstractInsnNode lastNode = methodNode.instructions.getLast().getPrevious();

            LabelNode label = new LabelNode();

            methodNode.instructions.insert(lastNode, label);

            // Hide dialogue
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.PUTFIELD, "client", "Ph", "Z"));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Format
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(I)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.SIPUSH, 21294));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Write byte
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "ja", "c", "(II)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 115));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "da", "f", "Lja;"));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Create Packet
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(II)V", false));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 116));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Check if dialogue option is pressed
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IF_ICMPGE, label));
            // Menu option count
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "client", "Id", "I"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IFLT, label));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
        } else if (methodNode.name.equals("f") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Hide Roof option
                if (insnNode.getOpcode() == Opcodes.GETFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;

                    if (field.owner.equals("client") && field.name.equals("yj")) {
                        AbstractInsnNode nextNode = insnNode.getNext();
                        if (nextNode.getOpcode() == Opcodes.IFNE) {
                            LabelNode label = ((JumpInsnNode) nextNode).label;
                            methodNode.instructions.insert(nextNode, new JumpInsnNode(Opcodes.IFGT, label));
                            methodNode.instructions.insert(nextNode,
                                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "HIDE_ROOFS", "Z"));
                        }
                    }
                }

                // Move wilderness skull
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 465 || call.operand == 453) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IIZ)Z")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Move the load screen text dialogue
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 192) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 19));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("d") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Center logout dialogue
                if (insnNode.getOpcode() == Opcodes.SIPUSH || insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 173) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 126) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 130));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 137) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 36));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("j") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Center welcome box
                if (insnNode.getOpcode() == Opcodes.SIPUSH || insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 167) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 6));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 56) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 200));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == -87) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 169));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 426) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 170));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 106) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 150));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 406) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 150));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("k") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Save settings from combat menu
                if (insnNode.getOpcode() == Opcodes.PUTFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;

                    if (field.owner.equals("client") && field.name.equals("Fg")) {
                        methodNode.instructions.insert(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                                "Client/Settings", "save", "()V", false));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.PUTSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.GETFIELD, "client", "Fg", "I"));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc
                .equals("(ZLjava/lang/String;ILjava/lang/String;IILjava/lang/String;Ljava/lang/String;)V")) {
            AbstractInsnNode first = methodNode.instructions.getFirst();
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ALOAD, 7));
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ALOAD, 4));
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(first, new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client",
                    "messageHook", "(Ljava/lang/String;Ljava/lang/String;I)V"));
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private static void emitConst(MethodVisitor mv, int value) {
    if (value >= -1 && value <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + value);
    } else if ((byte) value == value) {
        mv.visitIntInsn(Opcodes.BIPUSH, value);
    } else if ((short) value == value) {
        mv.visitIntInsn(Opcodes.SIPUSH, value);
    } else {//from   ww  w. j  a  v a2s. c  om
        mv.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value/*from  ww  w .j av  a2 s  .c  om*/
 */
public static void getInt(MethodVisitor method, int value) {
    if (-1 <= value && value <= 5) {
        method.visitInsn(Opcodes.ICONST_0 + value);
    } else if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) {
        method.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) {
        method.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.codename1.tools.translator.BytecodeMethod.java

License:Open Source License

boolean optimize() {
    int instructionCount = instructions.size();

    // optimize away a method that only contains the void return instruction e.g. blank constructors etc.
    if (instructionCount < 6) {
        int realCount = instructionCount;
        Instruction actual = null;/*from  w  ww  .ja  va  2  s .  co  m*/
        for (int iter = 0; iter < instructionCount; iter++) {
            Instruction current = instructions.get(iter);
            if (current instanceof LabelInstruction) {
                realCount--;
                continue;
            }
            if (current instanceof LineNumber) {
                realCount--;
                continue;
            }
            actual = current;
        }

        if (realCount == 1 && actual != null && actual.getOpcode() == Opcodes.RETURN) {
            return false;
        }
    }

    boolean astoreCalls = false;
    boolean hasInstructions = false;

    boolean hasTryCatch = false;
    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current instanceof TryCatch) {
            hasTryCatch = true;
        }
        current.setMethod(this);
        if (current.isOptimized()) {
            continue;
        }
        int currentOpcode = current.getOpcode();
        switch (currentOpcode) {
        case Opcodes.CHECKCAST: {
            // Remove the check cast for now as it gets in the way of other optimizations
            instructions.remove(iter);
            iter--;
            instructionCount--;
            break;
        }
        }
    }

    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current.isOptimized()) {
            // This instruction has already been optimized
            // we should skip it and proceed to the next one
            continue;
        }
        Instruction next = instructions.get(iter + 1);

        int currentOpcode = current.getOpcode();
        int nextOpcode = next.getOpcode();

        if (ArithmeticExpression.isArithmeticOp(current)) {
            int addedIndex = ArithmeticExpression.tryReduce(instructions, iter);
            if (addedIndex >= 0) {
                iter = addedIndex;
                instructionCount = instructions.size();
                continue;
            }
        }

        if (current instanceof Field) {
            int newIter = Field.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
        }

        switch (currentOpcode) {

        case Opcodes.ARRAYLENGTH: {
            if (!dependentClasses.contains("java_lang_NullPointerException")) {
                dependentClasses.add("java_lang_NullPointerException");
            }
            int newIter = ArrayLengthExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                instructionCount = instructions.size();
                iter = newIter;
                continue;
            }
            break;
        }

        case Opcodes.DUP: {
            int newIter = DupExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        case Opcodes.POP: {
            if (iter > 0) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    if (inv.methodHasReturnValue()) {
                        inv.setNoReturn(true);
                        instructions.remove(iter);
                        iter--;
                        instructionCount--;
                        continue;
                    }
                }
            }
            break;
        }

        case Opcodes.ASTORE:
        case Opcodes.ISTORE:
        case Opcodes.DSTORE:
        case Opcodes.LSTORE:
        case Opcodes.FSTORE: {
            if (iter > 0 && current instanceof VarOp) {
                VarOp currentVarOp = (VarOp) current;
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(expr, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }

                } else if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(inv, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }
                }
            }

            break;
        }

        case Opcodes.IRETURN:
        case Opcodes.FRETURN:
        case Opcodes.ARETURN:
        case Opcodes.LRETURN:
        case Opcodes.DRETURN: {
            if (iter > 0 && current instanceof BasicInstruction) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (expr.assignTo(null, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        String exprString = sb.toString().trim();
                        String retVal = exprString;
                        sb.setLength(0);
                        if (!prev.isConstant()) {
                            sb.append("\n{\n    ");
                            switch (currentOpcode) {
                            case Opcodes.IRETURN:
                                sb.append("JAVA_INT");
                                break;
                            case Opcodes.FRETURN:
                                sb.append("JAVA_FLOAT");
                                break;
                            case Opcodes.ARETURN:
                                sb.append("JAVA_OBJECT");
                                break;
                            case Opcodes.LRETURN:
                                sb.append("JAVA_LONG");
                                break;
                            case Opcodes.DRETURN:
                                sb.append("JAVA_DOUBLE");
                                break;
                            }
                            sb.append(" ___returnValue=").append(exprString).append(";\n");
                            retVal = "___returnValue";
                        }
                        if (synchronizedMethod) {
                            if (staticMethod) {
                                sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                sb.append(getClsName());
                                sb.append(");\n");
                            } else {
                                sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                            }
                        }
                        if (hasTryCatch) {
                            sb.append(
                                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                    .append(retVal).append(";\n");
                        } else {
                            sb.append("    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                    .append(retVal).append(";\n");
                        }
                        if (!prev.isConstant()) {
                            sb.append("}\n");
                        }

                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter--;
                        instructionCount = instructions.size();
                        continue;

                    }
                } else if (prev instanceof CustomInvoke) {

                    CustomInvoke expr = (CustomInvoke) prev;
                    String returnType = expr.getReturnValue();
                    if (returnType != null && !"JAVA_OBJECT".equals(returnType)) {
                        // We can't safely return a JAVA_OBJECT directly because it needs to be added 
                        // to the stack for the GC
                        StringBuilder sb = new StringBuilder();
                        if (expr.appendExpression(sb)) {
                            instructions.remove(iter - 1);
                            instructions.remove(iter - 1);
                            String exprString = sb.toString().trim();
                            String retVal = exprString;
                            sb.setLength(0);
                            if (!expr.isConstant()) {

                                sb.append("\n{\n    ");
                                switch (currentOpcode) {
                                case Opcodes.IRETURN:
                                    sb.append("JAVA_INT");
                                    break;
                                case Opcodes.FRETURN:
                                    sb.append("JAVA_FLOAT");
                                    break;
                                case Opcodes.ARETURN:
                                    sb.append("JAVA_OBJECT");
                                    break;
                                case Opcodes.LRETURN:
                                    sb.append("JAVA_LONG");
                                    break;
                                case Opcodes.DRETURN:
                                    sb.append("JAVA_DOUBLE");
                                    break;
                                }

                                sb.append(" ___returnValue=").append(exprString).append(";\n");
                                retVal = "___returnValue";
                            }
                            if (synchronizedMethod) {
                                if (staticMethod) {
                                    sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                    sb.append(getClsName());
                                    sb.append(");\n");
                                } else {
                                    sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                                }
                            }
                            if (hasTryCatch) {
                                sb.append(
                                        "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                        .append(retVal).append(";\n");
                            } else {
                                sb.append(
                                        "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                        .append(retVal).append(";\n");
                            }
                            if (!expr.isConstant()) {
                                sb.append("}\n");
                            }

                            instructions.add(iter - 1,
                                    new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                            iter--;
                            instructionCount = instructions.size();
                            continue;

                        }
                    }
                }
            }
            break;
        }

        case Opcodes.BASTORE:
        case Opcodes.SASTORE:
        case Opcodes.CASTORE:
        case Opcodes.AASTORE:
        case Opcodes.IASTORE:
        case Opcodes.DASTORE:
        case Opcodes.LASTORE:
        case Opcodes.FASTORE: {
            if (iter > 2 && current instanceof BasicInstruction) {
                StringBuilder devNull = new StringBuilder();
                String arrayLiteral = null;
                String indexLiteral = null;
                String valueLiteral = null;
                Instruction prev3 = instructions.get(iter - 3);
                if (prev3 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev3).assignTo(null, devNull)) {
                        arrayLiteral = devNull.toString().trim();

                    }
                }
                devNull.setLength(0);
                Instruction prev2 = instructions.get(iter - 2);
                if (prev2 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev2).assignTo(null, devNull)) {
                        indexLiteral = devNull.toString().trim();
                    }
                }
                devNull.setLength(0);
                Instruction prev1 = instructions.get(iter - 1);

                if (prev1 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev1).assignTo(null, devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                } else if (prev1 instanceof CustomInvoke) {
                    devNull.setLength(0);
                    if (((CustomInvoke) prev1).appendExpression(devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                }

                if (arrayLiteral != null && indexLiteral != null && valueLiteral != null) {
                    String elementType = null;
                    switch (current.getOpcode()) {
                    case Opcodes.AASTORE:
                        elementType = "OBJECT";
                        break;
                    case Opcodes.IASTORE:
                        elementType = "INT";
                        break;
                    case Opcodes.DASTORE:
                        elementType = "DOUBLE";
                        break;

                    case Opcodes.LASTORE:
                        elementType = "LONG";
                        break;
                    case Opcodes.FASTORE:
                        elementType = "FLOAT";
                        break;
                    case Opcodes.CASTORE:
                        elementType = "CHAR";
                        break;
                    case Opcodes.BASTORE:
                        elementType = "BYTE";
                        break;
                    case Opcodes.SASTORE:
                        elementType = "SHORT";
                        break;

                    }
                    if (elementType == null) {
                        break;
                    }

                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    String code = "    CN1_SET_ARRAY_ELEMENT_" + elementType + "(" + arrayLiteral + ", "
                            + indexLiteral + ", " + valueLiteral + ");\n";
                    instructions.add(iter - 3, new CustomIntruction(code, code, dependentClasses));
                    iter = iter - 3;
                    instructionCount = instructions.size();
                    continue;
                }
            }

            break;
        }

        case Opcodes.FALOAD:
        case Opcodes.BALOAD:
        case Opcodes.IALOAD:
        case Opcodes.LALOAD:
        case Opcodes.DALOAD:
        case Opcodes.AALOAD:
        case Opcodes.SALOAD:
        case Opcodes.CALOAD: {
            int newIter = ArrayLoadExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        /* Try to optimize if statements that just use constants
           and local variables so that they don't need the intermediate
           push and pop from the stack.
        */
        case Opcodes.IF_ACMPEQ:
        case Opcodes.IF_ACMPNE:
        case Opcodes.IF_ICMPLE:
        case Opcodes.IF_ICMPLT:
        case Opcodes.IF_ICMPNE:
        case Opcodes.IF_ICMPGT:
        case Opcodes.IF_ICMPEQ:
        case Opcodes.IF_ICMPGE: {

            if (iter > 1) {
                Instruction leftArg = instructions.get(iter - 2);
                Instruction rightArg = instructions.get(iter - 1);

                String leftLiteral = null;
                String rightLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }
                if (rightArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) rightArg).assignTo(null, sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                } else if (rightArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) rightArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                }

                if (rightLiteral != null && leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    //instructions.remove(iter-2);
                    iter -= 2;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IF_ICMPLE:
                        operator = "<=";
                        opName = "IF_ICMPLE";
                        break;
                    case Opcodes.IF_ICMPLT:
                        operator = "<";
                        opName = "IF_IMPLT";
                        break;
                    case Opcodes.IF_ICMPNE:
                        operator = "!=";
                        opName = "IF_ICMPNE";
                        break;
                    case Opcodes.IF_ICMPGT:
                        operator = ">";
                        opName = "IF_ICMPGT";
                        break;
                    case Opcodes.IF_ICMPGE:
                        operator = ">=";
                        opName = "IF_ICMPGE";
                        break;
                    case Opcodes.IF_ICMPEQ:
                        operator = "==";
                        opName = "IF_ICMPEQ";
                        break;
                    case Opcodes.IF_ACMPEQ:
                        operator = "==";
                        opName = "IF_ACMPEQ";
                        break;
                    case Opcodes.IF_ACMPNE:
                        operator = "!=";
                        opName = "IF_ACMPNE";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightLiteral).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }
        case Opcodes.IFNONNULL:
        case Opcodes.IFNULL:

        case Opcodes.IFLE:
        case Opcodes.IFLT:
        case Opcodes.IFNE:
        case Opcodes.IFGT:
        case Opcodes.IFEQ:
        case Opcodes.IFGE: {
            String rightArg = "0";
            if (currentOpcode == Opcodes.IFNONNULL || currentOpcode == Opcodes.IFNULL) {
                rightArg = "JAVA_NULL";
            }
            if (iter > 0) {
                Instruction leftArg = instructions.get(iter - 1);

                String leftLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }

                if (leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 1);
                    instructions.remove(iter - 1);
                    //instructions.remove(iter-2);
                    iter -= 1;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IFLE:
                        operator = "<=";
                        opName = "IFLE";
                        break;
                    case Opcodes.IFLT:
                        operator = "<";
                        opName = "IFLT";
                        break;
                    case Opcodes.IFNE:
                        operator = "!=";
                        opName = "IFNE";
                        break;
                    case Opcodes.IFGT:
                        operator = ">";
                        opName = "IFGT";
                        break;
                    case Opcodes.IFGE:
                        operator = ">=";
                        opName = "IFGE";
                        break;
                    case Opcodes.IFEQ:
                        operator = "==";
                        opName = "IFEQ";
                        break;
                    case Opcodes.IFNULL:
                        operator = "==";
                        opName = "IFNULL";
                        break;
                    case Opcodes.IFNONNULL:
                        operator = "!=";
                        opName = "IFNONNULL";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightArg).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }

        case Opcodes.INVOKEVIRTUAL:
        case Opcodes.INVOKESTATIC:
        case Opcodes.INVOKESPECIAL:
        case Opcodes.INVOKEINTERFACE: {
            if (current instanceof Invoke) {
                Invoke inv = (Invoke) current;
                List<ByteCodeMethodArg> invocationArgs = inv.getArgs();
                int numArgs = invocationArgs.size();

                //if (current.getOpcode() != Opcodes.INVOKESTATIC) {
                //    numArgs++;
                //}
                if (iter >= numArgs) {
                    String[] argLiterals = new String[numArgs];
                    StringBuilder devNull = new StringBuilder();
                    for (int i = 0; i < numArgs; i++) {
                        devNull.setLength(0);
                        Instruction instr = instructions.get(iter - numArgs + i);
                        if (instr instanceof AssignableExpression
                                && ((AssignableExpression) instr).assignTo(null, devNull)) {
                            argLiterals[i] = devNull.toString().trim();
                        } else if (instr instanceof CustomInvoke) {
                            CustomInvoke cinv = (CustomInvoke) instr;
                            devNull.setLength(0);
                            if (!"JAVA_OBJECT".equals(cinv.getReturnValue())
                                    && cinv.appendExpression(devNull)) {
                                // We can't add invocations that return objects directly
                                // because they need to be added to the stack for GC
                                argLiterals[i] = devNull.toString().trim();
                            }
                        } else if (instr instanceof ArithmeticExpression) {
                            argLiterals[i] = ((ArithmeticExpression) instr).getExpressionAsString().trim();
                        } else if (instr instanceof VarOp) {
                            VarOp var = (VarOp) instr;
                            switch (instr.getOpcode()) {
                            case Opcodes.ALOAD: {
                                if (!isStatic() && var.getIndex() == 0) {
                                    argLiterals[i] = "__cn1ThisObject";
                                } else {
                                    argLiterals[i] = "locals[" + var.getIndex() + "].data.o";
                                }
                                break;
                            }
                            case Opcodes.ILOAD: {
                                argLiterals[i] = "ilocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }
                            case Opcodes.DLOAD: {
                                argLiterals[i] = "dlocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.FLOAD: {
                                argLiterals[i] = "flocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.LLOAD: {
                                argLiterals[i] = "llocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH:
                            case Opcodes.SIPUSH: {
                                argLiterals[i] = String.valueOf(var.getIndex());

                                break;
                            }
                            }
                        } else {
                            switch (instr.getOpcode()) {

                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }

                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH: {
                                if (instr instanceof BasicInstruction) {
                                    argLiterals[i] = String.valueOf(((BasicInstruction) instr).getValue());
                                }
                                break;
                            }
                            case Opcodes.LDC: {
                                if (instr instanceof Ldc) {
                                    Ldc ldc = (Ldc) instr;
                                    argLiterals[i] = ldc.getValueAsString();

                                }
                                break;
                            }

                            }

                        }
                    }

                    // Check to make sure that we have all the args as literals.
                    boolean missingLiteral = false;
                    for (String lit : argLiterals) {
                        if (lit == null) {
                            missingLiteral = true;
                            break;
                        }
                    }

                    // We have all of the arguments as literals.  Let's
                    // add them to our invoke instruction.
                    if (!missingLiteral) {
                        CustomInvoke newInvoke = CustomInvoke.create(inv);
                        instructions.remove(iter);
                        instructions.add(iter, newInvoke);
                        int newIter = iter;
                        for (int i = 0; i < numArgs; i++) {
                            instructions.remove(iter - numArgs);
                            newIter--;
                            newInvoke.setLiteralArg(i, argLiterals[i]);
                        }
                        if (inv.getOpcode() != Opcodes.INVOKESTATIC) {
                            Instruction ldTarget = instructions.get(iter - numArgs - 1);
                            if (ldTarget instanceof AssignableExpression) {
                                StringBuilder targetExprStr = new StringBuilder();
                                if (((AssignableExpression) ldTarget).assignTo(null, targetExprStr)) {
                                    newInvoke.setTargetObjectLiteral(targetExprStr.toString().trim());
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;

                                }

                            } else if (ldTarget instanceof CustomInvoke) {
                                // WE Can't pass a custom invoke as the target directly
                                // because it the return value needs to be added to the 
                                // stack for the GC
                            } else {
                                switch (ldTarget.getOpcode()) {
                                case Opcodes.ALOAD: {
                                    VarOp v = (VarOp) ldTarget;
                                    if (isStatic() && v.getIndex() == 0) {
                                        newInvoke.setTargetObjectLiteral("__cn1ThisObject");
                                    } else {
                                        newInvoke.setTargetObjectLiteral("locals[" + v.getIndex() + "].data.o");
                                    }
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;
                                    break;
                                }
                                }
                            }
                        }

                        newInvoke.setOptimized(true);
                        //iter = 0;
                        instructionCount = instructions.size();
                        iter = newIter;

                    }
                }
            }
            break;
        }

        }
        astoreCalls = astoreCalls || currentOpcode == Opcodes.ASTORE || currentOpcode == Opcodes.ISTORE
                || currentOpcode == Opcodes.LSTORE || currentOpcode == Opcodes.DSTORE
                || currentOpcode == Opcodes.FSTORE;

        hasInstructions = hasInstructions | current.getOpcode() != -1;
    }
    return hasInstructions;
}

From source file:com.codename1.tools.translator.bytecodes.ArithmeticExpression.java

public static boolean isArg(Instruction instr) {
    if (instr instanceof ArithmeticExpression) {
        return true;
    }/* w ww . ja  va 2s .c  om*/
    if (instr instanceof AssignableExpression) {
        StringBuilder dummy = new StringBuilder();

        if (((AssignableExpression) instr).assignTo(null, dummy)) {
            return true;
        }
    }
    int opcode = instr.getOpcode();
    switch (opcode) {

    case Opcodes.FLOAD:
    case Opcodes.DLOAD:
    case Opcodes.ILOAD:
    case Opcodes.LLOAD:
    case org.objectweb.asm.Opcodes.ICONST_0:
    case org.objectweb.asm.Opcodes.ICONST_1:
    case org.objectweb.asm.Opcodes.ICONST_2:
    case org.objectweb.asm.Opcodes.ICONST_3:
    case org.objectweb.asm.Opcodes.ICONST_4:
    case org.objectweb.asm.Opcodes.ICONST_5:
    case org.objectweb.asm.Opcodes.ICONST_M1:
    case org.objectweb.asm.Opcodes.LCONST_0:
    case org.objectweb.asm.Opcodes.LCONST_1:
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
    case org.objectweb.asm.Opcodes.BIPUSH:
    case org.objectweb.asm.Opcodes.SIPUSH:
    case Opcodes.LDC:
        return true;
    }
    return false;
}