1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import java.util.*; 24 import soot.jimple.*; 25 import soot.dava.internal.SET.*; 26 import soot.dava.internal.AST.*; 27 import soot.dava.internal.asg.*; 28 import soot.dava.internal.javaRep.*; 29 import soot.dava.toolkits.base.AST.analysis.*; 30 58 59 public class StrengthenByIf{ 60 64 public static List getNewNode(ASTNode loopNode, ASTIfNode ifNode){ 65 List ifBody = ifNode.getIfBody(); 66 String label = isItOnlyBreak(ifBody); 67 if(label != null){ 68 70 if(((ASTLabeledNode)loopNode).get_Label().toString()!=null){ 72 73 if(((ASTLabeledNode)loopNode).get_Label().toString().compareTo(label)==0){ 74 77 if(loopNode instanceof ASTWhileNode){ 78 ASTCondition outerCond = ((ASTWhileNode)loopNode).get_Condition(); 79 ASTCondition innerCond = ifNode.get_Condition(); 81 innerCond.flip(); 82 ASTCondition newCond = new ASTAndCondition(outerCond,innerCond); 84 List newWhileBody = new ArrayList(); 86 88 SETNodeLabel newLabel = new SETNodeLabel(); 90 91 List toReturn = new ArrayList(); 93 toReturn.add(new ASTWhileNode(newLabel,newCond,newWhileBody)); 94 return toReturn; 95 96 } 97 else if(loopNode instanceof ASTDoWhileNode){ 98 102 return null; 103 } 104 else if (loopNode instanceof ASTUnconditionalLoopNode){ 105 112 113 ASTCondition innerCond = ifNode.get_Condition(); 115 innerCond.flip(); 116 117 List newWhileBody = new ArrayList(); 119 121 SETNodeLabel newLabel = new SETNodeLabel(); 123 124 List toReturn = new ArrayList(); 126 toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody)); 127 return toReturn; 128 } 129 } } 131 } else if(loopNode instanceof ASTUnconditionalLoopNode && ifBody.size()==1){ 133 135 ASTNode tempNode = (ASTNode)ifBody.get(0); 137 if(tempNode instanceof ASTStatementSequenceNode){ 138 List statements = ((ASTStatementSequenceNode)tempNode).getStatements(); 140 Iterator stIt = statements.iterator(); 141 while(stIt.hasNext()){ 142 AugmentedStmt as = (AugmentedStmt)stIt.next(); 143 Stmt stmt = as.get_Stmt(); 144 if(stmt instanceof DAbruptStmt && !(stIt.hasNext())){ 145 DAbruptStmt abStmt = (DAbruptStmt)stmt; 147 if(abStmt.is_Break()){ 148 String loopLabel = ((ASTLabeledNode)loopNode).get_Label().toString(); 150 String breakLabel= abStmt.getLabel().toString(); 151 if(loopLabel != null && breakLabel!=null){ 152 if(loopLabel.compareTo(breakLabel)==0){ 153 154 ASTCondition innerCond = ifNode.get_Condition(); 157 innerCond.flip(); 158 159 List newWhileBody = new ArrayList(); 161 SETNodeLabel newLabel = ((ASTUnconditionalLoopNode)loopNode).get_Label(); 162 163 List toReturn = new ArrayList(); 165 toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody)); 166 167 Iterator tempIt = statements.iterator(); 169 List newStmts = new ArrayList(); 170 while(tempIt.hasNext()){ 171 Object tempStmt = tempIt.next(); 172 if(tempIt.hasNext()){ 173 newStmts.add(tempStmt); 174 } 175 } 176 toReturn.add(new ASTStatementSequenceNode(newStmts)); 177 return toReturn; 178 } } } } 182 else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt){ 183 if(!(stIt.hasNext())){ 184 ASTCondition innerCond = ifNode.get_Condition(); 187 innerCond.flip(); 188 189 List newWhileBody = new ArrayList(); 191 193 194 SETNodeLabel newLabel = new SETNodeLabel(); 196 197 List toReturn = new ArrayList(); 199 toReturn.add(new ASTWhileNode(newLabel,innerCond,newWhileBody)); 200 201 Iterator tempIt = statements.iterator(); 203 List newStmts = new ArrayList(); 204 while(tempIt.hasNext()){ 205 newStmts.add(tempIt.next()); 206 } 207 toReturn.add(new ASTStatementSequenceNode(newStmts)); 208 return toReturn; 209 } 210 } 211 } } } return null; 215 } 217 218 219 220 230 private static String isItOnlyBreak(List body){ 231 if(body.size()!=1){ 232 return null; 234 } 235 ASTNode tempNode = (ASTNode)body.get(0); 236 if(!(tempNode instanceof ASTStatementSequenceNode)){ 237 return null; 239 } 240 241 List statements = ((ASTStatementSequenceNode)tempNode).getStatements(); 242 if(statements.size()!=1){ 243 return null; 245 } 246 AugmentedStmt as = (AugmentedStmt)statements.get(0); 247 Stmt stmt = as.get_Stmt(); 248 if(!(stmt instanceof DAbruptStmt)){ 249 return null; 251 } 252 DAbruptStmt abStmt = (DAbruptStmt)stmt; 253 if(!(abStmt.is_Break())){ 254 return null; 256 } 257 return abStmt.getLabel().toString(); 258 } 259 } 260 261 262 | Popular Tags |