Search in sources :

Example 11 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class ShortcutArrayInit method inASTStatementSequenceNode.

public void inASTStatementSequenceNode(ASTStatementSequenceNode node) {
    debug("inASTStatementSequenceNode");
    boolean success = false;
    ArrayList<AugmentedStmt> toRemove = new ArrayList<AugmentedStmt>();
    for (AugmentedStmt as : node.getStatements()) {
        success = false;
        Stmt s = as.get_Stmt();
        if (!(s instanceof DefinitionStmt))
            continue;
        DefinitionStmt ds = (DefinitionStmt) s;
        ValueBox right = ds.getRightOpBox();
        Value rightValue = right.getValue();
        if (!(rightValue instanceof NewArrayExpr))
            continue;
        debug("Found a new ArrayExpr" + rightValue);
        debug("Type of array is:" + rightValue.getType());
        // get type out
        Type arrayType = rightValue.getType();
        // get size....need to know this statically for sure!!!
        Value size = ((NewArrayExpr) rightValue).getSize();
        if (!(size instanceof IntConstant))
            continue;
        if (((IntConstant) size).value == 0) {
            debug("Size of array is 0 dont do anything");
            continue;
        }
        if (DEBUG)
            System.out.println("Size of array is: " + ((IntConstant) size).value);
        Iterator<AugmentedStmt> tempIt = node.getStatements().iterator();
        // get to the array creation stmt
        while (tempIt.hasNext()) {
            AugmentedStmt tempAs = tempIt.next();
            Stmt tempS = tempAs.get_Stmt();
            if (tempS.equals(s))
                break;
        }
        // have the size have the type, tempIt is poised at the current def
        // stmt
        ValueBox[] array = new ValueBox[((IntConstant) size).value];
        success = true;
        for (int i = 0; i < ((IntConstant) size).value; i++) {
            if (!tempIt.hasNext()) {
                // since its end of the stmt seq node just return
                if (DEBUG)
                    System.out.println("returning");
                return;
            }
            AugmentedStmt aug = tempIt.next();
            Stmt augS = aug.get_Stmt();
            if (!isInSequenceAssignment(augS, ds.getLeftOp(), i)) {
                // initializations
                if (DEBUG)
                    System.out.println("Out of order assignment aborting attempt");
                success = false;
                break;
            } else {
                if (DEBUG)
                    System.out.println("Assignment stmt in order adding to array");
                // the augS is the next assignment in the sequence add to
                // ValueBox array
                array[i] = ((DefinitionStmt) augS).getRightOpBox();
                toRemove.add(aug);
            }
        }
        if (success) {
            DArrayInitExpr tempExpr = new DArrayInitExpr(array, arrayType);
            DArrayInitValueBox tempValueBox = new DArrayInitValueBox(tempExpr);
            DAssignStmt newStmt = new DAssignStmt(ds.getLeftOpBox(), tempValueBox);
            // stmt
            if (DEBUG)
                System.out.println("Created new DAssignStmt and replacing it");
            InitializationDeclarationShortcut shortcutChecker = new InitializationDeclarationShortcut(as);
            methodNode.apply(shortcutChecker);
            boolean possible = shortcutChecker.isShortcutPossible();
            if (possible) {
                if (DEBUG)
                    System.out.println("Shortcut is possible");
                // create shortcut stmt
                DShortcutAssignStmt newShortcutStmt = new DShortcutAssignStmt(newStmt, arrayType);
                as.set_Stmt(newShortcutStmt);
                // make sure to mark the local in the DVariableDeclarations
                // so that its not printed
                markLocal(ds.getLeftOp());
            }
            break;
        }
    }
    // end going through stmt seq node
    if (success) {
        // means we did a transformation remove the stmts
        List<AugmentedStmt> newStmtList = new ArrayList<AugmentedStmt>();
        for (AugmentedStmt as : node.getStatements()) {
            if (toRemove.contains(as)) {
                toRemove.remove(as);
            } else {
                newStmtList.add(as);
            }
        }
        node.setStatements(newStmtList);
        // make sure any other possible simplifications are done
        inASTStatementSequenceNode(node);
        G.v().ASTTransformations_modified = true;
    }
    // try the second pattern also
    secondPattern(node);
}
Also used : DArrayInitValueBox(soot.dava.internal.javaRep.DArrayInitValueBox) InitializationDeclarationShortcut(soot.dava.toolkits.base.AST.traversals.InitializationDeclarationShortcut) ArrayList(java.util.ArrayList) DAssignStmt(soot.dava.internal.javaRep.DAssignStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DShortcutAssignStmt(soot.dava.internal.javaRep.DShortcutAssignStmt) DAssignStmt(soot.dava.internal.javaRep.DAssignStmt) Stmt(soot.jimple.Stmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DefinitionStmt(soot.jimple.DefinitionStmt) Type(soot.Type) DShortcutAssignStmt(soot.dava.internal.javaRep.DShortcutAssignStmt) NewArrayExpr(soot.jimple.NewArrayExpr) DArrayInitValueBox(soot.dava.internal.javaRep.DArrayInitValueBox) ValueBox(soot.ValueBox) Value(soot.Value) IntConstant(soot.jimple.IntConstant) DArrayInitExpr(soot.dava.internal.javaRep.DArrayInitExpr) DefinitionStmt(soot.jimple.DefinitionStmt)

Example 12 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class StructuredAnalysis method processASTForLoopNode.

public DavaFlowSet<E> processASTForLoopNode(ASTForLoopNode node, DavaFlowSet<E> input) {
    for (AugmentedStmt as : node.getInit()) {
        Stmt s = as.get_Stmt();
        input = process(s, input);
    }
    // finished processing the init part of the for loop
    DavaFlowSet<E> initialInput = cloneFlowSet(input);
    input = processCondition(node.get_Condition(), input);
    DavaFlowSet<E> lastin = null;
    String label = getLabel(node);
    DavaFlowSet<E> output2 = null;
    do {
        lastin = cloneFlowSet(input);
        // process body
        DavaFlowSet<E> output1 = processSingleSubBodyNode(node, input);
        // handle continues (Notice this is done before update!!!)
        output1 = handleContinue(label, output1, node);
        // notice that we dont merge with the initial output1 from
        // processing singleSubBody
        // the handlecontinue function takes care of it
        // handle update
        // if there is nothing in update
        output2 = cloneFlowSet(output1);
        for (AugmentedStmt as : node.getUpdate()) {
            Stmt s = as.get_Stmt();
            /*
				 * Since we are just going over a list of statements the output
				 * of each statement is the input of the next
				 */
            output2 = process(s, output2);
        }
        // output2 is the final result
        // merge this with the input
        input = merge(initialInput, output2);
        input = processCondition(node.get_Condition(), input);
    } while (isDifferent(lastin, input));
    // handle break
    return handleBreak(label, input, node);
}
Also used : AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnStmt(soot.jimple.ReturnStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) RetStmt(soot.jimple.RetStmt)

Example 13 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class StructuredAnalysis method processASTStatementSequenceNode.

public DavaFlowSet<E> processASTStatementSequenceNode(ASTStatementSequenceNode node, DavaFlowSet<E> input) {
    // needed if there are no stmts
    DavaFlowSet<E> output = cloneFlowSet(input);
    for (AugmentedStmt as : node.getStatements()) {
        Stmt s = as.get_Stmt();
        /*
			 * Since we are processing a list of statements the output of
			 * previous is input of next
			 */
        output = process(s, output);
        if (DEBUG_STATEMENTS) {
            System.out.println("After Processing statement " + s + output.toString());
            ;
        }
    }
    return output;
}
Also used : AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ReturnStmt(soot.jimple.ReturnStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) RetStmt(soot.jimple.RetStmt)

Example 14 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class DecrementIncrementStmtCreation method caseASTStatementSequenceNode.

public void caseASTStatementSequenceNode(ASTStatementSequenceNode node) {
    for (AugmentedStmt as : node.getStatements()) {
        // System.out.println(temp);
        Stmt s = as.get_Stmt();
        if (!(s instanceof DefinitionStmt))
            continue;
        // check if its i= i+1
        Value left = ((DefinitionStmt) s).getLeftOp();
        Value right = ((DefinitionStmt) s).getRightOp();
        if (right instanceof SubExpr) {
            Value op1 = ((SubExpr) right).getOp1();
            Value op2 = ((SubExpr) right).getOp2();
            if (left.toString().compareTo(op1.toString()) != 0) {
                // not the same
                continue;
            }
            // check if op2 is a constant with value 1 or -1
            if (op2 instanceof IntConstant) {
                if (((IntConstant) op2).value == 1) {
                    // this is i = i-1
                    DDecrementStmt newStmt = new DDecrementStmt(left, right);
                    as.set_Stmt(newStmt);
                } else if (((IntConstant) op2).value == -1) {
                    // this is i = i+1
                    DIncrementStmt newStmt = new DIncrementStmt(left, right);
                    as.set_Stmt(newStmt);
                }
            }
        } else if (right instanceof AddExpr) {
            Value op1 = ((AddExpr) right).getOp1();
            Value op2 = ((AddExpr) right).getOp2();
            if (left.toString().compareTo(op1.toString()) != 0) {
                continue;
            }
            // check if op2 is a constant with value 1 or -1
            if (op2 instanceof IntConstant) {
                if (((IntConstant) op2).value == 1) {
                    // this is i = i+1
                    DIncrementStmt newStmt = new DIncrementStmt(left, right);
                    as.set_Stmt(newStmt);
                } else if (((IntConstant) op2).value == -1) {
                    // this is i = i-1
                    DDecrementStmt newStmt = new DDecrementStmt(left, right);
                    as.set_Stmt(newStmt);
                }
            }
        }
    // right expr was addExpr
    }
// going through statements
}
Also used : DDecrementStmt(soot.dava.internal.javaRep.DDecrementStmt) DIncrementStmt(soot.dava.internal.javaRep.DIncrementStmt) SubExpr(soot.jimple.SubExpr) Value(soot.Value) IntConstant(soot.jimple.IntConstant) AddExpr(soot.jimple.AddExpr) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DefinitionStmt(soot.jimple.DefinitionStmt) DDecrementStmt(soot.dava.internal.javaRep.DDecrementStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DIncrementStmt(soot.dava.internal.javaRep.DIncrementStmt) DefinitionStmt(soot.jimple.DefinitionStmt) Stmt(soot.jimple.Stmt)

Example 15 with AugmentedStmt

use of soot.dava.internal.asg.AugmentedStmt in project soot by Sable.

the class IfElseSplitter method getLastStmt.

/*
	 * Given a list of ASTNodes see if the last astnode is a StatementSequenceNode
	 * if not return null
	 * else, return the last statement in this node
	 */
public Stmt getLastStmt(List<Object> body) {
    if (body.size() == 0)
        return null;
    ASTNode lastNode = (ASTNode) body.get(body.size() - 1);
    if (!(lastNode instanceof ASTStatementSequenceNode))
        return null;
    ASTStatementSequenceNode stmtNode = (ASTStatementSequenceNode) lastNode;
    List<AugmentedStmt> stmts = stmtNode.getStatements();
    if (stmts.size() == 0)
        return null;
    AugmentedStmt lastStmt = stmts.get(stmts.size() - 1);
    return lastStmt.get_Stmt();
}
Also used : ASTNode(soot.dava.internal.AST.ASTNode) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Aggregations

AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)27 Stmt (soot.jimple.Stmt)16 ArrayList (java.util.ArrayList)12 List (java.util.List)9 DefinitionStmt (soot.jimple.DefinitionStmt)9 ASTNode (soot.dava.internal.AST.ASTNode)8 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)8 Value (soot.Value)7 Iterator (java.util.Iterator)6 Type (soot.Type)6 GInvokeStmt (soot.grimp.internal.GInvokeStmt)6 DVariableDeclarationStmt (soot.dava.internal.javaRep.DVariableDeclarationStmt)5 GAssignStmt (soot.grimp.internal.GAssignStmt)5 GReturnStmt (soot.grimp.internal.GReturnStmt)5 IntType (soot.IntType)4 ValueBox (soot.ValueBox)4 DecompilationException (soot.dava.DecompilationException)4 DAbruptStmt (soot.dava.internal.javaRep.DAbruptStmt)4 BooleanType (soot.BooleanType)3 ByteType (soot.ByteType)3