Search in sources :

Example 1 with ASTOrCondition

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

the class SimplifyConditions method applyDeMorgans.

/*
     * !z0 && !z1  ---->   !(z0 || z1)
     * !z0 || !z1  ---->   !(z0 && z1)
     * 
     * Send null if no change else send new condition CONDITION
     */
public ASTCondition applyDeMorgans(ASTAggregatedCondition aggCond) {
    ASTCondition left = aggCond.getLeftOp();
    ASTCondition right = aggCond.getRightOp();
    if (aggCond.isNotted() && left instanceof ASTBinaryCondition && right instanceof ASTBinaryCondition) {
        // we can remove the not sign by simply flipping the two conditions
        // ! (  x==y &&  a<b )
        left.flip();
        right.flip();
        if (aggCond instanceof ASTAndCondition)
            aggCond = new ASTOrCondition(left, right);
        else
            aggCond = new ASTAndCondition(left, right);
        return aggCond;
    }
    if ((left.isNotted() && right.isNotted() && (!(left instanceof ASTBinaryCondition) && !(right instanceof ASTBinaryCondition))) || (left.isNotted() && aggCond.isNotted() && !(left instanceof ASTBinaryCondition)) || (right.isNotted() && aggCond.isNotted() && !(right instanceof ASTBinaryCondition))) {
        // both are notted and atleast one is not a binaryCondition
        left.flip();
        right.flip();
        ASTAggregatedCondition newCond;
        if (aggCond instanceof ASTAndCondition)
            newCond = new ASTOrCondition(left, right);
        else
            newCond = new ASTAndCondition(left, right);
        if (aggCond.isNotted())
            return newCond;
        else {
            newCond.flip();
            return newCond;
        }
    }
    return null;
}
Also used : ASTBinaryCondition(soot.dava.internal.AST.ASTBinaryCondition) ASTAggregatedCondition(soot.dava.internal.AST.ASTAggregatedCondition) ASTCondition(soot.dava.internal.AST.ASTCondition) ASTAndCondition(soot.dava.internal.AST.ASTAndCondition) ASTOrCondition(soot.dava.internal.AST.ASTOrCondition)

Example 2 with ASTOrCondition

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

Aggregations

ASTAndCondition (soot.dava.internal.AST.ASTAndCondition)2 ASTCondition (soot.dava.internal.AST.ASTCondition)2 ASTOrCondition (soot.dava.internal.AST.ASTOrCondition)2 ASTAggregatedCondition (soot.dava.internal.AST.ASTAggregatedCondition)1 ASTBinaryCondition (soot.dava.internal.AST.ASTBinaryCondition)1 ASTUnaryCondition (soot.dava.internal.AST.ASTUnaryCondition)1