use of soot.dava.internal.javaRep.DNotExpr in project soot by Sable.
the class EliminateConditions method eliminate.
public Boolean eliminate(ASTNode node) {
ASTCondition cond = null;
if (node instanceof ASTControlFlowNode)
cond = ((ASTControlFlowNode) node).get_Condition();
else
return null;
if (cond == null || !(cond instanceof ASTUnaryCondition))
return null;
ASTUnaryCondition unary = (ASTUnaryCondition) cond;
Value unaryValue = unary.getValue();
boolean notted = false;
if (unaryValue instanceof DNotExpr) {
notted = true;
unaryValue = ((DNotExpr) unaryValue).getOp();
}
Boolean isBoolean = isBooleanConstant(unaryValue);
if (isBoolean == null) {
// not a constant
return null;
}
boolean trueOrFalse = isBoolean.booleanValue();
if (notted) {
// since it is notted we reverse the booleans
trueOrFalse = !trueOrFalse;
}
AST.apply(finder);
Object temp = finder.getParentOf(node);
if (temp == null)
return null;
ASTNode parent = (ASTNode) temp;
List<Object> subBodies = parent.get_SubBodies();
Iterator<Object> it = subBodies.iterator();
int index = -1;
while (it.hasNext()) {
bodyContainingNode = (List<Object>) it.next();
index = bodyContainingNode.indexOf(node);
if (index < 0) {
bodyContainingNode = null;
} else {
// bound the body containing Node
return new Boolean(trueOrFalse);
}
}
return null;
}
use of soot.dava.internal.javaRep.DNotExpr in project soot by Sable.
the class CP method checkForValueHints.
/*
* The isElseBranch flag is true if the caller is the else branch of the
* ifelse statement. In that case we might be able to send something for the
* else branch
*/
public CPTuple checkForValueHints(ASTCondition cond, CPFlowSet input, boolean isElseBranch) {
if (cond instanceof ASTUnaryCondition) {
// check for lone boolean if(notDone)
ASTUnaryCondition unary = (ASTUnaryCondition) cond;
Value unaryValue = unary.getValue();
boolean NOTTED = false;
// Get the real value if this is a notted expression
if (unaryValue instanceof DNotExpr) {
unaryValue = ((DNotExpr) unaryValue).getOp();
NOTTED = true;
}
if (!(unaryValue instanceof Local)) {
// inset
return null;
}
// the unary value is a local add the value to the inset which woul
// dbe present in the if branch
CPVariable variable = new CPVariable((Local) unaryValue);
// false and vice verse
if (!isElseBranch) {
// we are in the if branch hence notted true would mean the
// variable is actually false here
Boolean boolVal = new Boolean(!NOTTED);
return new CPTuple(localClassName, variable, boolVal);
} else {
// in the else branch NOTTED true means the variable is true
Boolean boolVal = new Boolean(NOTTED);
return new CPTuple(localClassName, variable, boolVal);
}
} else if (cond instanceof ASTBinaryCondition) {
ASTBinaryCondition binary = (ASTBinaryCondition) cond;
ConditionExpr expr = binary.getConditionExpr();
Boolean equal = null;
String symbol = expr.getSymbol();
if (symbol.indexOf("==") > -1) {
// System.out.println("!!!!!!!!!1 FOUND == in binary comparison operaiton");
equal = new Boolean(true);
} else if (symbol.indexOf("!=") > -1) {
equal = new Boolean(false);
// System.out.println("!!!!!!!!!!!!!! FOUND != in binary comparison operaiton");
} else {
// System.out.println("symbol is"+symbol);
return null;
}
// we have a comparison the truth value of equal tells whether we
// are doing == or !=
Value a = expr.getOp1();
Value b = expr.getOp2();
// see if its possible to deduce a hint from these values
CPTuple tuple = createCPTupleIfPossible(a, b, input);
// if the tuple is not created
if (tuple == null)
return null;
// we have to make sure is that the == and != are taken into account
if (equal.booleanValue()) {
// branch a is in fact equal to b
if (!isElseBranch)
return tuple;
else
return null;
} else {
if (isElseBranch)
return tuple;
else
return null;
}
}
return null;
}
use of soot.dava.internal.javaRep.DNotExpr in project soot by Sable.
the class EliminateConditions method eliminateForTry.
public Boolean eliminateForTry(ASTNode node) {
ASTCondition cond = null;
if (node instanceof ASTControlFlowNode)
cond = ((ASTControlFlowNode) node).get_Condition();
else
return null;
if (cond == null || !(cond instanceof ASTUnaryCondition))
return null;
ASTUnaryCondition unary = (ASTUnaryCondition) cond;
Value unaryValue = unary.getValue();
boolean notted = false;
if (unaryValue instanceof DNotExpr) {
notted = true;
unaryValue = ((DNotExpr) unaryValue).getOp();
}
Boolean isBoolean = isBooleanConstant(unaryValue);
if (isBoolean == null) {
// not a constant
return null;
}
boolean trueOrFalse = isBoolean.booleanValue();
if (notted) {
// since it is notted we reverse the booleans
trueOrFalse = !trueOrFalse;
}
AST.apply(finder);
Object temp = finder.getParentOf(node);
if (temp == null)
return null;
if (!(temp instanceof ASTTryNode))
throw new RuntimeException("eliminateTry called when parent was not a try node");
ASTTryNode parent = (ASTTryNode) temp;
List<Object> tryBody = parent.get_TryBody();
int index = tryBody.indexOf(node);
if (index >= 0) {
// bound the body containing Node
bodyContainingNode = tryBody;
return new Boolean(trueOrFalse);
}
List<Object> catchList = parent.get_CatchList();
Iterator<Object> it = catchList.iterator();
while (it.hasNext()) {
ASTTryNode.container catchBody = (ASTTryNode.container) it.next();
List<Object> body = (List<Object>) catchBody.o;
index = body.indexOf(node);
if (index >= 0) {
// bound the body containing Node
bodyContainingNode = body;
return new Boolean(trueOrFalse);
}
}
return null;
}
use of soot.dava.internal.javaRep.DNotExpr 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