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);
}
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;
}
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");
}
}
Aggregations