KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > transformations > DecrementIncrementStmtCreation


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.toolkits.base.AST.transformations;
21
22 import java.util.*;
23 import soot.*;
24 import soot.jimple.*;
25 import soot.dava.*;
26 import soot.dava.internal.asg.*;
27 import soot.dava.internal.javaRep.*;
28 import soot.dava.internal.AST.*;
29 import soot.dava.toolkits.base.AST.analysis.*;
30
31 /*
32  * CHANGELOG: Nomair 9th Feb (For some reason only AddExpr was being checked for i++
33  * Added SubExpr for i--
34  */

35 public class DecrementIncrementStmtCreation extends DepthFirstAdapter {
36
37     public DecrementIncrementStmtCreation() {
38     }
39
40     public DecrementIncrementStmtCreation(boolean verbose) {
41         super(verbose);
42     }
43
44     public void caseASTStatementSequenceNode(ASTStatementSequenceNode node) {
45         List statements = node.getStatements();
46         Iterator stmtIt = statements.iterator();
47                 
48         while (stmtIt.hasNext()) {
49             Object JavaDoc temp = stmtIt.next();
50             //System.out.println(temp);
51
AugmentedStmt as = (AugmentedStmt)temp;
52             Stmt s = as.get_Stmt();
53             if (!( s instanceof DefinitionStmt))
54                 continue;
55             
56             // check if its i= i+1
57
Value left = ((DefinitionStmt) s).getLeftOp();
58             Value right = ((DefinitionStmt) s).getRightOp();
59
60
61             if (right instanceof SubExpr) {
62                 Value op1 = ((SubExpr) right).getOp1();
63                 Value op2 = ((SubExpr) right).getOp2();
64                 if (left.toString().compareTo(op1.toString()) != 0) {
65                     //not the same
66
continue;
67                 }
68                 //if they are the same
69

70                 // check if op2 is a constant with value 1 or -1
71
if (op2 instanceof IntConstant) {
72                     if (((IntConstant) op2).value == 1) {
73                         // this is i = i-1
74
DDecrementStmt newStmt = new DDecrementStmt(left, right);
75                         as.set_Stmt(newStmt);
76                     } else if (((IntConstant) op2).value == -1) {
77                         // this is i = i+1
78
DIncrementStmt newStmt = new DIncrementStmt(left, right);
79                         as.set_Stmt(newStmt);
80                     }
81                 }
82                 
83             }
84             else if (right instanceof AddExpr) {
85                 Value op1 = ((AddExpr) right).getOp1();
86                 Value op2 = ((AddExpr) right).getOp2();
87                 if (left.toString().compareTo(op1.toString()) != 0) {
88                     continue;
89                 }
90                 // check if op2 is a constant with value 1 or -1
91
if (op2 instanceof IntConstant) {
92                     if (((IntConstant) op2).value == 1) {
93                         // this is i = i+1
94
DIncrementStmt newStmt = new DIncrementStmt(left, right);
95                         as.set_Stmt(newStmt);
96                     } else if (((IntConstant) op2).value == -1) {
97                         // this is i = i-1
98
DDecrementStmt newStmt = new DDecrementStmt(left, right);
99                         as.set_Stmt(newStmt);
100                     }
101                 }
102             }//right expr was addExpr
103
}//going through statements
104
}
105 }
106
107
Popular Tags