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 31 32 61 62 public class OrAggregatorFour extends DepthFirstAdapter{ 63 64 public OrAggregatorFour(){ 65 } 66 public OrAggregatorFour(boolean verbose){ 67 super(verbose); 68 } 69 70 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 71 } 72 73 74 75 76 public void outASTForLoopNode(ASTForLoopNode node){ 77 String label = node.get_Label().toString(); 78 if(label==null) 79 return; 80 81 List subBodies=node.get_SubBodies(); 82 List newBody=matchPattern(label,subBodies); 83 if(newBody!=null){ 84 node.replaceBody(newBody); 85 G.v().ASTTransformations_modified = true; 87 } 88 89 92 UselessLabelFinder.v().findAndKill(node); 93 } 94 95 96 97 public void outASTWhileNode(ASTWhileNode node){ 98 String label = node.get_Label().toString(); 99 if(label==null) 100 return; 101 102 List subBodies=node.get_SubBodies(); 103 List newBody=matchPattern(label,subBodies); 104 if(newBody!=null){ 105 node.replaceBody(newBody); 106 G.v().ASTTransformations_modified = true; 108 } 109 110 113 UselessLabelFinder.v().findAndKill(node); 114 } 115 116 public void outASTDoWhileNode(ASTDoWhileNode node){ 117 String label = node.get_Label().toString(); 118 if(label==null) 119 return; 120 121 List subBodies=node.get_SubBodies(); 122 List newBody=matchPattern(label,subBodies); 123 if(newBody!=null){ 124 node.replaceBody(newBody); 125 G.v().ASTTransformations_modified = true; 127 } 128 131 UselessLabelFinder.v().findAndKill(node); 132 } 133 134 public void outASTUnconditionalLoopNode(ASTUnconditionalLoopNode node){ 135 String label = node.get_Label().toString(); 136 if(label==null) 137 return; 138 139 List subBodies=node.get_SubBodies(); 140 List newBody=matchPattern(label,subBodies); 141 if(newBody!=null){ 142 node.replaceBody(newBody); 143 G.v().ASTTransformations_modified = true; 145 } 146 149 UselessLabelFinder.v().findAndKill(node); 150 } 151 152 public List matchPattern(String whileLabel,List subBodies){ 153 if(subBodies.size()!=1){ 156 return null; 158 } 159 160 List subBody = (List)subBodies.get(0); 161 Iterator it = subBody.iterator(); 162 int nodeNumber=0; 163 while(it.hasNext()){ ASTNode temp = (ASTNode)it.next(); 166 if(temp instanceof ASTLabeledBlockNode){ 167 ASTLabeledBlockNode labeledNode = (ASTLabeledBlockNode)temp; 169 String innerLabel=labeledNode.get_Label().toString(); 170 if(innerLabel==null){ nodeNumber++; 172 continue; 173 } 174 175 List labeledBlocksSubBodies = labeledNode.get_SubBodies(); 177 if(labeledBlocksSubBodies.size()!=1){ 178 nodeNumber++; 180 continue; 181 } 182 183 List labeledBlocksSubBody = (List)labeledBlocksSubBodies.get(0); 185 186 boolean allIfs = checkAllAreIfsWithProperBreaks(labeledBlocksSubBody.iterator(),whileLabel,innerLabel); 187 if(!allIfs){ 188 nodeNumber++; 190 continue; 191 } 192 193 195 List whileBody = createWhileBody(subBody,labeledBlocksSubBody,nodeNumber); 197 if(whileBody!=null){ 198 return whileBody; 199 } 200 } nodeNumber++; 202 } return null; 204 } 205 206 private List createWhileBody(List subBody,List labeledBlocksSubBody,int nodeNumber){ 207 List bodyA = new ArrayList(); 209 210 Iterator it = subBody.iterator(); 212 213 int index=0; 215 while(index!=nodeNumber ){ 216 if(!it.hasNext()){ 217 return null; 218 } 219 bodyA.add(it.next()); 220 index++; 221 } 222 223 224 List conditions = getConditions(labeledBlocksSubBody.iterator()); 228 229 Iterator condIt = conditions.iterator(); 231 ASTCondition newCond=null;; 232 while(condIt.hasNext()){ 233 ASTCondition next = (ASTCondition)condIt.next(); 234 if(newCond==null) 235 newCond=next; 236 else 237 newCond=new ASTOrCondition(newCond,next); 238 } 239 240 241 it.next(); List bodyB = new ArrayList(); 244 while(it.hasNext()){ 245 bodyB.add(it.next()); 246 } 247 248 ASTIfNode newNode = new ASTIfNode(new SETNodeLabel(),newCond,bodyB); 249 250 251 bodyA.add(newNode); 253 return bodyA; 254 } 255 256 262 private List getConditions(Iterator it){ 263 List toReturn = new ArrayList(); 264 while(it.hasNext()){ 265 ASTIfNode node = (ASTIfNode)it.next(); 267 268 ASTCondition cond = node.get_Condition(); 269 if(it.hasNext()){ 271 toReturn.add(cond); 273 } 274 else{ 275 cond.flip(); 278 toReturn.add(cond); 280 } 281 } return toReturn; 283 } 284 285 286 287 288 private boolean checkAllAreIfsWithProperBreaks(Iterator it,String outerLabel, String innerLabel){ 289 while(it.hasNext()){ 291 ASTNode secondLabelsBody = (ASTNode)it.next(); 292 293 295 Stmt stmt = isIfNodeWithOneStatement(secondLabelsBody); 296 if(stmt == null){ 297 return false; 299 } 300 301 boolean abrupt = abruptLabel(stmt,outerLabel,innerLabel,it.hasNext()); 303 304 if(!abrupt){ 305 return false; 307 } 308 } 310 return true; 312 } 313 314 315 316 317 318 319 320 321 326 private boolean abruptLabel(Stmt stmt, String outerLabel, String innerLabel, boolean hasNext){ 327 if(!(stmt instanceof DAbruptStmt)){ 328 return false; 330 } 331 DAbruptStmt abStmt = (DAbruptStmt)stmt; 332 SETNodeLabel label = abStmt.getLabel(); 333 String abruptLabel=label.toString(); 334 335 336 if(abruptLabel==null) 337 return false; 338 339 if(abStmt.is_Break() && abruptLabel.compareTo(innerLabel)==0 && hasNext){ 340 return true; 341 } 342 else if (abStmt.is_Continue() && abruptLabel.compareTo(outerLabel)==0 && !hasNext){ 343 return true; 344 } 345 else 346 return false; 347 } 348 349 350 351 352 353 354 355 356 357 358 359 360 private Stmt isIfNodeWithOneStatement(ASTNode secondLabelsBody){ 361 if(!(secondLabelsBody instanceof ASTIfNode)){ 362 return null; 364 } 365 367 ASTIfNode ifNode =(ASTIfNode)secondLabelsBody; 368 List ifSubBodies =ifNode.get_SubBodies(); 369 if(ifSubBodies.size()!=1){ 370 return null; 372 } 373 374 List ifBody = (List)ifSubBodies.get(0); 376 377 if(ifBody.size()!=1){ 379 return null; 381 } 382 383 ASTNode ifBodysBody = (ASTNode)ifBody.get(0); 385 if(!(ifBodysBody instanceof ASTStatementSequenceNode)){ 386 return null; 388 } 389 390 List statements = ((ASTStatementSequenceNode)ifBodysBody).getStatements(); 392 if(statements.size()!=1){ 393 return null; 395 } 396 397 AugmentedStmt as = (AugmentedStmt)statements.get(0); 399 Stmt s = as.get_Stmt(); 400 return s; 401 } 402 403 } | Popular Tags |