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