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.AST.*; 26 import soot.dava.internal.asg.*; 27 import soot.dava.internal.javaRep.*; 28 29 53 public class OrAggregatorThree { 54 55 56 public static void checkAndTransform(ASTNode node,ASTIfNode ifOne,ASTIfNode ifTwo, int nodeNumber,int subBodyNumber){ 57 58 if(!(node instanceof ASTIfElseNode)){ 59 List subBodies = node.get_SubBodies(); 61 if(subBodies.size()!=1){ 62 throw new RuntimeException ("Please report this benchmark to the programmer"); 64 } 65 List onlySubBody = (List)subBodies.get(0); 66 67 71 72 List newBody = createNewNodeBody(onlySubBody,nodeNumber,ifOne,ifTwo); 74 75 76 if(newBody==null){ 77 return; 79 } 80 if(node instanceof ASTMethodNode){ 81 ((ASTMethodNode)node).replaceBody(newBody); 82 G.v().ASTTransformations_modified = true; 83 } 85 else if(node instanceof ASTSynchronizedBlockNode){ 86 ((ASTSynchronizedBlockNode)node).replaceBody(newBody); 87 G.v().ASTTransformations_modified = true; 88 } 90 else if(node instanceof ASTLabeledBlockNode){ 91 ((ASTLabeledBlockNode)node).replaceBody(newBody); 92 G.v().ASTTransformations_modified = true; 93 } 95 else if(node instanceof ASTUnconditionalLoopNode){ 96 ((ASTUnconditionalLoopNode)node).replaceBody(newBody); 97 G.v().ASTTransformations_modified = true; 98 } 100 else if(node instanceof ASTIfNode){ 101 ((ASTIfNode)node).replaceBody(newBody); 102 G.v().ASTTransformations_modified = true; 103 } 105 else if(node instanceof ASTWhileNode){ 106 ((ASTWhileNode)node).replaceBody(newBody); 107 G.v().ASTTransformations_modified = true; 108 } 110 else if(node instanceof ASTDoWhileNode){ 111 ((ASTDoWhileNode)node).replaceBody(newBody); 112 G.v().ASTTransformations_modified = true; 113 } 115 else { 116 return; 118 } 119 } 120 else{ if(subBodyNumber!=0 && subBodyNumber!=1){ 123 return; 126 } 127 List subBodies = node.get_SubBodies(); 128 if(subBodies.size()!=2){ 129 throw new RuntimeException ("Please report this benchmark to the programmer"); 131 } 132 133 List toModifySubBody = (List)subBodies.get(subBodyNumber); 134 135 139 List newBody = createNewNodeBody(toModifySubBody,nodeNumber,ifOne,ifTwo); 140 if(newBody==null){ 141 return; 143 } 144 if(subBodyNumber==0){ 145 G.v().ASTTransformations_modified = true; 148 ((ASTIfElseNode)node).replaceBody(newBody,(List)subBodies.get(1)); 149 } 150 else if(subBodyNumber==1){ 151 G.v().ASTTransformations_modified = true; 154 ((ASTIfElseNode)node).replaceBody((List)subBodies.get(0),newBody); 155 } 156 else{ return; 160 } 161 162 } } 164 165 166 174 175 public static List createNewNodeBody(List oldSubBody,int nodeNumber,ASTIfNode ifOne ,ASTIfNode ifTwo){ 176 if(!matchPattern(ifOne,ifTwo)){ 177 return null; 179 } 180 181 List newSubBody = new ArrayList(); 183 184 Iterator it = oldSubBody.iterator(); 186 187 int index=0; 189 while(index!=nodeNumber ){ 190 if(!it.hasNext()){ 191 return null; 192 } 193 newSubBody.add(it.next()); 194 index++; 195 } 196 197 ASTNode isItIfOne = (ASTNode)it.next(); 200 201 if(!(isItIfOne instanceof ASTIfNode)){ 202 return null; 204 } 205 206 ASTNode isItIfTwo = (ASTNode)it.next(); 208 if(!(isItIfTwo instanceof ASTIfNode)){ 209 return null; 211 } 212 213 214 if(!matchPattern((ASTIfNode)isItIfOne,(ASTIfNode)isItIfTwo)){ 217 return null; 219 } 220 221 223 ASTIfNode firstOne = (ASTIfNode)isItIfOne; 225 ASTIfNode secondOne = (ASTIfNode)isItIfTwo; 226 227 ASTCondition firstCond = firstOne.get_Condition(); 229 ASTCondition secondCond = secondOne.get_Condition(); 230 231 ASTCondition newCond = new ASTOrCondition(firstCond,secondCond); 232 233 ASTIfNode newNode = new ASTIfNode(firstOne.get_Label(),newCond,firstOne.getIfBody()); 234 235 newSubBody.add(newNode); 237 238 239 while(it.hasNext()){ 241 newSubBody.add(it.next()); 242 } 243 244 return newSubBody; 246 } 247 248 249 256 private static boolean matchPattern(ASTIfNode one, ASTIfNode two){ 257 List subBodiesOne=one.get_SubBodies(); 258 List subBodiesTwo=two.get_SubBodies(); 259 260 if(subBodiesOne.size()!=1 || subBodiesTwo.size()!=1){ 261 return false; 263 } 264 List onlySubBodyOne = (List)subBodiesOne.get(0); 265 List onlySubBodyTwo = (List)subBodiesTwo.get(0); 266 267 if(onlySubBodyOne.size()!=1 || onlySubBodyTwo.size()!=1){ 268 return false; 270 } 271 272 ASTNode onlyASTNodeOne = (ASTNode)onlySubBodyOne.get(0); 273 ASTNode onlyASTNodeTwo = (ASTNode)onlySubBodyTwo.get(0); 274 275 if( !(onlyASTNodeOne instanceof ASTStatementSequenceNode) || !(onlyASTNodeTwo instanceof ASTStatementSequenceNode)){ 276 return false; 278 } 279 280 ASTStatementSequenceNode stmtSeqOne = (ASTStatementSequenceNode)onlyASTNodeOne; 281 ASTStatementSequenceNode stmtSeqTwo = (ASTStatementSequenceNode)onlyASTNodeTwo; 282 283 List stmtsOne = stmtSeqOne.getStatements(); 284 List stmtsTwo = stmtSeqTwo.getStatements(); 285 286 if(stmtsOne.size()!=1 || stmtsTwo.size()!=1){ 287 return false; 289 } 290 291 AugmentedStmt asOne = (AugmentedStmt)stmtsOne.get(0); 292 AugmentedStmt asTwo = (AugmentedStmt)stmtsTwo.get(0); 293 294 295 Stmt s1 = asOne.get_Stmt(); 296 Stmt s2 = asTwo.get_Stmt(); 297 298 if(s1.toString().compareTo(s2.toString())!=0){ 299 return false; 301 } 302 303 if(s1 instanceof DAbruptStmt && s2 instanceof DAbruptStmt){ 305 return true; 307 } 308 else if(s1 instanceof ReturnStmt && s2 instanceof ReturnStmt){ 309 return true; 311 } 312 else if (s1 instanceof ReturnVoidStmt && s2 instanceof ReturnVoidStmt){ 313 return true; 315 } 316 else 317 return false; 318 } 319 } | Popular Tags |