Search in sources :

Example 1 with ASTCondition

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

the class CPApplication method inASTForLoopNode.

public void inASTForLoopNode(ASTForLoopNode node) {
    /*
		 * For the init part we should actually use the before set for each init
		 * stmt
		 */
    for (AugmentedStmt as : node.getInit()) {
        Stmt s = as.get_Stmt();
        List useBoxes = s.getUseBoxes();
        Object obj = cp.getBeforeSet(s);
        if (obj == null)
            continue;
        if (!(obj instanceof CPFlowSet))
            continue;
        // before set is a non null CPFlowSet
        CPFlowSet beforeSet = (CPFlowSet) obj;
        // System.out.println("Init Statement: "+s);
        // System.out.println("Before set is: "+beforeSet.toString());
        /*
			 * get all use boxes see if their value is determined from the
			 * before set if yes replace them
			 */
        substituteUses(useBoxes, beforeSet);
    }
    // get after set for the condition and update
    Object obj = cp.getAfterSet(node);
    if (obj == null)
        return;
    if (!(obj instanceof CPFlowSet))
        return;
    // after set is a non null CPFlowSet
    CPFlowSet afterSet = (CPFlowSet) obj;
    // conditon
    ASTCondition cond = node.get_Condition();
    // System.out.println("For Loop with condition: "+cond);
    // System.out.println("After set is: "+afterSet.toString());
    changedCondition(cond, afterSet);
    // update
    for (AugmentedStmt as : node.getUpdate()) {
        Stmt s = as.get_Stmt();
        List useBoxes = s.getUseBoxes();
        // System.out.println("For update Statement: "+s);
        // System.out.println("After set is: "+afterSet.toString());
        /*
			 * get all use boxes see if their value is determined from the
			 * before set if yes replace them
			 */
        substituteUses(useBoxes, afterSet);
    }
}
Also used : List(java.util.List) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ASTCondition(soot.dava.internal.AST.ASTCondition) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) Stmt(soot.jimple.Stmt)

Example 2 with ASTCondition

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

the class CPApplication method inASTIfNode.

public void inASTIfNode(ASTIfNode node) {
    // System.out.println(node);
    Object obj = cp.getBeforeSet(node);
    if (obj == null)
        return;
    if (!(obj instanceof CPFlowSet))
        return;
    // before set is a non null CPFlowSet
    CPFlowSet beforeSet = (CPFlowSet) obj;
    // System.out.println("Printing before Set for IF"+beforeSet.toString());
    ASTCondition cond = node.get_Condition();
    // System.out.println("If Statement with condition: "+cond);
    // System.out.println("Before set is: "+beforeSet.toString());
    changedCondition(cond, beforeSet);
}
Also used : ASTCondition(soot.dava.internal.AST.ASTCondition) CPFlowSet(soot.dava.toolkits.base.AST.structuredAnalysis.CPFlowSet)

Example 3 with ASTCondition

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

the class IfElseSplitter method outASTIfElseNode.

public void outASTIfElseNode(ASTIfElseNode node) {
    // if some pattern has already matched cant do another one in this go
    if (transform)
        return;
    List<Object> subBodies = node.get_SubBodies();
    if (subBodies.size() != 2)
        throw new DecompilationException("IfelseNode without two subBodies. report to developer");
    List<Object> ifBody = (List<Object>) subBodies.get(0);
    List<Object> elseBody = (List<Object>) subBodies.get(1);
    boolean patternMatched = tryBodyPattern(ifBody, node.get_Label(), elseBody);
    List<Object> newIfBody = null;
    List<Object> outerScopeBody = null;
    boolean negateIfCondition = false;
    if (patternMatched) {
        if (DEBUG)
            System.out.println("First pattern matched");
        newIfBody = ifBody;
        outerScopeBody = elseBody;
        negateIfCondition = false;
    } else {
        patternMatched = tryBodyPattern(elseBody, node.get_Label(), ifBody);
        if (patternMatched) {
            if (DEBUG)
                System.out.println("Second pattern matched");
            newIfBody = elseBody;
            outerScopeBody = ifBody;
            negateIfCondition = true;
        }
    }
    // if at this point newIfBody and outerScopeBody are non null we got ourselves a transformation :)
    if (newIfBody != null && outerScopeBody != null) {
        ASTCondition cond = node.get_Condition();
        if (negateIfCondition)
            cond.flip();
        ASTIfNode newNode = new ASTIfNode(node.get_Label(), cond, newIfBody);
        if (DEBUG) {
            System.out.println("New IF Node is: " + newNode.toString());
            System.out.println("Outer scope body list is:\n");
            for (int i = 0; i < outerScopeBody.size(); i++) System.out.println("\n\n " + outerScopeBody.get(i).toString());
        }
        ASTParentNodeFinder finder = new ASTParentNodeFinder();
        methodNode.apply(finder);
        Object returned = finder.getParentOf(node);
        if (returned == null) {
            // coundnt find parent so cant do anything
            return;
        }
        /*
			 * Setting globals since everything is ready for transformation
			 * BECAUSE we cant modify the parent here we are going to do some 
			 * bad coding style
			 * store the information needed for this into globals
			 * set a flag
			 * and the outASTMethod checks for this
			 */
        parent = (ASTNode) returned;
        toReplace = node;
        toInsert = newNode;
        bodyAfterInsert = outerScopeBody;
        transform = true;
    }
}
Also used : ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) DecompilationException(soot.dava.DecompilationException) List(java.util.List) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 4 with ASTCondition

use of soot.dava.internal.AST.ASTCondition 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;
}
Also used : ASTControlFlowNode(soot.dava.internal.AST.ASTControlFlowNode) DNotExpr(soot.dava.internal.javaRep.DNotExpr) Value(soot.Value) ASTNode(soot.dava.internal.AST.ASTNode) ASTUnaryCondition(soot.dava.internal.AST.ASTUnaryCondition) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 5 with ASTCondition

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

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