Search in sources :

Example 1 with DArrayInitValueBox

use of soot.dava.internal.javaRep.DArrayInitValueBox in project soot by Sable.

the class ShortcutArrayInit method secondPattern.

/*
	 * Maybe its a multi-D array then we need to look for a DAssignStmt followed
	 * by a definition Stmt
	 */
public void secondPattern(ASTStatementSequenceNode node) {
    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;
        if (DEBUG) {
            System.out.println("Found a new ArrayExpr" + rightValue);
            System.out.println("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("Found value to be 0 doing nothing");
            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 = (AugmentedStmt) 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++) {
            // a DefinitionStmt
            if (!tempIt.hasNext()) {
                // since its end of the stmt seq node just return
                if (DEBUG)
                    System.out.println("returning");
                return;
            }
            AugmentedStmt augOne = tempIt.next();
            Stmt augSOne = augOne.get_Stmt();
            if (!tempIt.hasNext()) {
                // since its end of the stmt seq node just return
                if (DEBUG)
                    System.out.println("returning");
                return;
            }
            AugmentedStmt augTwo = tempIt.next();
            Stmt augSTwo = augTwo.get_Stmt();
            if (!isInSequenceAssignmentPatternTwo(augSOne, augSTwo, 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 RHS of augSOne is the next assignment in the sequence
                // add to ValueBox array
                array[i] = ((DShortcutAssignStmt) augSOne).getRightOpBox();
                toRemove.add(augOne);
                toRemove.add(augTwo);
            }
        }
        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;
    }
}
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 2 with DArrayInitValueBox

use of soot.dava.internal.javaRep.DArrayInitValueBox 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)

Aggregations

ArrayList (java.util.ArrayList)2 Type (soot.Type)2 Value (soot.Value)2 ValueBox (soot.ValueBox)2 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)2 DArrayInitExpr (soot.dava.internal.javaRep.DArrayInitExpr)2 DArrayInitValueBox (soot.dava.internal.javaRep.DArrayInitValueBox)2 DAssignStmt (soot.dava.internal.javaRep.DAssignStmt)2 DShortcutAssignStmt (soot.dava.internal.javaRep.DShortcutAssignStmt)2 InitializationDeclarationShortcut (soot.dava.toolkits.base.AST.traversals.InitializationDeclarationShortcut)2 DefinitionStmt (soot.jimple.DefinitionStmt)2 IntConstant (soot.jimple.IntConstant)2 NewArrayExpr (soot.jimple.NewArrayExpr)2 Stmt (soot.jimple.Stmt)2