Example usage for org.objectweb.asm Opcodes IFEQ

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

Introduction

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

Prototype

int IFEQ

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

Click Source Link

Usage

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

License:Open Source License

private static JavaTypeName encodeTernaryOperatorExpr(
        JavaExpression.OperatorExpression.Ternary ternaryOperatorExpr, GenerationContext context)
        throws JavaGenerationException {

    MethodVisitor mv = context.getMethodVisitor();

    JavaExpression conditionExpr = ternaryOperatorExpr.getArgument(0);
    JavaExpression thenExpr = ternaryOperatorExpr.getArgument(1);
    JavaExpression elseExpr = ternaryOperatorExpr.getArgument(2);

    if (conditionExpr instanceof JavaExpression.OperatorExpression) {

        //generate more efficient code in the case of (boolean-valued-operator) ? thenExpr : elseExpr 

        //This case exists to handle the special case where an operator occurs as the child of an if-then-else (or ternary operator)
        //conditional. For example, in the situation:
        //// ww w .j ava 2 s . c o  m
        // (x != null) ? thenExpr : elseExpr
        // 
        // we do not want to evaluate x != null to a boolean value, push that value on the stack,
        // and then test it prior to selecting the correct branch. Rather, we can combine the evaluation
        // and jump operations into a single step.

        JavaExpression.OperatorExpression operatorExpr = (JavaExpression.OperatorExpression) conditionExpr;

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

        Label trueContinuation = new Label();
        Label falseContinuation = new Label();

        if (operator.isLogicalOp() || operator.isRelationalOp()) {

            encodeBooleanValuedOperatorHelper(operatorExpr, context, trueContinuation, falseContinuation);
            return encodeThenExprElseExpr(trueContinuation, falseContinuation, thenExpr, elseExpr, context);
        }

        throw new JavaGenerationException("Unrecognized boolean-valued conditional operator " + symbol + ".");
    }

    //encode the boolean conditional
    encodeExpr(conditionExpr, context);

    Label falseContinuation = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, falseContinuation);

    return encodeThenExprElseExpr(null, falseContinuation, thenExpr, elseExpr, context);
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation freturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opcode, final MethodVisitor mv) {
            // Strategy translated from jumble BCEL code
            // The following is complicated by the problem of NaNs. By default
            // the new value is -(x + 1), but this doesn't work for NaNs. But
            // for a NaN x != x is true, and we use this to detect them.
            mv.visitInsn(Opcodes.DUP);//ww w.  j a va 2 s .  com
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.FCMPG);
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, l1);
            mv.visitInsn(Opcodes.POP);
            mv.visitInsn(Opcodes.FCONST_0);
            mv.visitLabel(l1);
            mv.visitInsn(Opcodes.FCONST_1);
            mv.visitInsn(Opcodes.FADD);
            mv.visitInsn(Opcodes.FNEG);
            mv.visitInsn(Opcodes.FRETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "replaced return of float value with -(x + 1) for " + methodInfo.getDescription();
        }

    };
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation dreturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opCode, final MethodVisitor mv) {
            // Strategy translated from jumble BCEL code
            // The following is complicated by the problem of NaNs. By default
            // the new value is -(x + 1), but this doesn't work for NaNs. But
            // for a NaN x != x is true, and we use this to detect them.
            mv.visitInsn(Opcodes.DUP2);/*from  www .  j a  v  a2 s . c  o  m*/
            mv.visitInsn(Opcodes.DUP2);
            mv.visitInsn(Opcodes.DCMPG);
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, l1);
            mv.visitInsn(Opcodes.POP2);
            mv.visitInsn(Opcodes.DCONST_0);
            mv.visitLabel(l1);
            mv.visitInsn(Opcodes.DCONST_1);
            mv.visitInsn(Opcodes.DADD);
            mv.visitInsn(Opcodes.DNEG);
            mv.visitInsn(Opcodes.DRETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "replaced return of double value with -(x + 1) for " + methodInfo.getDescription();
        }

    };
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation ireturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opCode, final MethodVisitor mv) {
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, l1);
            mv.visitInsn(Opcodes.ICONST_0);
            mv.visitInsn(Opcodes.IRETURN);
            mv.visitLabel(l1);/*from   www  .jav  a  2 s  .  com*/
            mv.visitInsn(Opcodes.ICONST_1);
            mv.visitInsn(Opcodes.IRETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "replaced return of integer sized value with (x == 0 ? 1 : 0)";
        }

    };
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGBuilderTest.java

License:Open Source License

@Test
public void visited_label_should_be_assigned_to_true_successor() throws Exception {
    Label label0 = new Label();
    Label label1 = new Label();
    BytecodeCFG cfg = new Instructions().visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label0)
            .visitJumpInsn(Opcodes.IFEQ, label0).visitInsn(Opcodes.ICONST_0).visitJumpInsn(Opcodes.GOTO, label1)
            .visitLabel(label0).visitInsn(Opcodes.ICONST_1).visitLabel(label1).visitInsn(Opcodes.IRETURN).cfg();

    BytecodeCFG.Block block3 = cfg.blocks.get(3);
    assertThat(block3.terminator.opcode).isEqualTo(Opcodes.IFEQ);
    assertThat(block3.falseSuccessor()).isNotNull().isSameAs(cfg.blocks.get(4));
    assertThat(block3.trueSuccessor()).isNotNull().isSameAs(cfg.blocks.get(2));
    assertThat(block3.successors).hasSize(2);
    assertThat(block3.successors()).hasSize(2);
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGBuilderTest.java

License:Open Source License

@Test
public void goto_successors() throws Exception {
    Label label0 = new Label();
    Label label1 = new Label();
    BytecodeCFG cfg = new Instructions().visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label0)
            .visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label1).visitVarInsn(Opcodes.ALOAD, 0)
            .visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFEQ, label0).visitInsn(Opcodes.ICONST_0)
            .visitJumpInsn(Opcodes.GOTO, label1).visitLabel(label0).visitInsn(Opcodes.ICONST_1)
            .visitLabel(label1).visitInsn(Opcodes.IRETURN).cfg();
    assertThat(cfg.blocks.get(6).successors).containsExactly(cfg.blocks.get(4));
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGConstructionTest.java

License:Open Source License

private static boolean isJumpInstruction(int opcode) {
    return Opcodes.IFEQ <= opcode && opcode <= LOOKUPSWITCH && opcode != RET || opcode == IFNULL
            || opcode == IFNONNULL;// w  w  w  .  j av a2s  .  c  o  m
}

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

License:Open Source License

@Test
public void test_compare_with_zero() {
    SymbolicValue sv = new SymbolicValue();
    int[] opcodes = { Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFGE };
    for (int opcode : opcodes) {
        ProgramState programState = walker.branchingState(new Instruction(opcode),
                ProgramState.EMPTY_STATE.stackValue(sv));
        RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
        assertThat(relSV.getLeftOp()).isSameAs(sv);
        assertThat(relSV.getRightOp()).isNotSameAs(sv);
        assertThat(programState.getConstraints(relSV.getRightOp())
                .hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
    }//w  ww  . j  a v  a 2 s.  co  m

    // these opcodes inverse operator and swap operands
    int[] swapOperandsOpcodes = { Opcodes.IFLE, Opcodes.IFGT };
    for (int opcode : swapOperandsOpcodes) {
        ProgramState programState = walker.branchingState(new Instruction(opcode),
                ProgramState.EMPTY_STATE.stackValue(sv));
        RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
        assertThat(relSV.getRightOp()).isSameAs(sv);
        assertThat(relSV.getLeftOp()).isNotSameAs(sv);
        assertThat(programState.getConstraints(relSV.getLeftOp())
                .hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
    }
}

From source file:org.spongepowered.asm.mixin.injection.callback.CallbackInjector.java

License:MIT License

/**
 * if (e.isCancelled()) return e.getReturnValue();
 * /*from   w w w  .j  a  va2 s. c om*/
 * @param callback callback handle
 */
protected void injectCancellationCode(final Callback callback) {
    if (!this.cancellable) {
        return;
    }

    callback.add(new VarInsnNode(Opcodes.ALOAD, callback.marshallVar));
    callback.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, callback.target.callbackInfoClass,
            CallbackInfo.getIsCancelledMethodName(), CallbackInfo.getIsCancelledMethodSig(), false));

    LabelNode notCancelled = new LabelNode();
    callback.add(new JumpInsnNode(Opcodes.IFEQ, notCancelled));

    // If this is a void method, just injects a RETURN opcode, otherwise we
    // need to get the return value from the EventInfo
    this.injectReturnCode(callback);

    callback.add(notCancelled);
}

From source file:org.spongepowered.asm.mixin.injection.points.JumpInsnPoint.java

License:MIT License

public JumpInsnPoint(InjectionPointData data) {
    this.opCode = data.getOpcode(-1, Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFGE, Opcodes.IFGT,
            Opcodes.IFLE, Opcodes.IF_ICMPEQ, Opcodes.IF_ICMPNE, Opcodes.IF_ICMPLT, Opcodes.IF_ICMPGE,
            Opcodes.IF_ICMPGT, Opcodes.IF_ICMPLE, Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE, Opcodes.GOTO,
            Opcodes.JSR, Opcodes.IFNULL, Opcodes.IFNONNULL, -1);
    this.ordinal = data.getOrdinal();
}