1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import java.util.*; 24 import soot.dava.internal.SET.*; 25 import soot.dava.internal.AST.*; 26 import soot.dava.toolkits.base.AST.analysis.*; 27 28 29 44 45 public class LoopStrengthener extends DepthFirstAdapter{ 46 47 public LoopStrengthener(){ 48 } 49 50 public LoopStrengthener(boolean verbose){ 51 super(verbose); 52 } 53 54 55 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 56 } 57 58 71 public void normalRetrieving(ASTNode node){ 72 if(node instanceof ASTSwitchNode){ 73 dealWithSwitchNode((ASTSwitchNode)node); 74 return; 75 } 76 77 Iterator sbit = node.get_SubBodies().iterator(); 79 80 int subBodyNumber=0; 82 while (sbit.hasNext()) { 83 Object subBody = sbit.next(); 84 Iterator it = ((List) subBody).iterator(); 85 86 int nodeNumber=0; 87 while (it.hasNext()){ 89 ASTNode temp = (ASTNode) it.next(); 90 91 if(temp instanceof ASTWhileNode || temp instanceof ASTUnconditionalLoopNode || 92 temp instanceof ASTDoWhileNode){ 93 94 ASTNode oneNode=getOnlySubNode(temp); 95 if(oneNode !=null){ 96 List newNode=null; 97 if(oneNode instanceof ASTIfNode){ 98 newNode=StrengthenByIf.getNewNode(temp,(ASTIfNode)oneNode); 99 } 100 else if (oneNode instanceof ASTIfElseNode){ 101 newNode=StrengthenByIfElse.getNewNode(temp,(ASTIfElseNode)oneNode); 102 } 103 104 if(newNode!=null){ 105 replaceNode(node,subBodyNumber,nodeNumber,temp,newNode); 108 UselessLabelFinder.v().findAndKill(node); 109 } 110 } 111 } 112 113 temp.apply(this); 114 nodeNumber++; 115 } 116 subBodyNumber++; 117 } } 119 120 public void caseASTTryNode(ASTTryNode node){ 121 inASTTryNode(node); 122 123 List tryBody = node.get_TryBody(); 125 Iterator it = tryBody.iterator(); 126 127 int nodeNumber=0; 128 while (it.hasNext()){ 130 ASTNode temp = (ASTNode) it.next(); 131 if(temp instanceof ASTWhileNode || temp instanceof ASTUnconditionalLoopNode || 132 temp instanceof ASTDoWhileNode){ 133 134 ASTNode oneNode=getOnlySubNode(temp); 135 if(oneNode !=null){ 136 List newNode=null; 137 if(oneNode instanceof ASTIfNode){ 138 newNode=StrengthenByIf.getNewNode(temp,(ASTIfNode)oneNode); 139 } 140 else if (oneNode instanceof ASTIfElseNode){ 141 newNode=StrengthenByIfElse.getNewNode(temp,(ASTIfElseNode)oneNode); 142 } 143 144 if(newNode!=null){ 145 148 List newBody=createNewSubBody(tryBody,nodeNumber,temp,newNode); 149 if(newBody!=null){ 150 node.replaceTryBody(newBody); 152 G.v().ASTTransformations_modified = true; 153 } 155 UselessLabelFinder.v().findAndKill(node); 156 } 157 } 158 } temp.apply(this); 160 nodeNumber++; 161 } 162 163 164 165 166 Map exceptionMap = node.get_ExceptionMap(); 167 Map paramMap = node.get_ParamMap(); 168 List catchList = node.get_CatchList(); 173 Iterator itBody=null; 174 it = catchList.iterator(); 175 while (it.hasNext()) { 176 ASTTryNode.container catchBody = (ASTTryNode.container)it.next(); 177 178 SootClass sootClass = ((SootClass)exceptionMap.get(catchBody)); 179 Type type = sootClass.getType(); 180 181 caseType(type); 183 184 Local local = (Local)paramMap.get(catchBody); 186 decideCaseExprOrRef(local); 187 188 List body = (List)catchBody.o; 190 itBody = body.iterator(); 191 192 nodeNumber=0; 193 while (itBody.hasNext()){ 195 ASTNode temp = (ASTNode) itBody.next(); 196 if(temp instanceof ASTWhileNode || temp instanceof ASTUnconditionalLoopNode || 197 temp instanceof ASTDoWhileNode){ 198 199 ASTNode oneNode=getOnlySubNode(temp); 200 if(oneNode !=null){ 201 List newNode=null; 202 if(oneNode instanceof ASTIfNode){ 203 newNode=StrengthenByIf.getNewNode(temp,(ASTIfNode)oneNode); 204 } 205 else if (oneNode instanceof ASTIfElseNode){ 206 newNode=StrengthenByIfElse.getNewNode(temp,(ASTIfElseNode)oneNode); 207 } 208 209 if(newNode!=null){ 210 213 List newBody=createNewSubBody(body,nodeNumber,temp,newNode); 214 if(newBody!=null){ 215 catchBody.replaceBody(newBody); 217 G.v().ASTTransformations_modified = true; 218 } 220 UselessLabelFinder.v().findAndKill(node); 221 } 222 } 223 } temp.apply(this); 225 nodeNumber++; 226 } 227 } 228 229 outASTTryNode(node); 230 } 231 232 233 private void dealWithSwitchNode(ASTSwitchNode node){ 234 236 List indexList = node.getIndexList(); 237 Map index2BodyList = node.getIndex2BodyList(); 238 239 Iterator it = indexList.iterator(); 240 while (it.hasNext()) { Object currentIndex = it.next(); 242 List body = (List) index2BodyList.get( currentIndex); 243 244 if (body != null){ 245 247 Iterator itBody = body.iterator(); 248 int nodeNumber=0; 249 while (itBody.hasNext()){ 251 ASTNode temp = (ASTNode) itBody.next(); 252 if(temp instanceof ASTWhileNode || temp instanceof ASTUnconditionalLoopNode || 253 temp instanceof ASTDoWhileNode){ 254 255 ASTNode oneNode=getOnlySubNode(temp); 256 if(oneNode !=null){ 257 List newNode=null; 258 if(oneNode instanceof ASTIfNode){ 259 newNode=StrengthenByIf.getNewNode(temp,(ASTIfNode)oneNode); 260 } 261 else if (oneNode instanceof ASTIfElseNode){ 262 newNode=StrengthenByIfElse.getNewNode(temp,(ASTIfElseNode)oneNode); 263 } 264 265 if(newNode!=null){ 266 269 List newBody=createNewSubBody(body,nodeNumber,temp,newNode); 270 if(newBody!=null){ 271 index2BodyList.put(currentIndex,newBody); 273 node.replaceIndex2BodyList(index2BodyList); 275 G.v().ASTTransformations_modified = true; 276 } 278 UselessLabelFinder.v().findAndKill(node); 279 } 280 } 281 } temp.apply(this); 283 nodeNumber++; 284 } 285 } 286 } 287 } 288 289 290 291 292 293 294 302 private ASTNode getOnlySubNode(ASTNode node){ 303 if(!(node instanceof ASTWhileNode || node instanceof ASTDoWhileNode || 304 node instanceof ASTUnconditionalLoopNode)){ 305 return null; 307 } 308 List subBodies = node.get_SubBodies(); 309 if(subBodies.size()!=1){ 310 return null; 312 } 313 List subBody = (List)subBodies.get(0); 314 if(subBody.size()!=1){ 315 return null; 317 } 318 return (ASTNode)subBody.get(0); 319 } 320 321 322 323 324 325 326 327 328 329 330 331 332 333 343 private void replaceNode(ASTNode node,int subBodyNumber,int nodeNumber,ASTNode loopNode,List newNode){ 344 if(!(node instanceof ASTIfElseNode)){ 345 List subBodies = node.get_SubBodies(); 347 if(subBodies.size()!=1){ 348 throw new RuntimeException ("Please report this benchmark to the programmer"); 350 } 351 List onlySubBody = (List)subBodies.get(0); 352 353 357 List newBody = createNewSubBody(onlySubBody,nodeNumber,loopNode,newNode); 358 if(newBody==null){ 359 return; 361 } 362 if(node instanceof ASTMethodNode){ 363 ((ASTMethodNode)node).replaceBody(newBody); 364 G.v().ASTTransformations_modified = true; 365 } 367 else if(node instanceof ASTSynchronizedBlockNode){ 368 ((ASTSynchronizedBlockNode)node).replaceBody(newBody); 369 G.v().ASTTransformations_modified = true; 370 } 372 else if(node instanceof ASTLabeledBlockNode){ 373 ((ASTLabeledBlockNode)node).replaceBody(newBody); 374 G.v().ASTTransformations_modified = true; 375 } 377 else if(node instanceof ASTUnconditionalLoopNode){ 378 ((ASTUnconditionalLoopNode)node).replaceBody(newBody); 379 G.v().ASTTransformations_modified = true; 380 } 382 else if(node instanceof ASTIfNode){ 383 ((ASTIfNode)node).replaceBody(newBody); 384 G.v().ASTTransformations_modified = true; 385 } 387 else if(node instanceof ASTWhileNode){ 388 ((ASTWhileNode)node).replaceBody(newBody); 389 G.v().ASTTransformations_modified = true; 390 } 392 else if(node instanceof ASTDoWhileNode){ 393 ((ASTDoWhileNode)node).replaceBody(newBody); 394 G.v().ASTTransformations_modified = true; 395 } 397 else { 398 return; 400 } 401 } 402 else{ if(subBodyNumber!=0 && subBodyNumber!=1){ 405 return; 408 } 409 List subBodies = node.get_SubBodies(); 410 if(subBodies.size()!=2){ 411 throw new RuntimeException ("Please report this benchmark to the programmer"); 413 } 414 415 List toModifySubBody = (List)subBodies.get(subBodyNumber); 416 417 421 List newBody = createNewSubBody(toModifySubBody,nodeNumber,loopNode,newNode); 422 if(newBody==null){ 423 return; 425 } 426 if(subBodyNumber==0){ 427 G.v().ASTTransformations_modified = true; 430 ((ASTIfElseNode)node).replaceBody(newBody,(List)subBodies.get(1)); 431 } 432 else if(subBodyNumber==1){ 433 G.v().ASTTransformations_modified = true; 436 ((ASTIfElseNode)node).replaceBody((List)subBodies.get(0),newBody); 437 } 438 else{ return; 442 } 443 444 } } 446 447 448 449 450 451 452 453 public static List createNewSubBody(List oldSubBody,int nodeNumber,ASTNode oldNode,List newNode){ 454 List newSubBody = new ArrayList(); 456 457 Iterator it = oldSubBody.iterator(); 459 460 int index=0; 462 while(index!=nodeNumber ){ 463 if(!it.hasNext()){ 464 return null; 465 } 466 newSubBody.add(it.next()); 467 index++; 468 } 469 470 ASTNode toRemove = (ASTNode)it.next(); 473 if(toRemove.toString().compareTo(oldNode.toString())!=0){ 474 System.out.println("The replace nodes dont match please report benchmark to developer"); 475 return null; 476 } 477 else{ 478 newSubBody.addAll(newNode); 480 } 481 482 while(it.hasNext()){ 484 newSubBody.add(it.next()); 485 } 486 487 return newSubBody; 489 } 490 491 } | Popular Tags |