Search in sources :

Example 6 with ASTUnaryCondition

use of soot.dava.internal.AST.ASTUnaryCondition in project soot by Sable.

the class SimplifyConditions method simplifyTheCondition.

/*
	 * In a loop keep simplifying the condition as much as possible
	 * 
	 */
public ASTCondition simplifyTheCondition(ASTCondition cond) {
    if (cond instanceof ASTAggregatedCondition) {
        ASTAggregatedCondition aggCond = (ASTAggregatedCondition) cond;
        ASTCondition leftCond = simplifyTheCondition(aggCond.getLeftOp());
        ASTCondition rightCond = simplifyTheCondition(aggCond.getRightOp());
        // if returned values are non null then set leftop /rightop to new condition
        if (leftCond != null) {
            aggCond.setLeftOp(leftCond);
        }
        if (rightCond != null) {
            aggCond.setRightOp(rightCond);
        }
        ASTCondition returned = simplifyIfAtleastOneConstant(aggCond);
        if (returned != null) {
            changed = true;
            return returned;
        }
        returned = applyDeMorgans(aggCond);
        if (returned != null) {
            changed = true;
            return returned;
        }
        return aggCond;
    } else if (cond instanceof ASTUnaryCondition) {
        // dont do anything with unary conditions
        ASTUnaryCondition unary = (ASTUnaryCondition) cond;
        /*
			 * if unary is a noted constant simplify it
			 * !true to be converted to false
			 * !false to be converted to true
			 */
        Value unaryVal = unary.getValue();
        if (unaryVal instanceof DNotExpr) {
            if (DEBUG)
                System.out.println("Found NotExpr in unary COndition" + unaryVal);
            DNotExpr notted = (DNotExpr) unaryVal;
            Value internal = notted.getOp();
            Boolean isIt = isBooleanConstant(internal);
            if (isIt != null) {
                // convert !true to false
                if (isIt.booleanValue()) {
                    // true
                    if (DEBUG)
                        System.out.println("CONVERTED !true to false");
                    changed = true;
                    return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
                } else if (!isIt.booleanValue()) {
                    // false
                    if (DEBUG)
                        System.out.println("CONVERTED !false to true");
                    changed = true;
                    return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
                } else
                    throw new RuntimeException("BooleanType found with value different than 0 or 1");
            } else {
                if (DEBUG)
                    System.out.println("Not boolean type");
            }
        }
        return unary;
    } else if (cond instanceof ASTBinaryCondition) {
        ASTBinaryCondition binary = (ASTBinaryCondition) cond;
        ConditionExpr expr = binary.getConditionExpr();
        // returns null if no change
        ASTUnaryCondition temp = evaluateBinaryCondition(expr);
        if (DEBUG)
            System.out.println("changed binary condition " + cond + " to" + temp);
        if (temp != null)
            changed = true;
        return temp;
    } else {
        throw new RuntimeException("Method getUseList in ASTUsesAndDefs encountered unknown condition type");
    }
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) DNotExpr(soot.dava.internal.javaRep.DNotExpr) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value) ASTAggregatedCondition(soot.dava.internal.AST.ASTAggregatedCondition) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 7 with ASTUnaryCondition

use of soot.dava.internal.AST.ASTUnaryCondition in project soot by Sable.

the class SimplifyConditions method evaluateBinaryCondition.

// return condition if was able to simplify (convert to a boolean true or false) else null
public ASTUnaryCondition evaluateBinaryCondition(ConditionExpr expr) {
    String symbol = expr.getSymbol();
    int op = -1;
    if (symbol.indexOf("==") > -1) {
        if (DEBUG)
            System.out.println("==");
        op = 1;
    } else if (symbol.indexOf(">=") > -1) {
        if (DEBUG)
            System.out.println(">=");
        op = 2;
    } else if (symbol.indexOf('>') > -1) {
        if (DEBUG)
            System.out.println(">");
        op = 3;
    } else if (symbol.indexOf("<=") > -1) {
        if (DEBUG)
            System.out.println("<=");
        op = 4;
    } else if (symbol.indexOf('<') > -1) {
        if (DEBUG)
            System.out.println("<");
        op = 5;
    } else if (symbol.indexOf("!=") > -1) {
        if (DEBUG)
            System.out.println("!=");
        op = 6;
    }
    Value leftOp = expr.getOp1();
    Value rightOp = expr.getOp2();
    Boolean result = null;
    if (leftOp instanceof LongConstant && rightOp instanceof LongConstant) {
        if (DEBUG)
            System.out.println("long constants!!");
        long left = ((LongConstant) leftOp).value;
        long right = ((LongConstant) rightOp).value;
        result = longSwitch(op, left, right);
    } else if (leftOp instanceof DoubleConstant && rightOp instanceof DoubleConstant) {
        double left = ((DoubleConstant) leftOp).value;
        double right = ((DoubleConstant) rightOp).value;
        result = doubleSwitch(op, left, right);
    } else if (leftOp instanceof FloatConstant && rightOp instanceof FloatConstant) {
        float left = ((FloatConstant) leftOp).value;
        float right = ((FloatConstant) rightOp).value;
        result = floatSwitch(op, left, right);
    } else if (leftOp instanceof IntConstant && rightOp instanceof IntConstant) {
        int left = ((IntConstant) leftOp).value;
        int right = ((IntConstant) rightOp).value;
        result = intSwitch(op, left, right);
    }
    if (result != null) {
        if (result.booleanValue())
            return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
        else
            return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
    }
    return null;
}
Also used : LongConstant(soot.jimple.LongConstant) DoubleConstant(soot.jimple.DoubleConstant) FloatConstant(soot.jimple.FloatConstant) Value(soot.Value) IntConstant(soot.jimple.IntConstant) DIntConstant(soot.dava.internal.javaRep.DIntConstant) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition)

Aggregations

ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)7 Value (soot.Value)6 ASTCondition (soot.dava.internal.AST.ASTCondition)5 DNotExpr (soot.dava.internal.javaRep.DNotExpr)4 ASTBinaryCondition (soot.dava.internal.AST.ASTBinaryCondition)3 Local (soot.Local)2 ASTAggregatedCondition (soot.dava.internal.AST.ASTAggregatedCondition)2 ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)2 ConditionExpr (soot.jimple.ConditionExpr)2 List (java.util.List)1 SootField (soot.SootField)1 ASTAndCondition (soot.dava.internal.AST.ASTAndCondition)1 ASTNode (soot.dava.internal.AST.ASTNode)1 ASTOrCondition (soot.dava.internal.AST.ASTOrCondition)1 ASTTryNode (soot.dava.internal.AST.ASTTryNode)1 DIntConstant (soot.dava.internal.javaRep.DIntConstant)1 DoubleConstant (soot.jimple.DoubleConstant)1 FieldRef (soot.jimple.FieldRef)1 FloatConstant (soot.jimple.FloatConstant)1 IntConstant (soot.jimple.IntConstant)1