Example usage for org.objectweb.asm Opcodes LCMP

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

Introduction

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

Prototype

int LCMP

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

Click Source Link

Usage

From source file:org.evosuite.TestSuiteGeneratorHelper.java

License:Open Source License

static void getBytecodeStatistics() {
    if (Properties.TRACK_BOOLEAN_BRANCHES) {
        int gradientBranchCount = ExecutionTraceImpl.gradientBranches.size() * 2;
        ClientServices.track(RuntimeVariable.Gradient_Branches, gradientBranchCount);
    }//from   w  w w  .j  a  va  2s.  c  o m
    if (Properties.TRACK_COVERED_GRADIENT_BRANCHES) {
        int coveredGradientBranchCount = ExecutionTraceImpl.gradientBranchesCoveredTrue.size()
                + ExecutionTraceImpl.gradientBranchesCoveredFalse.size();
        ClientServices.track(RuntimeVariable.Gradient_Branches_Covered, coveredGradientBranchCount);
    }
    if (Properties.BRANCH_COMPARISON_TYPES) {
        int cmp_intzero = 0, cmp_intint = 0, cmp_refref = 0, cmp_refnull = 0;
        int bc_lcmp = 0, bc_fcmpl = 0, bc_fcmpg = 0, bc_dcmpl = 0, bc_dcmpg = 0;
        for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT())
                .getAllBranches()) {
            int branchOpCode = b.getInstruction().getASMNode().getOpcode();
            int previousOpcode = -2;
            if (b.getInstruction().getASMNode().getPrevious() != null) {
                previousOpcode = b.getInstruction().getASMNode().getPrevious().getOpcode();
            }
            switch (previousOpcode) {
            case Opcodes.LCMP:
                bc_lcmp++;
                break;
            case Opcodes.FCMPL:
                bc_fcmpl++;
                break;
            case Opcodes.FCMPG:
                bc_fcmpg++;
                break;
            case Opcodes.DCMPL:
                bc_dcmpl++;
                break;
            case Opcodes.DCMPG:
                bc_dcmpg++;
                break;
            }
            switch (branchOpCode) {
            // copmpare int with zero
            case Opcodes.IFEQ:
            case Opcodes.IFNE:
            case Opcodes.IFLT:
            case Opcodes.IFGE:
            case Opcodes.IFGT:
            case Opcodes.IFLE:
                cmp_intzero++;
                break;
            // copmpare int with int
            case Opcodes.IF_ICMPEQ:
            case Opcodes.IF_ICMPNE:
            case Opcodes.IF_ICMPLT:
            case Opcodes.IF_ICMPGE:
            case Opcodes.IF_ICMPGT:
            case Opcodes.IF_ICMPLE:
                cmp_intint++;
                break;
            // copmpare reference with reference
            case Opcodes.IF_ACMPEQ:
            case Opcodes.IF_ACMPNE:
                cmp_refref++;
                break;
            // compare reference with null
            case Opcodes.IFNULL:
            case Opcodes.IFNONNULL:
                cmp_refnull++;
                break;

            }
        }
        ClientServices.track(RuntimeVariable.Cmp_IntZero, cmp_intzero);
        ClientServices.track(RuntimeVariable.Cmp_IntInt, cmp_intint);
        ClientServices.track(RuntimeVariable.Cmp_RefRef, cmp_refref);
        ClientServices.track(RuntimeVariable.Cmp_RefNull, cmp_refnull);

        ClientServices.track(RuntimeVariable.BC_lcmp, bc_lcmp);
        ClientServices.track(RuntimeVariable.BC_fcmpl, bc_fcmpl);
        ClientServices.track(RuntimeVariable.BC_fcmpg, bc_fcmpg);
        ClientServices.track(RuntimeVariable.BC_dcmpl, bc_dcmpl);
        ClientServices.track(RuntimeVariable.BC_dcmpg, bc_dcmpg);

        RuntimeVariable[] bytecodeVarsCovered = new RuntimeVariable[] { RuntimeVariable.Covered_lcmp,
                RuntimeVariable.Covered_fcmpl, RuntimeVariable.Covered_fcmpg, RuntimeVariable.Covered_dcmpl,
                RuntimeVariable.Covered_dcmpg, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntInt,
                RuntimeVariable.Covered_IntZero, RuntimeVariable.Covered_RefRef,
                RuntimeVariable.Covered_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsCovered) {
            ClientServices.track(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredFalse)
                            + getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredTrue));
        }

        RuntimeVariable[] bytecodeVarsReached = new RuntimeVariable[] { RuntimeVariable.Reached_lcmp,
                RuntimeVariable.Reached_fcmpl, RuntimeVariable.Reached_fcmpg, RuntimeVariable.Reached_dcmpl,
                RuntimeVariable.Reached_dcmpg, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntInt,
                RuntimeVariable.Reached_IntZero, RuntimeVariable.Reached_RefRef,
                RuntimeVariable.Reached_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsReached) {
            ClientServices.track(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionReached) * 2);
        }

    }

}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompilerForScaledLongs_Base.java

License:Open Source License

@Override
protected int compileComparison(int _ifOpcode, int _comparisonOpcode) throws CompilerException {
    mv().visitInsn(Opcodes.LCMP);
    return _ifOpcode;
}

From source file:org.jacoco.core.internal.instr.FrameTracker.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    final Object t1, t2, t3, t4;
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.RETURN:
        break;// ww  w.j  ava2s . co  m
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
    case Opcodes.POP:
        pop(1);
        break;
    case Opcodes.DRETURN:
    case Opcodes.LRETURN:
    case Opcodes.POP2:
        pop(2);
        break;
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.FASTORE:
    case Opcodes.IASTORE:
    case Opcodes.SASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    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:
        push(Opcodes.INTEGER);
        break;
    case Opcodes.ARRAYLENGTH:
    case Opcodes.F2I:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.INEG:
        pop(1);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.D2I:
    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
    case Opcodes.IADD:
    case Opcodes.IALOAD:
    case Opcodes.IAND:
    case Opcodes.IDIV:
    case Opcodes.IMUL:
    case Opcodes.IOR:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.ISUB:
    case Opcodes.IUSHR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.SALOAD:
        pop(2);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
    case Opcodes.LCMP:
        pop(4);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(Opcodes.FLOAT);
        break;
    case Opcodes.FNEG:
    case Opcodes.I2F:
        pop(1);
        push(Opcodes.FLOAT);
        break;
    case Opcodes.D2F:
    case Opcodes.FADD:
    case Opcodes.FALOAD:
    case Opcodes.FDIV:
    case Opcodes.FMUL:
    case Opcodes.FREM:
    case Opcodes.FSUB:
    case Opcodes.L2F:
        pop(2);
        push(Opcodes.FLOAT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.F2L:
    case Opcodes.I2L:
        pop(1);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.D2L:
    case Opcodes.LALOAD:
    case Opcodes.LNEG:
        pop(2);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.LADD:
    case Opcodes.LAND:
    case Opcodes.LDIV:
    case Opcodes.LMUL:
    case Opcodes.LOR:
    case Opcodes.LREM:
    case Opcodes.LSUB:
    case Opcodes.LXOR:
        pop(4);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.F2D:
    case Opcodes.I2D:
        pop(1);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.DNEG:
    case Opcodes.L2D:
        pop(2);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.DADD:
    case Opcodes.DDIV:
    case Opcodes.DMUL:
    case Opcodes.DREM:
    case Opcodes.DSUB:
        pop(4);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.ACONST_NULL:
        push(Opcodes.NULL);
        break;
    case Opcodes.AALOAD:
        pop(1);
        t1 = pop();
        push(Type.getType(((String) t1).substring(1)));
        break;
    case Opcodes.DUP:
        t1 = pop();
        push(t1);
        push(t1);
        break;
    case Opcodes.DUP_X1:
        t1 = pop();
        t2 = pop();
        push(t1);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP_X2:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        push(t1);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2:
        t1 = pop();
        t2 = pop();
        push(t2);
        push(t1);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2_X1:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        push(t2);
        push(t1);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2_X2:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.SWAP:
        t1 = pop();
        t2 = pop();
        push(t1);
        push(t2);
        break;
    default:
        throw new IllegalArgumentException();
    }
    mv.visitInsn(opcode);
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * Generates the instructions to jump to a label based on the comparison of
 * the top two stack values.//from   w w w  . j  av a  2s  .  c  om
 *
 * @param type the type of the top two stack values.
 * @param mode how these values must be compared. One of EQ, NE, LT, GE, GT,
 *        LE.
 * @param label where to jump if the comparison result is <tt>true</tt>.
 */
public void ifCmp(final Type type, final int mode, final Label label) {
    int intOp = -1;
    switch (type.getSort()) {
    case Type.LONG:
        visitInsn(Opcodes.LCMP);
        break;
    case Type.DOUBLE:
        visitInsn(Opcodes.DCMPG);
        break;
    case Type.FLOAT:
        visitInsn(Opcodes.FCMPG);
        break;
    case Type.ARRAY:
    case Type.OBJECT:
        switch (mode) {
        case EQ:
            visitJumpInsn(Opcodes.IF_ACMPEQ, label);
            return;
        case NE:
            visitJumpInsn(Opcodes.IF_ACMPNE, label);
            return;
        }
        throw new IllegalArgumentException("Bad comparison for type " + type);
    default:
        switch (mode) {
        case EQ:
            intOp = Opcodes.IF_ICMPEQ;
            break;
        case NE:
            intOp = Opcodes.IF_ICMPNE;
            break;
        case GE:
            intOp = Opcodes.IF_ICMPGE;
            break;
        case LT:
            intOp = Opcodes.IF_ICMPLT;
            break;
        case LE:
            intOp = Opcodes.IF_ICMPLE;
            break;
        case GT:
            intOp = Opcodes.IF_ICMPGT;
            break;
        }
        visitJumpInsn(intOp, label);
        return;
    }
    int jumpMode = mode;
    switch (mode) {
    case GE:
        jumpMode = LT;
        break;
    case LE:
        jumpMode = GT;
        break;
    }
    visitJumpInsn(jumpMode, label);
}

From source file:org.jboss.byteman.rule.expression.ComparisonExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    Expression oper0 = getOperand(0);
    Expression oper1 = getOperand(1);

    int removed = 0;

    // evaluate the operands and ensure the reuslt is of the correct type for comparison adds 2
    oper0.compile(mv, compileContext);/*  w w w  .  j a  v  a2 s .  co m*/
    compileTypeConversion(oper0.getType(), comparisonType, mv, compileContext);
    oper1.compile(mv, compileContext);
    compileTypeConversion(oper1.getType(), comparisonType, mv, compileContext);

    // now do the appropriate type of comparison
    if (comparisonType == type.B || comparisonType == type.S || comparisonType == type.S
            || comparisonType == type.I) {
        Label elsetarget = new Label();
        Label endtarget = new Label();
        // we remove 2 words from the stack and then add 1 back
        removed = 2;
        switch (oper) {
        case LT:
            mv.visitJumpInsn(Opcodes.IF_ICMPGE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case LE:
            mv.visitJumpInsn(Opcodes.IF_ICMPGT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GT:
            mv.visitJumpInsn(Opcodes.IF_ICMPLE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GE:
            mv.visitJumpInsn(Opcodes.IF_ICMPLT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case EQ:
            mv.visitJumpInsn(Opcodes.IF_ICMPNE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case NE:
            mv.visitJumpInsn(Opcodes.IF_ICMPEQ, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        }
    } else if (comparisonType == type.J || comparisonType == type.F || comparisonType == type.D) {
        if (comparisonType == type.J) {
            mv.visitInsn(Opcodes.LCMP);
            // we remove four words from the stack and add 1 back
            removed = 4;
        } else if (comparisonType == type.F) {
            // we remove two words from the stack and add 1 back
            removed = 2;
            mv.visitInsn(Opcodes.FCMPG);
        } else if (comparisonType == type.D) {
            // we remove four words from the stack and add 1 back
            removed = 4;
            mv.visitInsn(Opcodes.DCMPG);
        }
        Label elsetarget = new Label();
        Label endtarget = new Label();
        switch (oper) {
        case LT:
            mv.visitJumpInsn(Opcodes.IFGE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case LE:
            mv.visitJumpInsn(Opcodes.IFGT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GT:
            mv.visitJumpInsn(Opcodes.IFLE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GE:
            mv.visitJumpInsn(Opcodes.IFLT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case EQ:
            mv.visitJumpInsn(Opcodes.IFNE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case NE:
            mv.visitJumpInsn(Opcodes.IFEQ, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        }
    } else if (comparable) {
        // we add a further two words setting up the relevant test then remove them
        // and also remove the original two words replacing them with a single word
        removed = 4;
        compileContext.addStackCount(2);
        // we need to deal with null values correctly
        // if op1 == null || op2 == null
        // then
        //   EQ:
        //   push value1 == value2
        //   NE:
        //   push value1 != value2
        //   ow:
        //   push false
        // else
        //   execute compareTo or equals and test for the desired outcome
        // end if
        Label splittarget = new Label(); // else
        Label jointarget = new Label(); // end if
        mv.visitInsn(Opcodes.DUP2); // [... op1, op2 ] ==> [... op1, op2, op1,  op2]
        mv.visitInsn(Opcodes.POP); // [... op1, op2, op1, op2 ] ==> [... op1, op2, op1]
        // if op1 == null
        mv.visitJumpInsn(Opcodes.IFNULL, splittarget); // [... op1, op2, op1] ==> [... op1, op2]
        mv.visitInsn(Opcodes.DUP); // [... op1, op2 ] ==> [... op1, op2, op2]
        // || op2 == null
        mv.visitJumpInsn(Opcodes.IFNULL, splittarget); // [... op1, op2, op2] ==> [... op1, op2]
        // so, it is ok to call compareTo leaving an int or equals leaving a boolean
        if (oper != EQ && oper != NE) {
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Comparable", "compareTo",
                    "(Ljava/lang/Object;)I");
        } else {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
        }
        // now if we did a compareTo we need to generate the required boolean
        Label elsetarget = new Label();
        Label endtarget = new Label();
        // if needed the convert the compareTo result to the required boolean outcome
        switch (oper) {
        case LT:
            mv.visitJumpInsn(Opcodes.IFGE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case LE:
            mv.visitJumpInsn(Opcodes.IFGT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GT:
            mv.visitJumpInsn(Opcodes.IFLE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case GE:
            mv.visitJumpInsn(Opcodes.IFLT, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
            break;
        case NE:
            mv.visitJumpInsn(Opcodes.IFEQ, elsetarget);
            mv.visitLdcInsn(false);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(true);
            mv.visitLabel(endtarget);
            break;
        }
        // skip to the join point
        mv.visitJumpInsn(Opcodes.GOTO, jointarget);
        // label the split point
        mv.visitLabel(splittarget);
        if (oper == EQ) {
            elsetarget = new Label();
            endtarget = new Label();
            mv.visitJumpInsn(Opcodes.IF_ACMPEQ, elsetarget);
            mv.visitLdcInsn(false);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(true);
            mv.visitLabel(endtarget);
        } else if (oper == NE) {
            elsetarget = new Label();
            endtarget = new Label();
            mv.visitJumpInsn(Opcodes.IF_ACMPNE, elsetarget);
            mv.visitLdcInsn(false);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(true);
            mv.visitLabel(endtarget);
        } else {
            // pop the operands and stack false
            mv.visitInsn(Opcodes.POP2);
            mv.visitLdcInsn(false);
        }
        // label the join point
        mv.visitLabel(jointarget);
    } else if (comparisonType == Type.Z) {
        // unboxed booleans need special treatment
        // we remove two words replacing them with a single word
        removed = 2;
        Label elsetarget = new Label();
        Label endtarget = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, elsetarget);
        // on this branch for EQ the stacked value is what we need and for NE
        // the stacked value needs flipping
        if (oper == NE) {
            Label elsetarget2 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, elsetarget2);
            mv.visitLdcInsn(false);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget2);
            mv.visitLdcInsn(true);
        }
        mv.visitJumpInsn(Opcodes.GOTO, endtarget);
        mv.visitLabel(elsetarget);
        // on this branch for NE the stacked value is what we need and for EQ
        // the stacked value needs flipping
        if (oper == EQ) {
            Label elsetarget2 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, elsetarget2);
            mv.visitLdcInsn(false);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget2);
            mv.visitLdcInsn(true);
        }
        mv.visitLabel(endtarget);

    } else if (comparisonType == Type.BOOLEAN) {
        // boxed booleans need special treatment
        // we remove two words replacing them with a single word
        removed = 2;
        Label elsetarget = new Label();
        Label endtarget = new Label();
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java.lang.Boolean", "equals", "(Ljava/lang/Boolean;)Z");
        if (oper == NE) {
            mv.visitJumpInsn(Opcodes.IFEQ, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
        }
    } else {
        // we remove two words replacing them with a single word
        removed = 2;
        Label elsetarget = new Label();
        Label endtarget = new Label();
        if (oper == EQ) {
            mv.visitJumpInsn(Opcodes.IF_ACMPNE, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
        } else {
            mv.visitJumpInsn(Opcodes.IF_ACMPEQ, elsetarget);
            mv.visitLdcInsn(true);
            mv.visitJumpInsn(Opcodes.GOTO, endtarget);
            mv.visitLabel(elsetarget);
            mv.visitLdcInsn(false);
            mv.visitLabel(endtarget);
        }
    }
    compileContext.addStackCount(1 - removed);
}

From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java

License:Open Source License

/**
 * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null}
 * if the instruction is not a full statement (in that case, the instruction is stored in the
 * given Expression {@link Stack})./*from  w  w w.  ja va2  s.c  o m*/
 * 
 * @param insnNode the instruction to read
 * @param expressionStack the expression stack to put on or pop from.
 * @param localVariables the local variables
 * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created
 *         after reading the current instruction.
 * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode
 *      instruction listings on Wikipedia</a>
 */
private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack,
        final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) {
    final List<Statement> statements = new ArrayList<>();
    final AbstractInsnNode insnNode = insnCursor.getCurrent();
    switch (insnNode.getOpcode()) {
    // return a reference from a method
    case Opcodes.ARETURN:
        // return an integer from a method
    case Opcodes.IRETURN:
        statements.add(new ReturnStatement(expressionStack.pop()));
        break;
    // return void from method
    case Opcodes.RETURN:
        // wrap all pending expressions into ExpressionStatements
        while (!expressionStack.isEmpty()) {
            final Expression pendingExpression = expressionStack.pop();
            statements.add(new ExpressionStatement(pendingExpression));
        }
        break;
    // push a null reference onto the stack
    case Opcodes.ACONST_NULL:
        expressionStack.add(new NullLiteral());
        break;
    // load the int value 0 onto the stack
    case Opcodes.ICONST_0:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(0));
        break;
    // load the int value 1 onto the stack
    case Opcodes.ICONST_1:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(1));
        break;
    // load the int value 2 onto the stack
    case Opcodes.ICONST_2:
        expressionStack.add(new NumberLiteral(2));
        break;
    // load the int value 3 onto the stack
    case Opcodes.ICONST_3:
        expressionStack.add(new NumberLiteral(3));
        break;
    // load the int value 4 onto the stack
    case Opcodes.ICONST_4:
        expressionStack.add(new NumberLiteral(4));
        break;
    // load the int value 5 onto the stack
    case Opcodes.ICONST_5:
        expressionStack.add(new NumberLiteral(5));
        break;
    // push the long 0 onto the stack
    case Opcodes.LCONST_0:
        expressionStack.add(new NumberLiteral(0L));
        break;
    // push the long 1 onto the stack
    case Opcodes.LCONST_1:
        expressionStack.add(new NumberLiteral(1L));
        break;
    // push the 0.0f onto the stack
    case Opcodes.FCONST_0:
        expressionStack.add(new NumberLiteral(0f));
        break;
    // push the 1.0f onto the stack
    case Opcodes.FCONST_1:
        expressionStack.add(new NumberLiteral(1f));
        break;
    // push the 2.0f onto the stack
    case Opcodes.FCONST_2:
        expressionStack.add(new NumberLiteral(2f));
        break;
    // push the constant 0.0 onto the stack
    case Opcodes.DCONST_0:
        expressionStack.add(new NumberLiteral(0d));
        break;
    // push the constant 1.0 onto the stack
    case Opcodes.DCONST_1:
        expressionStack.add(new NumberLiteral(1d));
        break;
    // compare two longs values
    case Opcodes.LCMP:
        // compare two doubles
    case Opcodes.DCMPL:
        // compare two doubles
    case Opcodes.DCMPG:
        // compare two floats
    case Opcodes.FCMPL:
        // compare two floats
    case Opcodes.FCMPG:
        statements.addAll(
                readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables));
        break;
    // add 2 ints
    case Opcodes.IADD:
        expressionStack.add(readOperation(Operator.ADD, expressionStack));
        break;
    // int subtract
    case Opcodes.ISUB:
        expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack));
        break;
    // multiply 2 integers
    case Opcodes.IMUL:
        expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack));
        break;
    // divide 2 integers
    case Opcodes.IDIV:
        expressionStack.add(readOperation(Operator.DIVIDE, expressionStack));
        break;
    // negate int
    case Opcodes.INEG:
        expressionStack.add(inverseInteger(expressionStack));
        break;
    // discard the top value on the stack
    case Opcodes.POP:
        statements.add(new ExpressionStatement(expressionStack.pop()));
        break;
    // duplicate the value on top of the stack
    case Opcodes.DUP:
        expressionStack.push(expressionStack.peek());
        break;
    // insert a copy of the top value into the stack two values from the top.
    case Opcodes.DUP_X1:
        expressionStack.add(expressionStack.size() - 2, expressionStack.peek());
        break;
    // store into a reference in an array
    case Opcodes.AASTORE:
        readArrayStoreInstruction(insnNode, expressionStack);
        break;
    // converts Float to Double -> ignored.
    case Opcodes.F2D:
        break;
    default:
        throw new AnalyzeException(
                "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported.");
    }
    return statements;
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**  
 * Boolean valued operators (!, &&, ||, ==, !=, <, <=, > and >=) are highly optimized during compilation to bytecode.
 * Here is a quick outline of the optimizations used:
 * -not (e1 && e2) is compiled as a single notAnd operator
 * -not (e1 || e2) is compiled as a single notOr operator
 * -not (not e) is optimized out./*from   w w  w.j av a 2  s  .c  o  m*/
 * -not (x < y) is compiled as x >= y for integral comparisons. A similar thing is done for not (double <), but it is not quite double >= because
 *  of NaN. However, there is special java bytecode support for treatment of this.
 * -Comparisons where the right-hand-side is an int 0 are treated more efficiently i.e. x > 0.
 * -Comparisons to null are treated specially i.e. x != null, x == null.  
 * -if the result of a boolean valued operator is used by the condition part of an if-then-else statement (or ternary operator) then
 *  the resulting true or false value is not pushed onto the stack and then tested. Rather we directly branch to the appropriate
 *  continuation.
 * -the most complicated optimization is that "trees" of boolean valued operators are effectively compiled as a single operator. 
 *  What this means is that the resulting "true" and "false" values are not popped onto the stack and consumed by subsequent operators
 *  but rather a "continuation style" is employed where we just jump to the correct next comparison.
 *  This saves an extra comparison per operator, as well as unecessary pushes of trues and falses compared to the naive compilation scheme. 
 *  The precise bytecode instructions used in the compilation schemes varies depending on context (see the endsWithTrueForm argument).
 * 
 * @param operatorExpr
 * @param context
 * @param trueContinuation label to jump to if the expression has a true value
 * @param falseContinuation label to jump to if the expression has a false value 
 * @param endsWithTrueForm operators are encoded as a series of tests with jumps where if none of the jumps are taken the operator slips
 *    through to the default case. This is usually "true" but if the "endsWithTrueForm" flag is set to false, then the default case will
 *    be false. For example, this is useful when encoding a boolean-valued operator that is the left argument of the || operator. 
 *    In that case we want the default case to proceed to evaluation of the second argument of ||.
 * @throws JavaGenerationException
 */
private static void encodeBooleanValuedOperatorHelper(JavaExpression.OperatorExpression operatorExpr,
        GenerationContext context, Label trueContinuation, Label falseContinuation, boolean endsWithTrueForm)
        throws JavaGenerationException {

    MethodVisitor mv = context.getMethodVisitor();

    JavaOperator operator = operatorExpr.getJavaOperator();
    String symbol = operator.getSymbol();
    JavaTypeName valueType = operator.getValueType();

    if (operator.isLogicalOp()) {

        // Logical op:    {"!", "&&", "||"}
        // Note: conditional statements should not be handled here..
        //   eg. "if" conditional evaluation happens during "if" source generation.
        //   We can get here if, eg. printing the result of a conditional.            

        // boolean negation
        if (symbol.equals("!")) {

            JavaExpression arg0Expr = operatorExpr.getArgument(0);

            //attempt to optimize a variety of cases where not is composed with another boolean valued operator.

            if (arg0Expr instanceof JavaExpression.OperatorExpression) {

                if (arg0Expr instanceof JavaExpression.OperatorExpression.Binary) {

                    JavaExpression.OperatorExpression.Binary arg0BinaryOperatorExpr = (JavaExpression.OperatorExpression.Binary) arg0Expr;
                    JavaOperator arg0BinaryOperator = arg0BinaryOperatorExpr.getJavaOperator();

                    //not (expr1 && expr2) is encoded in a special way. Effectively there is a notAnd operator.

                    if (arg0BinaryOperator == JavaOperator.CONDITIONAL_AND) {

                        //x notAnd y                    
                        //is encoded as                    
                        //if x == false then goto trueContinuation
                        //if y == true then goto falseContinuation

                        ////what follows is a sample continuation in the case when a literal value is pushed onto the stack
                        //label trueContinuation:
                        //push true
                        //goto next
                        //label falseContinuation:
                        //push false
                        //next: 

                        JavaExpression andOpArg0Expr = arg0BinaryOperatorExpr.getArgument(0);
                        if (isBooleanValuedOperatorExpr(andOpArg0Expr)) {
                            Label innerTrueContinuation = new Label();
                            encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) andOpArg0Expr,
                                    context, innerTrueContinuation, trueContinuation);
                            mv.visitLabel(innerTrueContinuation);
                        } else {
                            encodeExpr(andOpArg0Expr, context);
                            mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
                        }

                        JavaExpression andOpArg1Expr = arg0BinaryOperatorExpr.getArgument(1);
                        if (isBooleanValuedOperatorExpr(andOpArg1Expr)) {
                            encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) andOpArg1Expr,
                                    context, falseContinuation, trueContinuation, !endsWithTrueForm);
                        } else {
                            encodeExpr(andOpArg1Expr, context);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
                            }
                        }

                        return;
                    }

                    //not (expr1 || expr2) is encoded in a special way. Effectively there is a notOr operator.

                    if (arg0BinaryOperator == JavaOperator.CONDITIONAL_OR) {

                        //x notOr y                    
                        //is encoded as                    
                        //if x == true then goto falseContinuation
                        //if y == true then goto falseContinuation

                        ////what follows is a sample continuation in the case when a literal value is pushed onto the stack                           
                        //label trueContinuation:
                        //push true
                        //goto next
                        //label falseContinuation:
                        //push false
                        //next: 

                        JavaExpression orOpArg0Expr = arg0BinaryOperatorExpr.getArgument(0);
                        if (isBooleanValuedOperatorExpr(orOpArg0Expr)) {
                            Label innerFalseContinuation = new Label();
                            //if x evaluates to false, we want to continue with evaluating y, this is why the "endsWithTrueForm" argument is false here.
                            //if x evaluates to false, then x notOr y returns true without needing to evaluate y. That is why the trueContinuation for x, is
                            //the falseContinuation for the call that encodes x.
                            encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) orOpArg0Expr,
                                    context, falseContinuation, innerFalseContinuation, false);
                            mv.visitLabel(innerFalseContinuation);
                        } else {
                            encodeExpr(orOpArg0Expr, context);
                            mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
                        }

                        JavaExpression orOpArg1Expr = arg0BinaryOperatorExpr.getArgument(1);
                        if (isBooleanValuedOperatorExpr(orOpArg1Expr)) {
                            encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) orOpArg1Expr,
                                    context, falseContinuation, trueContinuation, !endsWithTrueForm);
                        } else {
                            encodeExpr(orOpArg1Expr, context);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
                            }
                        }

                        return;
                    }

                    //try to optimize not composed with a boolean valued operator as a single operation
                    //for example, for int operators, not (x < y) is actually encoded as x >= y.                        

                    JavaExpression.OperatorExpression.Binary notComposedOperatorExpr = arg0BinaryOperatorExpr
                            .getNotComposedOperatorExpr();
                    if (notComposedOperatorExpr != null) {

                        encodeBooleanValuedOperatorHelper(notComposedOperatorExpr, context, trueContinuation,
                                falseContinuation, endsWithTrueForm);
                        return;
                    }

                    //not (x Double.< y) is encoded like x Double.>= y except that the opposite DCMP instruction is used.                           
                    //this is to handle NAN. Similar for the others.

                    if (arg0BinaryOperator == JavaOperator.LESS_THAN_DOUBLE
                            || arg0BinaryOperator == JavaOperator.LESS_THAN_EQUALS_DOUBLE
                            || arg0BinaryOperator == JavaOperator.GREATER_THAN_DOUBLE
                            || arg0BinaryOperator == JavaOperator.GREATER_THAN_EQUALS_DOUBLE) {

                        //encode the first argument
                        JavaTypeName firstArgType = encodeExpr(arg0BinaryOperatorExpr.getArgument(0), context);

                        // Add instructions to widen the first argument if necessary.
                        int wideningOpCode = getWideningOpCode(firstArgType, JavaTypeName.DOUBLE);
                        if (wideningOpCode != Opcodes.NOP) {
                            mv.visitInsn(wideningOpCode);
                        }

                        //endcode the second argument
                        JavaExpression secondArgExpr = arg0BinaryOperatorExpr.getArgument(1);
                        JavaTypeName secondArgType = encodeExpr(secondArgExpr, context);
                        wideningOpCode = getWideningOpCode(secondArgType, JavaTypeName.DOUBLE);
                        if (wideningOpCode != Opcodes.NOP) {
                            mv.visitInsn(wideningOpCode);
                        }

                        if (arg0BinaryOperator == JavaOperator.LESS_THAN_DOUBLE) {

                            mv.visitInsn(Opcodes.DCMPG);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFLT, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFGE, trueContinuation);
                            }

                        } else if (arg0BinaryOperator == JavaOperator.LESS_THAN_EQUALS_DOUBLE) {

                            mv.visitInsn(Opcodes.DCMPG);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFLE, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFGT, trueContinuation);
                            }

                        } else if (arg0BinaryOperator == JavaOperator.GREATER_THAN_DOUBLE) {

                            mv.visitInsn(Opcodes.DCMPL);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFGT, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFLE, trueContinuation);
                            }

                        } else if (arg0BinaryOperator == JavaOperator.GREATER_THAN_EQUALS_DOUBLE) {

                            mv.visitInsn(Opcodes.DCMPL);
                            if (endsWithTrueForm) {
                                mv.visitJumpInsn(Opcodes.IFGE, falseContinuation);
                            } else {
                                mv.visitJumpInsn(Opcodes.IFLT, trueContinuation);
                            }

                        } else {

                            throw new JavaGenerationException(
                                    "Expecting one of the double operators <, >, <= or >=.");
                        }

                        return;
                    }

                    //fall through to the unoptimized case...

                } else if (arg0Expr instanceof JavaExpression.OperatorExpression.Unary) {

                    //"not (not expr)" is encoded as "id expr"

                    JavaExpression.OperatorExpression.Unary arg0UnaryOperatorExpr = (JavaExpression.OperatorExpression.Unary) arg0Expr;
                    if (arg0UnaryOperatorExpr.getJavaOperator() != JavaOperator.LOGICAL_NEGATE) {
                        throw new JavaGenerationException("Unary logical negation expected.");
                    }

                    JavaExpression expr = arg0UnaryOperatorExpr.getArgument(0);
                    if (isBooleanValuedOperatorExpr(expr)) {
                        encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) expr, context,
                                trueContinuation, falseContinuation, endsWithTrueForm);
                    } else {
                        encodeExpr(expr, context);
                        if (endsWithTrueForm) {
                            mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
                        } else {
                            mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
                        }
                    }

                    return;
                }
            }

            //!x 
            //is encoded as
            //if x == true then goto falseContinuation;

            ////what follows is a sample continuation in the case when a literal value is pushed onto the stack
            //push true;
            //goto next;
            //falseContinuation:
            //push false;
            //label next:                                   

            encodeExpr(arg0Expr, context);
            if (endsWithTrueForm) {
                //Note that IFNE consumes a value on the stack.
                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
            }

            return;
        }

        if (symbol.equals("&&")) {

            //x && y                    
            //is encoded as                    
            //if x == false then goto falseContinuation
            //if y == false then goto falseContinuation

            ////what follows is a sample continuation in the case when a literal value is pushed onto the stack
            //push true
            //goto next
            //label falseContinuation:
            //push false
            //label next:

            JavaExpression arg0Expr = operatorExpr.getArgument(0);
            if (isBooleanValuedOperatorExpr(arg0Expr)) {
                Label innerTrueContinuation = new Label();
                encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) arg0Expr, context,
                        innerTrueContinuation, falseContinuation);
                mv.visitLabel(innerTrueContinuation);
            } else {
                encodeExpr(arg0Expr, context);
                mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
            }

            JavaExpression arg1Expr = operatorExpr.getArgument(1);
            if (isBooleanValuedOperatorExpr(arg1Expr)) {
                encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) arg1Expr, context,
                        trueContinuation, falseContinuation, endsWithTrueForm);
            } else {
                encodeExpr(arg1Expr, context);
                if (endsWithTrueForm) {
                    mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
                }
            }

            return;
        }

        if (symbol.equals("||")) {

            //x || y
            //is encoded as
            //if x == true then goto trueContinuation
            //if y == false then goto falseContinuation

            ////what follows is a sample continuation in the case when a literal value is pushed onto the stack
            //push true
            //goto next
            //label falseContinuation:
            //push false
            //label next:

            JavaExpression arg0Expr = operatorExpr.getArgument(0);
            if (isBooleanValuedOperatorExpr(arg0Expr)) {
                Label innerFalseContinuation = new Label();
                //if x evaluates to false, we want to continue with evaluating y, this is why the "endsWithTrueForm" argument is false here.
                encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) arg0Expr, context,
                        trueContinuation, innerFalseContinuation, false);
                mv.visitLabel(innerFalseContinuation);
            } else {
                encodeExpr(arg0Expr, context);
                mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
            }

            JavaExpression arg1Expr = operatorExpr.getArgument(1);
            if (isBooleanValuedOperatorExpr(arg1Expr)) {
                encodeBooleanValuedOperatorHelper((JavaExpression.OperatorExpression) arg1Expr, context,
                        trueContinuation, falseContinuation, endsWithTrueForm);
            } else {
                encodeExpr(arg1Expr, context);
                if (endsWithTrueForm) {
                    mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
                }
            }

            return;
        }

        throw new JavaGenerationException("Unknown logical operator " + symbol + ".");

    } // if(operator.isLogicalOp()) 

    // A relational operator

    //one comment on the bytecode sequences: there is some subtle points here because of the treatment of special values e.g. such
    //as not a number, plus infinity, minus 0 etc in the double and float types. The code below is based on copying what the Java
    //compiler generates for simple functions such as:
    //double foo(double x, double y) {double z = x < y; return z;}

    //encode the first argument
    JavaTypeName firstArgType = encodeExpr(operatorExpr.getArgument(0), context);

    // Add instructions to widen the first argument if necessary.
    int wideningOpCode = getWideningOpCode(firstArgType, valueType);
    if (wideningOpCode != Opcodes.NOP) {
        mv.visitInsn(wideningOpCode);
    }

    //Deal with comparisons to null as a special case. Don't push the second argument, since the null is 
    //implicit in the bytecode instruction.
    JavaExpression secondArgExpr = operatorExpr.getArgument(1);
    final boolean compareToNull = secondArgExpr == LiteralWrapper.NULL;

    //Deal with comparisons to int zero as a special case. There are special 1 argument operators for this case.
    //javac makes use of this optimization. Interestingly, javac does not optimize the case when the first argument
    //is a literal int zero i.e. 0 < x, is not converted to x > 0 which then can make use of the 1 argument comparison.        
    final boolean compareToIntZero = isInternalIntType(valueType) && isLiteralIntZeroExpr(secondArgExpr);

    if (!compareToNull && !compareToIntZero) {
        //endcode the second argument
        JavaTypeName secondArgType = encodeExpr(secondArgExpr, context);
        wideningOpCode = getWideningOpCode(secondArgType, valueType);
        if (wideningOpCode != Opcodes.NOP) {
            mv.visitInsn(wideningOpCode);
        }
    }

    // relational symbols: {">", ">=", "<", "<=", "==", "!="}
    if (symbol.equals(">")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFLE, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPLE, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFGT, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPGT, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGT, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGT, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGT, trueContinuation);
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM > operator.");
        }

    } else if (symbol.equals(">=")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFLT, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPLT, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFGE, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPGE, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFLT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFGE, trueContinuation);
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM >= operator.");
        }

    } else if (symbol.equals("<")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFGE, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPGE, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFLT, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPLT, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLT, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPG);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLT, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPG);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLT, trueContinuation);
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM < operator.");
        }

    } else if (symbol.equals("<=")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFGT, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPGT, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFLE, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPLE, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPG);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPG);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFGT, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFLE, trueContinuation);
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM <= operator.");
        }

    } else if (symbol.equals("==")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BOOLEAN_TAG:
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPNE, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFNE, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFEQ, trueContinuation);
            }
            break;
        }

        case JavaTypeName.ARRAY_TAG:
        case JavaTypeName.OBJECT_TAG: {
            if (endsWithTrueForm) {
                if (compareToNull) {
                    mv.visitJumpInsn(Opcodes.IFNONNULL, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ACMPNE, falseContinuation);
                }
            } else {
                if (compareToNull) {
                    mv.visitJumpInsn(Opcodes.IFNULL, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ACMPEQ, trueContinuation);
                }
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM == operator.");
        }

    } else if (symbol.equals("!=")) {

        switch (valueType.getTag()) {
        case JavaTypeName.BOOLEAN_TAG:
        case JavaTypeName.BYTE_TAG:
        case JavaTypeName.SHORT_TAG:
        case JavaTypeName.CHAR_TAG:
        case JavaTypeName.INT_TAG: {
            if (endsWithTrueForm) {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, falseContinuation);
                }
            } else {
                if (compareToIntZero) {
                    mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ICMPNE, trueContinuation);
                }
            }
            break;
        }

        case JavaTypeName.LONG_TAG: {
            mv.visitInsn(Opcodes.LCMP);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.DOUBLE_TAG: {
            mv.visitInsn(Opcodes.DCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.FLOAT_TAG: {
            mv.visitInsn(Opcodes.FCMPL);
            if (endsWithTrueForm) {
                mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);
            } else {
                mv.visitJumpInsn(Opcodes.IFNE, trueContinuation);
            }
            break;
        }

        case JavaTypeName.ARRAY_TAG:
        case JavaTypeName.OBJECT_TAG: {
            if (endsWithTrueForm) {
                if (compareToNull) {
                    mv.visitJumpInsn(Opcodes.IFNULL, falseContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ACMPEQ, falseContinuation);
                }
            } else {
                if (compareToNull) {
                    mv.visitJumpInsn(Opcodes.IFNONNULL, trueContinuation);
                } else {
                    mv.visitJumpInsn(Opcodes.IF_ACMPNE, trueContinuation);
                }
            }
            break;
        }

        default:
            throw new IllegalArgumentException("Unsupported operand type for JVM != operator.");
        }

    } else {
        throw new JavaGenerationException("Unknown relational operator " + symbol + ".");
    }
}

From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java

License:Open Source License

@Test
public void test_cmp() throws Exception {
    int[] opcodes = new int[] { Opcodes.LCMP, Opcodes.FCMPG, Opcodes.FCMPL, Opcodes.DCMPG, Opcodes.FCMPL };
    assertConsume2produceNotNull(opcodes);

    assertThrowWhenInvalidStack(opcodes, " needs 2 values on stack");
}

From source file:serianalyzer.JVMImpl.java

License:Open Source License

/**
 * @param opcode/*from  www.  ja v a 2s.  c  o m*/
 * @param s
 */
static void handleJVMInsn(int opcode, JVMStackState s) {
    BaseType o1;
    BaseType o2;
    BaseType o3;
    List<BaseType> l1;
    List<BaseType> l2;
    switch (opcode) {
    case Opcodes.NOP:
        break;

    case Opcodes.ARRAYLENGTH:
        o1 = s.pop();
        s.push(new BasicConstant(Type.INT_TYPE, 0, !(o1 != null && !o1.isTainted())));
        break;
    case Opcodes.ACONST_NULL:
        s.push(new BasicConstant(Type.VOID_TYPE, "<null>")); //$NON-NLS-1$
        break;
    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:
        s.push(new BasicConstant(Type.INT_TYPE, opcode - 3));
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        s.push(new BasicConstant(Type.LONG_TYPE, opcode - 9L));
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        s.push(new BasicConstant(Type.FLOAT_TYPE, opcode - 11f));
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        s.push(new BasicConstant(Type.DOUBLE_TYPE, opcode - 14d));
        break;
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD:
        o1 = s.pop();
        o2 = s.pop();
        s.push(new BasicVariable(toType(opcode), "primitive array elem", //$NON-NLS-1$
                (o1 == null || o1.isTainted()) | (o2 == null || o2.isTainted())));
        break;

    case Opcodes.AALOAD:
        o1 = s.pop();
        o2 = s.pop();
        if (o1 != null && o2 instanceof SimpleType && ((SimpleType) o2).getType().toString().startsWith("[")) { //$NON-NLS-1$
            Type atype = Type.getType(((SimpleType) o2).getType().toString().substring(1));
            if (o2.getAlternativeTypes() != null && !o2.getAlternativeTypes().isEmpty()) {
                s.clear();
                break;
            }
            s.push(new BasicVariable(atype, "array elem " + atype, o1.isTainted() | o2.isTainted())); //$NON-NLS-1$
        } else {
            s.clear();
        }
        break;

    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
        s.pop(3);
        break;

    case Opcodes.POP2:
        s.pop();
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
    case Opcodes.POP:
        s.pop();
        break;

    case Opcodes.DUP:
        if (!s.isEmpty()) {
            o1 = s.pop();
            s.push(o1);
            s.push(o1);
        }
        break;
    case Opcodes.DUP_X1:
        o1 = s.pop();
        o2 = s.pop();
        s.push(o1);
        s.push(o2);
        s.push(o1);
        break;
    case Opcodes.DUP_X2:
        o1 = s.pop();
        o2 = s.pop();
        o3 = s.pop();
        s.push(o1);
        s.push(o3);
        s.push(o2);
        s.push(o1);
        break;
    case Opcodes.DUP2:
        l1 = s.popWord();
        if (l1.isEmpty()) {
            log.trace("DUP2 with unknown operand"); //$NON-NLS-1$
            s.clear();
        } else {
            s.pushWord(l1);
            s.pushWord(l1);
        }
        break;
    case Opcodes.DUP2_X1:
        l1 = s.popWord();
        o1 = s.pop();
        if (l1.isEmpty()) {
            log.trace("DUP2 with unknown operand"); //$NON-NLS-1$
            s.clear();
        } else {
            s.pushWord(l1);
            s.push(o1);
            s.pushWord(l1);
        }
        break;
    case Opcodes.DUP2_X2:
        l1 = s.popWord();
        l2 = s.popWord();
        if (l1.isEmpty() || l2.isEmpty()) {
            log.trace("DUP2 with unknown operand"); //$NON-NLS-1$
            s.clear();
        } else {
            s.pushWord(l1);
            s.pushWord(l2);
            s.pushWord(l1);
        }
        break;

    case Opcodes.SWAP:
        o1 = s.pop();
        o2 = s.pop();
        s.push(o1);
        s.push(o2);
        break;

    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
    case Opcodes.IAND:
    case Opcodes.LAND:
    case Opcodes.IOR:
    case Opcodes.LOR:
    case Opcodes.IXOR:
    case Opcodes.LXOR:
    case Opcodes.LCMP:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
    case Opcodes.DCMPL:
    case Opcodes.DCMPG:
        s.merge(2);
        break;

    case Opcodes.ISHL:
    case Opcodes.LSHL:
    case Opcodes.ISHR:
    case Opcodes.LSHR:
    case Opcodes.IUSHR:
    case Opcodes.LUSHR:
        s.pop(); // amount
        // ignore value
        break;

    case Opcodes.INEG:
    case Opcodes.F2I:
    case Opcodes.D2I:
    case Opcodes.L2I:
        s.push(cast(s.pop(), Type.INT_TYPE));
        break;

    case Opcodes.LNEG:
    case Opcodes.I2L:
    case Opcodes.F2L:
    case Opcodes.D2L:
        s.push(cast(s.pop(), Type.LONG_TYPE));
        break;

    case Opcodes.FNEG:
    case Opcodes.I2F:
    case Opcodes.L2F:
    case Opcodes.D2F:
        s.push(cast(s.pop(), Type.FLOAT_TYPE));

    case Opcodes.DNEG:
    case Opcodes.I2D:
    case Opcodes.L2D:
    case Opcodes.F2D:
        s.push(cast(s.pop(), Type.DOUBLE_TYPE));

    case Opcodes.I2B:
        s.push(cast(s.pop(), Type.BYTE_TYPE));
        break;
    case Opcodes.I2C:
        s.push(cast(s.pop(), Type.CHAR_TYPE));
        break;
    case Opcodes.I2S:
        s.push(cast(s.pop(), Type.SHORT_TYPE));
        break;

    case Opcodes.ARETURN:
        s.clear();
        break;

    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.RETURN:
        if (log.isTraceEnabled()) {
            log.trace("Found return " + s.pop()); //$NON-NLS-1$
        }
        s.clear();
        break;

    case Opcodes.ATHROW:
        Object thrw = s.pop();
        log.trace("Found throw " + thrw); //$NON-NLS-1$
        s.clear();
        break;

    default:
        log.warn("Unsupported instruction code " + opcode); //$NON-NLS-1$
    }
}