Search in sources :

Example 11 with ASTCondition

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

the class SimplifyConditions method fixedPoint.

public void fixedPoint(ASTControlFlowNode node) {
    ASTCondition returned;
    do {
        if (DEBUG)
            System.out.println("Invoking simplify");
        changed = false;
        ASTCondition cond = node.get_Condition();
        returned = simplifyTheCondition(cond);
        if (returned != null)
            node.set_Condition(returned);
    } while (changed);
}
Also used : ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 12 with ASTCondition

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

the class SimplifyConditions method simplifyIfAtleastOneConstant.

/*
     * When this method is invoked we are sure that there are no occurences of !true or !false since
     * this is AFTER doing depth first of the children so the unaryCondition must have simplified the above
     * 
     * Return Null if no change else return changed condition
     */
public ASTCondition simplifyIfAtleastOneConstant(ASTAggregatedCondition aggCond) {
    ASTCondition left = aggCond.getLeftOp();
    ASTCondition right = aggCond.getRightOp();
    Boolean leftBool = null;
    Boolean rightBool = null;
    if (left instanceof ASTUnaryCondition)
        leftBool = isBooleanConstant(((ASTUnaryCondition) left).getValue());
    if (right instanceof ASTUnaryCondition)
        rightBool = isBooleanConstant(((ASTUnaryCondition) right).getValue());
    /*
		 *  a && b NOCHANGE    DONE
		 *  b && a NOCHANGE	 DONE
		 *              
		 *  a || b NOCHANGE DONE 
		 *  b || a NOCHANGE DONE
		 *  
		 */
    if (leftBool == null && rightBool == null) {
        // meaning both are not constants
        return null;
    }
    if (aggCond instanceof ASTAndCondition) {
        if (leftBool != null && rightBool != null) {
            // meaning both are constants
            if (leftBool.booleanValue() && rightBool.booleanValue()) {
                // both are true
                return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
            } else {
                // atleast one of the two is false
                return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
            }
        }
        if (leftBool != null) {
            // condition passed
            if (leftBool.booleanValue()) {
                // condition.......just return the right condition
                return right;
            } else {
                // return a unary false
                return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
            }
        }
        if (rightBool != null) {
            // implicityly means that the leftBool is null
            if (rightBool.booleanValue()) {
                // rightBool is true so it all depends on left
                return left;
            } else {
                // remove the leftBool since there might be side effects
                return aggCond;
            }
        }
    } else if (aggCond instanceof ASTOrCondition) {
        /*
			 * 
			 * true || false ---> true    DONE 
			 * true || true --> true      DONE
			 * false || true --> true     DONE
			 * false || false ---> false  DONE
			 * 
			 * 
			 * true || b ----> true DONE
			 * false || b -----> b   DONE
			 *   
			 * b || true ---> b || true .... although we know the condition is true we have to evaluate b because of possible side effects   DONE 
			 * b || false ---> b     DONE
			 * 
			 */
        if (leftBool != null && rightBool != null) {
            // meaning both are constants
            if (!leftBool.booleanValue() && !rightBool.booleanValue()) {
                // both are false
                return new ASTUnaryCondition(DIntConstant.v(0, BooleanType.v()));
            } else {
                // atleast one of the two is true
                return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
            }
        }
        if (leftBool != null) {
            // condition passed
            if (leftBool.booleanValue()) {
                // left bool is true that means we will stop evaluation of condition, just return true
                return new ASTUnaryCondition(DIntConstant.v(1, BooleanType.v()));
            } else {
                // left bool is false so we have to continue evaluating right
                return right;
            }
        }
        if (rightBool != null) {
            // implicityly means that the leftBool is null
            if (rightBool.booleanValue()) {
                // rightBool is true but leftBool must be evaluated beforehand
                return aggCond;
            } else {
                // rightBool is false so everything depends on left
                return left;
            }
        }
    } else
        throw new RuntimeException("Found unknown aggregated condition");
    return null;
}
Also used : ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition) ASTAndCondition(soot.dava.internal.AST.ASTAndCondition) ASTOrCondition(soot.dava.internal.AST.ASTOrCondition)

Example 13 with ASTCondition

use of soot.dava.internal.AST.ASTCondition 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)

Aggregations

ASTCondition (soot.dava.internal.AST.ASTCondition)13 ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)5 CPFlowSet (soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)5 Value (soot.Value)4 List (java.util.List)3 ASTAggregatedCondition (soot.dava.internal.AST.ASTAggregatedCondition)3 ASTBinaryCondition (soot.dava.internal.AST.ASTBinaryCondition)3 DNotExpr (soot.dava.internal.javaRep.DNotExpr)3 ASTAndCondition (soot.dava.internal.AST.ASTAndCondition)2 ASTControlFlowNode (soot.dava.internal.AST.ASTControlFlowNode)2 ASTOrCondition (soot.dava.internal.AST.ASTOrCondition)2 Local (soot.Local)1 SootField (soot.SootField)1 DecompilationException (soot.dava.DecompilationException)1 ASTIfNode (soot.dava.internal.AST.ASTIfNode)1 ASTNode (soot.dava.internal.AST.ASTNode)1 ASTTryNode (soot.dava.internal.AST.ASTTryNode)1 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)1 ASTParentNodeFinder (soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder)1 ConditionExpr (soot.jimple.ConditionExpr)1