1 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.SET.*; 27 import soot.dava.internal.asg.*; 28 import soot.dava.internal.AST.*; 29 import soot.util.*; 30 31 public class ForLoopCreationHelper{ 32 33 ASTStatementSequenceNode stmtSeqNode; 34 ASTWhileNode whileNode; 35 36 ASTStatementSequenceNode newStmtSeqNode; 37 ASTForLoopNode forNode; 38 39 Map varToStmtMap; 40 41 51 List myStmts; boolean removeLast=false; 54 55 56 public ForLoopCreationHelper(ASTStatementSequenceNode stmtSeqNode, ASTWhileNode whileNode){ 57 this.stmtSeqNode=stmtSeqNode; 58 this.whileNode=whileNode; 59 varToStmtMap = new HashMap(); 60 } 61 62 63 72 public List createNewBody(List oldSubBody, int nodeNumber){ 73 List newSubBody = new ArrayList(); 74 75 if(oldSubBody.size()<= nodeNumber){ 76 return null; 78 } 79 80 Iterator oldIt = oldSubBody.iterator(); 81 int index=0; 82 while(index!=nodeNumber){ 83 newSubBody.add(oldIt.next()); 84 index++; 85 } 86 87 ASTNode temp = (ASTNode)oldIt.next(); 89 if(!(temp instanceof ASTStatementSequenceNode)) 90 return null; 91 temp = (ASTNode)oldIt.next(); 92 if(!(temp instanceof ASTWhileNode)) 93 return null; 94 95 if(newStmtSeqNode!=null){ 97 newSubBody.add(newStmtSeqNode); 98 } 99 else{ 100 } 102 103 newSubBody.add(forNode); 105 106 107 while(oldIt.hasNext()){ 109 newSubBody.add(oldIt.next()); 110 } 111 112 return newSubBody; 113 } 114 115 116 117 127 private List getDefs(){ 128 if(stmtSeqNode==null){ 129 return null; 130 } 131 132 List toReturn= new ArrayList(); 133 134 List statements = stmtSeqNode.getStatements(); 135 Iterator stmtIt = statements.iterator(); 136 int stmtNum=0; 137 while(stmtIt.hasNext()){ 138 AugmentedStmt as = (AugmentedStmt)stmtIt.next(); 139 Stmt s = as.get_Stmt(); 140 141 if(s instanceof DefinitionStmt){ 143 Value left = ((DefinitionStmt)s).getLeftOp(); 144 toReturn.add(left.toString()); 145 varToStmtMap.put(left.toString(),new Integer (stmtNum)); 146 } 147 else{ 148 toReturn = new ArrayList(); 149 varToStmtMap = new HashMap(); 150 } 151 stmtNum++; 152 } return toReturn; 154 } 155 156 161 private List getCondUses(){ 162 if(whileNode == null){ 163 return null; 164 } 165 ASTCondition cond = whileNode.get_Condition(); 166 167 return getCond(cond); 168 } 169 170 private List getCond(ASTCondition cond){ 171 List toReturn=new ArrayList(); 172 173 if(cond instanceof ASTUnaryCondition){ 174 toReturn.add(((ASTUnaryCondition)cond).toString()); 175 } 176 else if (cond instanceof ASTBinaryCondition){ 177 ConditionExpr condExpr = ((ASTBinaryCondition)cond).getConditionExpr(); 178 toReturn.add(condExpr.getOp1().toString()); 179 toReturn.add(condExpr.getOp2().toString()); 180 } 181 else if (cond instanceof ASTAggregatedCondition){ 182 toReturn.addAll(getCond(((ASTAggregatedCondition)cond).getLeftOp())); 183 toReturn.addAll(getCond(((ASTAggregatedCondition)cond).getRightOp())); 184 } 185 return toReturn; 186 187 } 188 189 190 191 private List getCommonVars(List defs, List condUses){ 192 193 List toReturn = new ArrayList(); 194 Iterator defIt = defs.iterator(); 195 196 while(defIt.hasNext()){ 197 String defString = (String )defIt.next(); 198 Iterator condIt = condUses.iterator(); 199 while(condIt.hasNext()){ 200 String condString = (String )condIt.next(); 201 202 if(condString.compareTo(defString)==0){ 203 toReturn.add(defString); 205 break; 206 } 207 } 208 } 209 210 return toReturn; 211 } 212 213 214 223 public boolean checkPattern(){ 224 List defs = getDefs(); 225 if(defs==null){ 226 return false; 227 } 228 if(defs.size()==0){ 229 return false; 230 } 231 232 List condUses = getCondUses(); 233 if(condUses==null){ 234 return false; 235 } 236 if(condUses.size()==0){ 237 return false; 238 } 239 240 243 List commonVars = getCommonVars(defs,condUses); 244 245 250 251 List update = getUpdate(defs,condUses,commonVars); 252 if(update==null || update.size() == 0){ 253 return false; 255 } 256 257 if(commonVars==null || commonVars.size()==0){ 258 return false; 260 } 261 262 267 268 269 List init= createNewStmtSeqNodeAndGetInit(commonVars); 271 if(init.size()==0){ 272 return false; 274 } 275 276 277 278 ASTCondition condition = whileNode.get_Condition(); 279 List body = (List)whileNode.get_SubBodies().get(0); 280 SETNodeLabel label = ((ASTLabeledNode)whileNode).get_Label(); 281 282 283 284 289 291 if(removeLast){ 293 myStmts.remove(myStmts.size()-1); 295 removeLast=false; 296 } 297 298 forNode = new ASTForLoopNode(label,init,condition,update,body); 299 return true; 300 } 301 302 303 304 305 private List getUpdate(List defs,List condUses, List commonUses){ 306 List toReturn = new ArrayList(); 307 308 List subBodies = whileNode.get_SubBodies(); 310 if(subBodies.size()!=1){ 311 return toReturn; 313 } 314 315 List subBody = (List)subBodies.get(0); 316 Iterator it = subBody.iterator(); 317 while(it.hasNext()){ 318 ASTNode temp = (ASTNode)it.next(); 319 320 if(it.hasNext()){ 321 continue; 323 } 324 325 327 if(!(temp instanceof ASTStatementSequenceNode)){ 328 return null; 331 } 332 333 List stmts = ((ASTStatementSequenceNode)temp).getStatements(); 334 AugmentedStmt last = (AugmentedStmt)stmts.get(stmts.size()-1); 335 Stmt lastStmt = last.get_Stmt(); 336 337 if(!(lastStmt instanceof DefinitionStmt)){ 338 return null; 341 } 342 343 344 345 346 Value left = ((DefinitionStmt)lastStmt).getLeftOp(); 348 Iterator defIt = defs.iterator(); 349 while(defIt.hasNext()){ 350 String defString = (String )defIt.next(); 351 if(left.toString().compareTo(defString)==0){ 352 toReturn.add(last); 354 355 myStmts=stmts; 356 removeLast=true; 357 359 Iterator coIt = commonUses.iterator(); 361 boolean matched=false; 362 while(coIt.hasNext()){ 363 if (defString.compareTo((String )coIt.next())==0){ 364 matched=true; 365 } 366 } 367 if(!matched){ 368 commonUses.add(defString); 370 } 371 372 return toReturn; 373 } 374 } 375 376 Iterator condIt = condUses.iterator(); 378 while(condIt.hasNext()){ 379 String condString = (String )condIt.next(); 380 if(left.toString().compareTo(condString)==0){ 381 toReturn.add(last); 383 384 myStmts=stmts; 385 removeLast=true; 386 388 Iterator coIt = commonUses.iterator(); 390 boolean matched=false; 391 while(coIt.hasNext()){ 392 if (condString.compareTo((String )coIt.next())==0){ 393 matched=true; 394 } 395 } 396 if(!matched){ 397 commonUses.add(condString); 399 } 400 return toReturn; 401 } 402 } 403 } 405 return toReturn; 406 } 407 408 409 private List createNewStmtSeqNodeAndGetInit(List commonVars){ 410 int currentLowestPosition=999; 412 Iterator it = commonVars.iterator(); 413 while(it.hasNext()){ 414 String temp = (String )it.next(); 415 Integer tempInt = (Integer )varToStmtMap.get(temp); 416 if(tempInt !=null){ 417 if(tempInt.intValue()<currentLowestPosition){ 418 currentLowestPosition=tempInt.intValue(); 419 } 420 } 421 } 422 423 List stmts = new ArrayList(); 424 425 426 List statements = stmtSeqNode.getStatements(); 427 Iterator stmtIt = statements.iterator(); 428 int stmtNum=0; 429 430 while(stmtNum<currentLowestPosition && stmtIt.hasNext()){ 431 stmts.add(stmtIt.next()); 432 stmtNum++; 433 } 434 435 if(stmts.size()>0){ 436 newStmtSeqNode = new ASTStatementSequenceNode(stmts); 437 } 438 else{ 439 newStmtSeqNode = null; 440 } 441 442 443 List init = new ArrayList(); 444 while(stmtIt.hasNext()){ 445 init.add(stmtIt.next()); 446 } 447 448 return init; 449 } 450 451 452 453 471 } | Popular Tags |