Example usage for org.objectweb.asm Opcodes POP2

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

Introduction

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

Prototype

int POP2

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

Click Source Link

Usage

From source file:de.fhkoeln.gm.cui.javahardener.CheckNullMethodVisitor.java

License:Open Source License

/**
 * Surrounds the original invoke call with an IFNULL check.
 * If the object is null a default value will be pushed to the stack.
 * /*from   w w  w.  ja  v  a 2s  . co m*/
 * This method duplicates the current object reference with DUP2 and POP (to
 * remove the argument from the stack) before checking it with IFNULL.
 * The original instance reference and the argument will be removed with
 * POP2 before {@link #pushDefault(Type) the default value will pushed}.
 * 
 * Notice: This works only for 32 bit arguments: Objects, integers, etc.
 * but doesn't work with long or double values.
 * 
 * @param opcode
 * @param owner
 * @param name
 * @param desc
 */
private void invokeMethodWithOneArgument(int opcode, String owner, String name, String desc) {
    // Instead of the original invoke call:
    //super.visitMethodInsn(opcode, owner, name, desc);

    Label fallback = new Label();
    Label behind = new Label();

    // We surround the original call with an IFNULL check:
    super.visitInsn(Opcodes.DUP2); // Duplicate stack pointer of the current object AND the argument
    super.visitInsn(Opcodes.POP); // Remove the argument again
    super.visitJumpInsn(Opcodes.IFNULL, fallback); // Skip method call if reference is null
    super.visitMethodInsn(opcode, owner, name, desc); // Original method call
    super.visitJumpInsn(Opcodes.GOTO, behind); // Jump over the reference is null path

    // But if not we need add a default value to the stack:
    super.visitLabel(fallback); // Reference is null path
    super.visitInsn(Opcodes.POP2); // Pop the dup value from stack AND the argument
    pushDefault(Type.getReturnType(desc));

    super.visitLabel(behind); // Label to jump of the reference is null path
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.arithmetic.ArithmeticReplaceMethodAdapter.java

License:Open Source License

@Override
protected void handleMutation(Mutation mutation, int opcode) {
    logger.debug("Querying mutation " + mutation);
    List<Mutation> mutations = QueryManager.getMutations(className.replace('/', '.'), methodName + desc,
            mutation.getLineNumber(), mutation.getMutationForLine(), MutationType.ARITHMETIC_REPLACE);
    MutationCode unMutated = new SingleInsnMutationCode(null, opcode);
    List<MutationCode> mutationCode = new ArrayList<MutationCode>();
    for (Mutation m : mutations) {
        if (mutationManager.shouldApplyMutation(m)) {
            final int add = Integer.parseInt(m.getOperatorAddInfo());
            MutationCode mutated = null;
            if (add == REMOVE_LEFT_VALUE_SINGLE || add == REMOVE_RIGHT_VALUE_SINGLE) {

                mutated = new MutationCode(m) {

                    @Override//from   ww w. j  a  v  a2  s.c om
                    public void insertCodeBlock(MethodVisitor mv) {
                        if (add == REMOVE_LEFT_VALUE_SINGLE) {
                            mv.visitInsn(Opcodes.SWAP);
                        }
                        mv.visitInsn(Opcodes.POP);
                    }
                };
            } else if (add == REMOVE_LEFT_VALUE_DOUBLE || add == REMOVE_RIGHT_VALUE_DOUBLE) {
                mutated = new MutationCode(m) {

                    @Override
                    public void insertCodeBlock(MethodVisitor mv) {
                        if (add == REMOVE_LEFT_VALUE_DOUBLE) {
                            mv.visitInsn(Opcodes.DUP2_X2);
                            mv.visitInsn(Opcodes.POP2);
                            mv.visitInsn(Opcodes.POP2);
                        } else {
                            mv.visitInsn(Opcodes.POP2);
                        }
                    }
                };
            } else {
                mutated = new SingleInsnMutationCode(m, Integer.parseInt(m.getOperatorAddInfo()));
            }
            mutationCode.add(mutated);
        }
    }
    if (mutationCode.size() > 0) {
        BytecodeTasks.insertIfElse(mv, unMutated, mutationCode.toArray(new MutationCode[0]));
    } else {
        mv.visitInsn(opcode);
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.negateJumps.NegateJumpsMethodAdapter.java

License:Open Source License

@Override
protected void handleMutation(Mutation mutation, final Label label, final int opcode) {

    logger.debug("Applying mutation for line: " + getLineNumber());
    MutationCode unMutated = new MutationCode(null) {
        @Override//  w  ww. j a  v  a  2  s . c o  m
        public void insertCodeBlock(MethodVisitor mv) {
            mv.visitJumpInsn(opcode, label);
        }

    };
    List<Mutation> mutations = QueryManager.getMutations(mutation.getClassName(), mutation.getMethodName(),
            mutation.getLineNumber(), mutation.getMutationForLine(), MutationType.NEGATE_JUMP);
    List<MutationCode> mutationCode = new ArrayList<MutationCode>();
    for (final Mutation m : mutations) {
        if (mutationManager.shouldApplyMutation(m)) {
            MutationCode mutated = new MutationCode(m) {
                @Override
                public void insertCodeBlock(MethodVisitor mv) {
                    int insertOpcode = Integer.parseInt(m.getOperatorAddInfo());
                    if (insertOpcode == POP_ONCE_TRUE) {
                        mv.visitInsn(Opcodes.POP);
                        mv.visitJumpInsn(Opcodes.GOTO, label);
                    } else if (insertOpcode == POP_ONCE_FALSE) {
                        mv.visitInsn(Opcodes.POP);
                    } else if (insertOpcode == POP_TWICE_TRUE) {
                        mv.visitInsn(Opcodes.POP2);
                        mv.visitJumpInsn(Opcodes.GOTO, label);
                    } else if (insertOpcode == POP_TWICE_FALSE) {
                        mv.visitInsn(Opcodes.POP2);
                    } else {
                        mv.visitJumpInsn(insertOpcode, label);
                    }
                }
            };
            mutationCode.add(mutated);
        }
    }
    if (mutationCode.size() > 0) {
        BytecodeTasks.insertIfElse(mv, unMutated, mutationCode.toArray(new MutationCode[0]));
    } else {
        mv.visitJumpInsn(opcode, label);
    }
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.SimpleInstruction.java

License:Open Source License

@Override
public String toString() {
    switch (getOpcode()) {
    // the not interesting ones:
    case Opcodes.NOP:
        return "NOP";
    // constants:
    case Opcodes.ACONST_NULL:
        return "ACONST_NULL";
    case Opcodes.ICONST_M1:
        return "ICONST_M1";
    case Opcodes.ICONST_0:
        return "ICONST_0";
    case Opcodes.ICONST_1:
        return "ICONST_1";
    case Opcodes.ICONST_2:
        return "ICONST_2";
    case Opcodes.ICONST_3:
        return "ICONST_3";
    case Opcodes.ICONST_4:
        return "ICONST_4";
    case Opcodes.ICONST_5:
        return "ICONST_5";
    case Opcodes.LCONST_0:
        return "LCONST_0";
    case Opcodes.LCONST_1:
        return "LCONST_1";
    case Opcodes.FCONST_0:
        return "FCONST_0";
    case Opcodes.FCONST_1:
        return "FCONST_1";
    case Opcodes.FCONST_2:
        return "FCONST_2";
    case Opcodes.DCONST_0:
        return "DCONST_0";
    case Opcodes.DCONST_1:
        return "DCONST_1";

    // array load:
    case Opcodes.IALOAD:
        return "IALOAD";
    case Opcodes.LALOAD:
        return "LALOAD";
    case Opcodes.FALOAD:
        return "FALOAD";
    case Opcodes.DALOAD:
        return "DALOAD";
    case Opcodes.AALOAD:
        return "AALOAD";
    case Opcodes.BALOAD:
        return "BALOAD";
    case Opcodes.CALOAD:
        return "CALOAD";
    case Opcodes.SALOAD:
        return "SALOAD";

    // array store:
    case Opcodes.IASTORE:
        return "IASTORE";
    case Opcodes.LASTORE:
        return "LASTORE";
    case Opcodes.FASTORE:
        return "FASTORE";
    case Opcodes.DASTORE:
        return "DASTORE";
    case Opcodes.AASTORE:
        return "AASTORE";
    case Opcodes.BASTORE:
        return "BASTORE";
    case Opcodes.CASTORE:
        return "CASTORE";
    case Opcodes.SASTORE:
        return "SASTORE";

    // stack manipulation:
    case Opcodes.POP:
        return "POP";
    case Opcodes.POP2:
        return "POP2";
    case Opcodes.DUP:
        return "DUP";
    case Opcodes.DUP_X1:
        return "DUP_X1";
    case Opcodes.DUP_X2:
        return "DUP_X2";
    case Opcodes.DUP2:
        return "DUP2";
    case Opcodes.DUP2_X1:
        return "DUP2_X1";
    case Opcodes.DUP2_X2:
        return "DUP2_X2";
    case Opcodes.SWAP:
        return "SWAP";

    // arithmetic:
    case Opcodes.IADD:
        return "IADD";
    case Opcodes.LADD:
        return "LADD";
    case Opcodes.FADD:
        return "FADD";
    case Opcodes.DADD:
        return "DADD";
    case Opcodes.ISUB:
        return "ISUB";
    case Opcodes.LSUB:
        return "LSUB";
    case Opcodes.FSUB:
        return "FSUB";
    case Opcodes.DSUB:
        return "DSUB";
    case Opcodes.IMUL:
        return "IMUL";
    case Opcodes.LMUL:
        return "LMUL";
    case Opcodes.FMUL:
        return "FMUL";
    case Opcodes.DMUL:
        return "DMUL";
    case Opcodes.IDIV:
        return "IDIV";
    case Opcodes.LDIV:
        return "LDIV";
    case Opcodes.FDIV:
        return "FDIV";
    case Opcodes.DDIV:
        return "DDIV";
    case Opcodes.IREM:
        return "IREM";
    case Opcodes.LREM:
        return "LREM";
    case Opcodes.FREM:
        return "FREM";
    case Opcodes.DREM:
        return "DREM";
    case Opcodes.INEG:
        return "INEG";
    case Opcodes.LNEG:
        return "LNEG";
    case Opcodes.FNEG:
        return "FNEG";
    case Opcodes.DNEG:
        return "DNEG";
    case Opcodes.ISHL:
        return "ISHL";
    case Opcodes.LSHL:
        return "LSHL";
    case Opcodes.ISHR:
        return "ISHR";
    case Opcodes.LSHR:
        return "LSHR";
    case Opcodes.IUSHR:
        return "IUSHR";
    case Opcodes.LUSHR:
        return "LUSHR";
    case Opcodes.IAND:
        return "IAND";
    case Opcodes.LAND:
        return "LAND";
    case Opcodes.IOR:
        return "IOR";
    case Opcodes.LOR:
        return "LOR";
    case Opcodes.IXOR:
        return "IXOR";
    case Opcodes.LXOR:
        return "LXOR";

    // type conversions:
    case Opcodes.I2L:
        return "I2L";
    case Opcodes.I2F:
        return "I2F";
    case Opcodes.I2D:
        return "I2D";
    case Opcodes.L2I:
        return "L2I";
    case Opcodes.L2F:
        return "L2F";
    case Opcodes.L2D:
        return "L2D";
    case Opcodes.F2I:
        return "F2I";
    case Opcodes.F2L:
        return "F2L";
    case Opcodes.F2D:
        return "F2D";
    case Opcodes.D2I:
        return "D2I";
    case Opcodes.D2L:
        return "D2L";
    case Opcodes.D2F:
        return "D2F";
    case Opcodes.I2B:
        return "I2B";
    case Opcodes.I2C:
        return "I2C";
    case Opcodes.I2S:
        return "I2S";

    // comparison:
    case Opcodes.LCMP:
        return "LCMP";
    case Opcodes.FCMPL:
        return "FCMPL";
    case Opcodes.FCMPG:
        return "FCMPG";
    case Opcodes.DCMPL:
        return "DCMPL";
    case Opcodes.DCMPG:
        return "DCMPG";

    // control-flow statements:
    case Opcodes.IRETURN:
        return "IRETURN";
    case Opcodes.LRETURN:
        return "LRETURN";
    case Opcodes.FRETURN:
        return "FRETURN";
    case Opcodes.DRETURN:
        return "DRETURN";
    case Opcodes.ARETURN:
        return "ARETURN";
    case Opcodes.RETURN:
        return "RETURN";

    // special things
    case Opcodes.ARRAYLENGTH:
        return "ARRAYLENGTH";
    case Opcodes.ATHROW:
        return "ATHROW";
    case Opcodes.MONITORENTER:
        return "MONITORENTER";
    case Opcodes.MONITOREXIT:
        return "MONITOREXIT";

    default://w ww  . j  a  v  a 2  s.com
        assert false;
        return "--ERROR--";
    }
}

From source file:dyco4j.instrumentation.internals.InitTracingMethodVisitor.java

License:BSD License

@Override
public void visitInsn(final int opcode) {
    super.visitInsn(opcode);
    switch (opcode) {
    case Opcodes.IRETURN: // 1 before n/a after
    case Opcodes.FRETURN: // 1 before n/a after
    case Opcodes.ARETURN: // 1 before n/a after
    case Opcodes.ATHROW: // 1 before n/a after
        this.stackFrame.pop();
        break;//from www .j  a v  a  2  s .  c  om
    case Opcodes.LRETURN: // 2 before n/a after
    case Opcodes.DRETURN: // 2 before n/a after
        this.stackFrame.pop();
        this.stackFrame.pop();
        break;
    case Opcodes.NOP:
    case Opcodes.LALOAD: // remove 2 add 2
    case Opcodes.DALOAD: // remove 2 add 2
    case Opcodes.LNEG:
    case Opcodes.DNEG:
    case Opcodes.FNEG:
    case Opcodes.INEG:
    case Opcodes.L2D:
    case Opcodes.D2L:
    case Opcodes.F2I:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.I2F:
    case Opcodes.ARRAYLENGTH:
        break;
    case Opcodes.ACONST_NULL:
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
    case Opcodes.F2L: // 1 before 2 after
    case Opcodes.F2D:
    case Opcodes.I2L:
    case Opcodes.I2D:
        this.stackFrame.push(OTHER);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        this.stackFrame.push(OTHER);
        this.stackFrame.push(OTHER);
        break;
    case Opcodes.IALOAD: // remove 2 add 1
    case Opcodes.FALOAD: // remove 2 add 1
    case Opcodes.AALOAD: // remove 2 add 1
    case Opcodes.BALOAD: // remove 2 add 1
    case Opcodes.CALOAD: // remove 2 add 1
    case Opcodes.SALOAD: // remove 2 add 1
    case Opcodes.POP:
    case Opcodes.IADD:
    case Opcodes.FADD:
    case Opcodes.ISUB:
    case Opcodes.LSHL: // 3 before 2 after
    case Opcodes.LSHR: // 3 before 2 after
    case Opcodes.LUSHR: // 3 before 2 after
    case Opcodes.L2I: // 2 before 1 after
    case Opcodes.L2F: // 2 before 1 after
    case Opcodes.D2I: // 2 before 1 after
    case Opcodes.D2F: // 2 before 1 after
    case Opcodes.FSUB:
    case Opcodes.FMUL:
    case Opcodes.FDIV:
    case Opcodes.FREM:
    case Opcodes.FCMPL: // 2 before 1 after
    case Opcodes.FCMPG: // 2 before 1 after
    case Opcodes.IMUL:
    case Opcodes.IDIV:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
    case Opcodes.IAND:
    case Opcodes.IOR:
    case Opcodes.IXOR:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        this.stackFrame.pop();
        break;
    case Opcodes.POP2:
    case Opcodes.LSUB:
    case Opcodes.LMUL:
    case Opcodes.LDIV:
    case Opcodes.LREM:
    case Opcodes.LADD:
    case Opcodes.LAND:
    case Opcodes.LOR:
    case Opcodes.LXOR:
    case Opcodes.DADD:
    case Opcodes.DMUL:
    case Opcodes.DSUB:
    case Opcodes.DDIV:
    case Opcodes.DREM:
        this.stackFrame.pop();
        this.stackFrame.pop();
        break;
    case Opcodes.IASTORE:
    case Opcodes.FASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
    case Opcodes.LCMP: // 4 before 1 after
    case Opcodes.DCMPL:
    case Opcodes.DCMPG:
        this.stackFrame.pop();
        this.stackFrame.pop();
        this.stackFrame.pop();
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        this.stackFrame.pop();
        this.stackFrame.pop();
        this.stackFrame.pop();
        this.stackFrame.pop();
        break;
    case Opcodes.DUP:
        this.stackFrame.push(this.stackFrame.peek());
        break;
    case Opcodes.DUP_X1: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 2, stackFrame.get(_s - 1));
        break;
    }
    case Opcodes.DUP_X2: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 3, stackFrame.get(_s - 1));
        break;
    }
    case Opcodes.DUP2: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 2, stackFrame.get(_s - 1));
        stackFrame.add(_s - 2, stackFrame.get(_s - 1));
        break;
    }
    case Opcodes.DUP2_X1: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 3, stackFrame.get(_s - 1));
        stackFrame.add(_s - 3, stackFrame.get(_s - 1));
        break;
    }
    case Opcodes.DUP2_X2: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 4, stackFrame.get(_s - 1));
        stackFrame.add(_s - 4, stackFrame.get(_s - 1));
        break;
    }
    case Opcodes.SWAP: {
        final int _s = stackFrame.size();
        stackFrame.add(_s - 2, stackFrame.get(_s - 1));
        stackFrame.remove(_s);
        break;
    }
    }
}

From source file:dyco4j.instrumentation.internals.TracingMethodVisitor.java

License:BSD License

private void visitArrayStoreInsn(final int opcode) {
    if (opcode == Opcodes.LASTORE || opcode == Opcodes.DASTORE) {
        super.visitInsn(Opcodes.DUP2_X2);
        super.visitInsn(Opcodes.POP2);
        super.visitInsn(Opcodes.DUP2_X2);
        super.visitInsn(Opcodes.DUP2_X2);
        super.visitInsn(Opcodes.POP2);
        super.visitInsn(Opcodes.DUP2_X2);
    } else {/*  w  ww.j av  a 2  s  .  c  om*/
        super.visitInsn(Opcodes.DUP_X2);
        super.visitInsn(Opcodes.POP);
        super.visitInsn(Opcodes.DUP2_X1);
        super.visitInsn(Opcodes.DUP2_X1);
        super.visitInsn(Opcodes.POP2);
        super.visitInsn(Opcodes.DUP_X2);
    }

    switch (opcode) {
    case Opcodes.AASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.getObjectType("java/lang/Object"));
        break;
    case Opcodes.BASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.BYTE_TYPE);
        break;
    case Opcodes.CASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.CHAR_TYPE);
        break;
    case Opcodes.FASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.FLOAT_TYPE);
        break;
    case Opcodes.IASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.INT_TYPE);
        break;
    case Opcodes.SASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.SHORT_TYPE);
        break;
    case Opcodes.DASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.DOUBLE_TYPE);
        break;
    case Opcodes.LASTORE:
        LoggingHelper.emitConvertToString(this.mv, Type.LONG_TYPE);
        break;
    }

    LoggingHelper.emitLogArray(this.mv, Logger.ArrayAction.PUTA);

    super.visitInsn(opcode);
}

From source file:dyco4j.instrumentation.LoggingHelper.java

License:BSD License

public static void emitSwapTwoWordsAndOneWord(final MethodVisitor mv, final Type tos) {
    if (tos.getSort() == Type.LONG || tos.getSort() == Type.DOUBLE) {
        mv.visitInsn(Opcodes.DUP2_X1);// w  w  w  . j av a  2 s. com
        mv.visitInsn(Opcodes.POP2);
    } else {
        mv.visitInsn(Opcodes.SWAP);
    }
}

From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java

License:Open Source License

private void interpret(InsnNode insn, FrameState frame, BBInfo block) {
    ReturnType returnType = block.block.getParent().getType().getReturnType();
    switch (insn.getOpcode()) {
    case Opcodes.NOP:
        break;/*from  w w  w . j  a  v  a 2 s. c om*/
    //<editor-fold defaultstate="collapsed" desc="Stack manipulation opcodes (pop, dup, swap)">
    case Opcodes.POP:
        assert frame.stack.peek().getType().getCategory() == 1;
        frame.stack.pop();
        break;
    case Opcodes.POP2:
        final int[][][] pop2Permutations = { { { 1, 1 }, {} }, { { 2 }, {} } };
        conditionallyPermute(frame, pop2Permutations);
        break;
    case Opcodes.DUP:
        final int[][][] dupPermutations = { { { 1 }, { 1, 1 } } };
        conditionallyPermute(frame, dupPermutations);
        break;
    case Opcodes.DUP_X1:
        final int[][][] dup_x1Permutations = { { { 1, 1 }, { 1, 2, 1 } } };
        conditionallyPermute(frame, dup_x1Permutations);
        break;
    case Opcodes.DUP_X2:
        final int[][][] dup_x2Permutations = { { { 1, 1, 1 }, { 1, 3, 2, 1 } }, { { 1, 2 }, { 1, 2, 1 } } };
        conditionallyPermute(frame, dup_x2Permutations);
        break;
    case Opcodes.DUP2:
        final int[][][] dup2Permutations = { { { 1, 1 }, { 2, 1, 2, 1 } }, { { 2 }, { 1, 1 } } };
        conditionallyPermute(frame, dup2Permutations);
        break;
    case Opcodes.DUP2_X1:
        final int[][][] dup2_x1Permutations = { { { 1, 1, 1 }, { 2, 1, 3, 2, 1 } }, { { 2, 1 }, { 1, 2, 1 } } };
        conditionallyPermute(frame, dup2_x1Permutations);
        break;
    case Opcodes.DUP2_X2:
        final int[][][] dup2_x2Permutations = { { { 1, 1, 1, 1 }, { 2, 1, 4, 3, 2, 1 } },
                { { 2, 1, 1 }, { 1, 3, 2, 1 } }, { { 3, 2, 1 }, { 2, 1, 3, 2, 1 } },
                { { 2, 2 }, { 1, 2, 1 } } };
        conditionallyPermute(frame, dup2_x2Permutations);
        break;
    case Opcodes.SWAP:
        final int[][][] swapPermutations = { { { 1, 1 }, { 1, 2 } } };
        conditionallyPermute(frame, swapPermutations);
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constant-stacking opcodes (iconst_0, etc.; see also bipush, sipush)">
    case Opcodes.ACONST_NULL:
        frame.stack.push(module.constants().getNullConstant());
        break;
    case Opcodes.ICONST_M1:
        frame.stack.push(module.constants().getSmallestIntConstant(-1));
        break;
    case Opcodes.ICONST_0:
        frame.stack.push(module.constants().getSmallestIntConstant(0));
        break;
    case Opcodes.ICONST_1:
        frame.stack.push(module.constants().getSmallestIntConstant(1));
        break;
    case Opcodes.ICONST_2:
        frame.stack.push(module.constants().getSmallestIntConstant(2));
        break;
    case Opcodes.ICONST_3:
        frame.stack.push(module.constants().getSmallestIntConstant(3));
        break;
    case Opcodes.ICONST_4:
        frame.stack.push(module.constants().getSmallestIntConstant(4));
        break;
    case Opcodes.ICONST_5:
        frame.stack.push(module.constants().getSmallestIntConstant(5));
        break;
    case Opcodes.LCONST_0:
        frame.stack.push(module.constants().getConstant(0L));
        break;
    case Opcodes.LCONST_1:
        frame.stack.push(module.constants().getConstant(1L));
        break;
    case Opcodes.FCONST_0:
        frame.stack.push(module.constants().getConstant(0f));
        break;
    case Opcodes.FCONST_1:
        frame.stack.push(module.constants().getConstant(1f));
        break;
    case Opcodes.FCONST_2:
        frame.stack.push(module.constants().getConstant(2f));
        break;
    case Opcodes.DCONST_0:
        frame.stack.push(module.constants().getConstant(0d));
        break;
    case Opcodes.DCONST_1:
        frame.stack.push(module.constants().getConstant(1d));
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Return opcodes">
    case Opcodes.IRETURN:
        assert returnType.isSubtypeOf(typeFactory.getType(int.class));
        assert frame.stack.peek().getType().isSubtypeOf(returnType);
        block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop()));
        break;
    case Opcodes.LRETURN:
        assert returnType.isSubtypeOf(typeFactory.getType(long.class));
        assert frame.stack.peek().getType().isSubtypeOf(returnType);
        block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop()));
        break;
    case Opcodes.FRETURN:
        assert returnType.isSubtypeOf(typeFactory.getType(float.class));
        assert frame.stack.peek().getType().isSubtypeOf(returnType);
        block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop()));
        break;
    case Opcodes.DRETURN:
        assert returnType.isSubtypeOf(typeFactory.getType(double.class));
        assert frame.stack.peek().getType().isSubtypeOf(returnType);
        block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop()));
        break;
    case Opcodes.ARETURN:
        assert returnType.isSubtypeOf(typeFactory.getType(Object.class));
        assert frame.stack.peek().getType().isSubtypeOf(returnType);
        block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop()));
        break;
    case Opcodes.RETURN:
        assert returnType instanceof VoidType || method.isConstructor();
        block.block.instructions().add(new ReturnInst(typeFactory.getVoidType()));
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Unary math opcodes (negation)">
    //Unary minus is rendered as a multiplication by -1.  (The obvious
    //other choice, subtraction from 0, is not equivalent for floats and
    //doubles due to negative zero.)
    case Opcodes.INEG:
        frame.stack.push(module.constants().getSmallestIntConstant(-1));
        binary(BinaryInst.Operation.MUL, frame, block);
        break;
    case Opcodes.LNEG:
        frame.stack.push(module.constants().getConstant(-1L));
        binary(BinaryInst.Operation.MUL, frame, block);
        break;
    case Opcodes.FNEG:
        frame.stack.push(module.constants().getConstant(-1f));
        binary(BinaryInst.Operation.MUL, frame, block);
        break;
    case Opcodes.DNEG:
        frame.stack.push(module.constants().getConstant(-1d));
        binary(BinaryInst.Operation.MUL, frame, block);
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Binary math opcodes">
    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
        binary(BinaryInst.Operation.ADD, frame, block);
        break;
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
        binary(BinaryInst.Operation.SUB, frame, block);
        break;
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
        binary(BinaryInst.Operation.MUL, frame, block);
        break;
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
        binary(BinaryInst.Operation.DIV, frame, block);
        break;
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
        binary(BinaryInst.Operation.REM, frame, block);
        break;
    case Opcodes.ISHL:
    case Opcodes.LSHL:
        binary(BinaryInst.Operation.SHL, frame, block);
        break;
    case Opcodes.ISHR:
    case Opcodes.LSHR:
        binary(BinaryInst.Operation.SHR, frame, block);
        break;
    case Opcodes.IUSHR:
    case Opcodes.LUSHR:
        binary(BinaryInst.Operation.USHR, frame, block);
        break;
    case Opcodes.IAND:
    case Opcodes.LAND:
        binary(BinaryInst.Operation.AND, frame, block);
        break;
    case Opcodes.IOR:
    case Opcodes.LOR:
        binary(BinaryInst.Operation.OR, frame, block);
        break;
    case Opcodes.IXOR:
    case Opcodes.LXOR:
        binary(BinaryInst.Operation.XOR, frame, block);
        break;
    case Opcodes.LCMP:
    case Opcodes.FCMPL:
    case Opcodes.DCMPL:
        binary(BinaryInst.Operation.CMP, frame, block);
        break;
    case Opcodes.FCMPG:
    case Opcodes.DCMPG:
        binary(BinaryInst.Operation.CMPG, frame, block);
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Primitive casts">
    case Opcodes.I2L:
        cast(int.class, long.class, frame, block);
        break;
    case Opcodes.I2F:
        cast(int.class, float.class, frame, block);
        break;
    case Opcodes.I2D:
        cast(int.class, double.class, frame, block);
        break;
    case Opcodes.L2I:
        cast(long.class, int.class, frame, block);
        break;
    case Opcodes.L2F:
        cast(long.class, float.class, frame, block);
        break;
    case Opcodes.L2D:
        cast(long.class, double.class, frame, block);
        break;
    case Opcodes.F2I:
        cast(float.class, int.class, frame, block);
        break;
    case Opcodes.F2L:
        cast(float.class, long.class, frame, block);
        break;
    case Opcodes.F2D:
        cast(float.class, double.class, frame, block);
        break;
    case Opcodes.D2I:
        cast(double.class, int.class, frame, block);
        break;
    case Opcodes.D2L:
        cast(double.class, long.class, frame, block);
        break;
    case Opcodes.D2F:
        cast(double.class, float.class, frame, block);
        break;
    case Opcodes.I2B:
        cast(int.class, byte.class, frame, block);
        break;
    case Opcodes.I2C:
        cast(int.class, char.class, frame, block);
        break;
    case Opcodes.I2S:
        cast(int.class, short.class, frame, block);
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Array store opcodes">
    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
        Value data = frame.stack.pop();
        Value index = frame.stack.pop();
        Value array = frame.stack.pop();
        ArrayStoreInst asi = new ArrayStoreInst(array, index, data);
        block.block.instructions().add(asi);
        break;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Array load opcodes">
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        Value index2 = frame.stack.pop();
        Value array2 = frame.stack.pop();
        ArrayLoadInst ali = new ArrayLoadInst(array2, index2);
        block.block.instructions().add(ali);
        frame.stack.push(ali);
        break;
    //</editor-fold>
    case Opcodes.ARRAYLENGTH:
        ArrayLengthInst lengthInst = new ArrayLengthInst(frame.stack.pop());
        block.block.instructions().add(lengthInst);
        frame.stack.push(lengthInst);
        break;
    case Opcodes.ATHROW:
        block.block.instructions().add(new ThrowInst(frame.stack.pop()));
        break;
    default:
        throw new UnsupportedOperationException("" + insn.getOpcode());
    }
}

From source file:jerl.bcm.inj.impl.MethodCallRemove.java

License:Apache License

@Override
public void inject(MethodVisitor mv) {
    // TODO Auto-generated method stub
    // pop arguments
    Type[] args = Type.getArgumentTypes(desc);
    for (int i = 0; i < args.length; i++) {
        if (args[i].getSort() == Type.DOUBLE || args[i].getSort() == Type.LONG) {
            mv.visitInsn(Opcodes.POP2);
        } else {/*from ww  w .j ava2  s  .  co m*/
            mv.visitInsn(Opcodes.POP);
        }
    }
    if (opcode == Opcodes.INVOKESTATIC) {
    } else {
        mv.visitInsn(Opcodes.POP);
    }
}

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

License:Open Source License

private void logSyscall(HLEModuleFunction func, String logPrefix, String logCheckFunction, String logFunction) {
    // Modules.getLogger(func.getModuleName()).warn("Unimplemented...");
    loadModuleLoggger(func);// w w  w  . ja  va  2 s  .  co  m
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logCheckFunction, "()Z");
    Label loggingDisabled = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, loggingDisabled);

    loadModuleLoggger(func);

    StringBuilder formatString = new StringBuilder();
    if (logPrefix != null) {
        formatString.append(logPrefix);
    }
    formatString.append(func.getFunctionName());
    ParameterInfo[] parameters = new ClassAnalyzer().getParameters(func.getFunctionName(),
            func.getHLEModuleMethod().getDeclaringClass());
    if (parameters != null) {
        // Log message:
        //    String.format(
        //       "Unimplemented <function name>
        //                 <parameterIntegerName>=0x%X,
        //                 <parameterBooleanName>=%b,
        //                 <parameterLongName>=0x%X,
        //                 <parameterFloatName>=%f,
        //                 <parameterOtherTypeName>=%s",
        //       new Object[] {
        //                 new Integer(parameterValueInteger),
        //                 new Boolean(parameterValueBoolean),
        //                 new Long(parameterValueLong),
        //                 new Float(parameterValueFloat),
        //                 parameterValueOtherTypes
        //       })
        loadImm(parameters.length);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
        CompilerParameterReader parameterReader = new CompilerParameterReader(this);
        Annotation[][] paramsAnotations = func.getHLEModuleMethod().getParameterAnnotations();
        int objectArrayIndex = 0;
        for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
            ParameterInfo parameter = parameters[paramIndex];
            Class<?> parameterType = parameter.type;
            CompilerTypeInformation typeInformation = compilerTypeManager
                    .getCompilerTypeInformation(parameterType);

            mv.visitInsn(Opcodes.DUP);
            loadImm(objectArrayIndex);

            formatString.append(paramIndex > 0 ? ", " : " ");
            formatString.append(parameter.name);
            formatString.append("=");
            formatString.append(typeInformation.formatString);

            if (typeInformation.boxingTypeInternalName != null) {
                mv.visitTypeInsn(Opcodes.NEW, typeInformation.boxingTypeInternalName);
                mv.visitInsn(Opcodes.DUP);
            }

            loadParameter(parameterReader, func, parameterType, paramsAnotations[paramIndex], null, null);

            if (typeInformation.boxingTypeInternalName != null) {
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, typeInformation.boxingTypeInternalName, "<init>",
                        typeInformation.boxingMethodDescriptor);
            }
            mv.visitInsn(Opcodes.AASTORE);

            objectArrayIndex++;
        }
        mv.visitLdcInsn(formatString.toString());
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
                "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class) + ")"
                        + Type.getDescriptor(String.class));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                "(" + Type.getDescriptor(Object.class) + ")V");

        parameterReader = new CompilerParameterReader(this);
        for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
            ParameterInfo parameter = parameters[paramIndex];
            Class<?> parameterType = parameter.type;

            LengthInfo lengthInfo = BufferInfo.defaultLengthInfo;
            int length = BufferInfo.defaultLength;
            Usage usage = BufferInfo.defaultUsage;
            for (Annotation parameterAnnotation : paramsAnotations[paramIndex]) {
                if (parameterAnnotation instanceof BufferInfo) {
                    BufferInfo bufferInfo = (BufferInfo) parameterAnnotation;
                    lengthInfo = bufferInfo.lengthInfo();
                    length = bufferInfo.length();
                    usage = bufferInfo.usage();
                }
            }

            boolean parameterRead = false;
            if ((usage == Usage.in || usage == Usage.inout)
                    && (lengthInfo != LengthInfo.unknown || parameterType == TPointer16.class
                            || parameterType == TPointer32.class || parameterType == TPointer64.class)) {
                loadModuleLoggger(func);
                loadImm(1);
                mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
                mv.visitInsn(Opcodes.DUP);
                loadImm(0);

                Label done = new Label();
                Label addressNull = new Label();
                parameterReader.loadNextInt();
                parameterRead = true;
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFEQ, addressNull);

                String format = String.format("%s[%s]:%%s", parameter.name, usage);
                boolean useMemoryDump = true;

                switch (lengthInfo) {
                case fixedLength:
                    loadImm(length);
                    break;
                case nextNextParameter:
                    parameterReader.skipNextInt();
                    paramIndex++;
                    parameterReader.loadNextInt();
                    paramIndex++;
                    break;
                case nextParameter:
                    parameterReader.loadNextInt();
                    paramIndex++;
                    break;
                case previousParameter:
                    // Go back to the address parameter
                    parameterReader.rewindPreviousInt();
                    // Go back to the previous parameter
                    parameterReader.rewindPreviousInt();
                    // Load the length from the previous parameter
                    parameterReader.loadNextInt();
                    // Skip again the address parameter
                    // to come back to the above situation
                    parameterReader.skipNextInt();
                    break;
                case variableLength:
                    mv.visitInsn(Opcodes.DUP);
                    loadMemory();
                    mv.visitInsn(Opcodes.SWAP);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                    break;
                case unknown:
                    useMemoryDump = false;
                    format = String.format("%s[%s]: 0x%%X", parameter.name, usage);
                    loadMemory();
                    mv.visitInsn(Opcodes.SWAP);
                    if (parameterType == TPointer64.class) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read64", "(I)J");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Long.class));
                        mv.visitInsn(Opcodes.DUP);
                        mv.visitInsn(Opcodes.DUP2_X2);
                        mv.visitInsn(Opcodes.POP2);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Long.class), "<init>",
                                "(J)V");
                    } else if (parameterType == TPointer16.class) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read16", "(I)I");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                        mv.visitInsn(Opcodes.DUP_X1);
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>",
                                "(I)V");
                    } else {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                        mv.visitInsn(Opcodes.DUP_X1);
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>",
                                "(I)V");
                    }
                    break;
                default:
                    log.error(String.format("Unimplemented lengthInfo=%s", lengthInfo));
                    break;
                }

                if (useMemoryDump) {
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Utilities.class),
                            "getMemoryDump", "(II)" + Type.getDescriptor(String.class));
                }
                mv.visitInsn(Opcodes.AASTORE);

                mv.visitLdcInsn(format);
                mv.visitInsn(Opcodes.SWAP);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
                        "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class) + ")"
                                + Type.getDescriptor(String.class));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                        "(" + Type.getDescriptor(Object.class) + ")V");
                mv.visitJumpInsn(Opcodes.GOTO, done);

                mv.visitLabel(addressNull);
                mv.visitInsn(Opcodes.POP);
                mv.visitInsn(Opcodes.POP2);
                mv.visitInsn(Opcodes.POP2);
                mv.visitLabel(done);
            }

            if (!parameterRead) {
                if (parameterType == long.class) {
                    parameterReader.skipNextLong();
                } else if (parameterType == float.class) {
                    parameterReader.skipNextFloat();
                } else {
                    parameterReader.skipNextInt();
                }
            }
        }
    } else {
        mv.visitLdcInsn(formatString.toString());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                "(" + Type.getDescriptor(Object.class) + ")V");
    }

    mv.visitLabel(loggingDisabled);
}