1 19 20 23 24 30 31 32 package soot.dava.toolkits.base.AST.traversals; 33 34 import soot.*; 35 import java.util.*; 36 import soot.jimple.*; 37 import soot.dava.internal.asg.*; 38 import soot.dava.internal.AST.*; 39 import soot.dava.internal.javaRep.*; 40 import soot.dava.toolkits.base.AST.analysis.*; 41 import soot.dava.toolkits.base.AST.structuredAnalysis.*; 42 43 62 public class ASTUsesAndDefs extends DepthFirstAdapter{ 63 HashMap uD; HashMap dU; ReachingDefs reaching; 67 public ASTUsesAndDefs(ASTNode AST){ 68 uD = new HashMap(); 69 dU = new HashMap(); 70 reaching = new ReachingDefs(AST); 71 } 72 73 public ASTUsesAndDefs(boolean verbose,ASTNode AST){ 74 super(verbose); 75 uD = new HashMap(); 76 dU = new HashMap(); 77 reaching = new ReachingDefs(AST); 78 } 79 80 81 82 83 84 88 private List getUsesFromBoxes(List useBoxes){ 89 ArrayList toReturn = new ArrayList(); 90 Iterator it = useBoxes.iterator(); 91 while(it.hasNext()){ 92 Value val =((ValueBox)it.next()).getValue(); 93 if(val instanceof Local) 94 toReturn.add(val); 95 } 96 return toReturn; 98 } 99 100 101 102 103 104 public void checkStatementUses(Stmt s,Object useNodeOrStatement){ 105 List useBoxes = s.getUseBoxes(); 106 List uses= getUsesFromBoxes(useBoxes); 108 110 Iterator it = uses.iterator(); 111 while(it.hasNext()){ 112 Local local = (Local)it.next(); 113 createUDDUChain(local,useNodeOrStatement); 114 } 116 117 123 if(s instanceof DefinitionStmt){ 124 if(dU.get(s)==null){ 126 dU.put(s,new ArrayList()); 127 } 128 } 129 } 130 131 132 133 134 135 136 137 138 143 public void createUDDUChain(Local local, Object useNodeOrStatement){ 144 147 List reachingDefs = reaching.getReachingDefs(local,useNodeOrStatement); 148 150 Object tempObj = uD.get(useNodeOrStatement); 152 if(tempObj != null){ 153 List tempList = (List)tempObj; 154 tempList.addAll(reachingDefs); 155 uD.put(useNodeOrStatement,tempList); 156 } 157 else 158 uD.put(useNodeOrStatement,reachingDefs); 159 160 Iterator defIt = reachingDefs.iterator(); 162 while(defIt.hasNext()){ 163 Object defStmt = defIt.next(); 165 166 Object useObj = dU.get(defStmt); 168 List uses=null; 169 if(useObj==null) 170 uses = new ArrayList(); 171 else 172 uses = (List)useObj; 173 174 uses.add(useNodeOrStatement); 176 dU.put(defStmt,uses); 178 } 179 } 180 181 182 183 184 185 186 187 188 189 190 191 192 196 public List getUseList(ASTCondition cond){ 197 ArrayList useList = new ArrayList(); 198 if(cond instanceof ASTAggregatedCondition){ 199 useList.addAll(getUseList(((ASTAggregatedCondition)cond).getLeftOp())); 200 useList.addAll(getUseList(((ASTAggregatedCondition)cond).getRightOp())); 201 return useList; 202 } 203 else if(cond instanceof ASTUnaryCondition){ 204 List uses = new ArrayList(); 206 207 Value val = ((ASTUnaryCondition)cond).getValue(); 208 if(val instanceof Local){ 209 uses.add(val); 210 } 211 else{ 212 List useBoxes = val.getUseBoxes(); 213 uses= getUsesFromBoxes(useBoxes); 214 } 215 return uses; 216 } 217 else if(cond instanceof ASTBinaryCondition){ 218 Value val = ((ASTBinaryCondition)cond).getConditionExpr(); 220 List useBoxes = val.getUseBoxes(); 221 return getUsesFromBoxes(useBoxes); 222 } 223 else{ 224 throw new RuntimeException ("Method getUseList in ASTUsesAndDefs encountered unknown condition type"); 225 } 226 } 227 228 229 230 231 232 233 234 235 236 237 238 239 243 public void checkConditionalUses(ASTCondition cond,ASTNode node){ 244 List useList = getUseList(cond); 245 246 248 Iterator it = useList.iterator(); 250 while(it.hasNext()){ 251 Local local = (Local)it.next(); 252 createUDDUChain(local,node); 254 } } 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 287 public void inASTSwitchNode(ASTSwitchNode node){ 288 Value val = (Value)node.get_Key(); 289 List uses = new ArrayList(); 290 if(val instanceof Local){ 291 uses.add(val); 292 } 293 else{ 294 List useBoxes = val.getUseBoxes(); 295 uses= getUsesFromBoxes(useBoxes); 296 } 297 298 Iterator it = uses.iterator(); 299 while(it.hasNext()){ 301 Local local = (Local)it.next(); 302 createUDDUChain(local,node); 304 } } 307 308 309 310 311 312 313 public void inASTSynchronizedBlockNode(ASTSynchronizedBlockNode node){ 314 Local local = node.getLocal(); 315 createUDDUChain(local,node); 316 } 317 318 319 320 321 322 323 324 328 public void inASTIfNode(ASTIfNode node){ 329 ASTCondition cond = node.get_Condition(); 330 checkConditionalUses(cond,node); 331 } 332 333 334 338 public void inASTIfElseNode(ASTIfElseNode node){ 339 ASTCondition cond = node.get_Condition(); 340 checkConditionalUses(cond,node); 341 } 342 343 344 345 346 350 public void inASTWhileNode(ASTWhileNode node){ 351 ASTCondition cond = node.get_Condition(); 352 checkConditionalUses(cond,node); 353 } 354 355 356 357 361 public void inASTDoWhileNode(ASTDoWhileNode node){ 362 ASTCondition cond = node.get_Condition(); 363 checkConditionalUses(cond,node); 364 } 365 366 367 368 369 375 public void inASTForLoopNode(ASTForLoopNode node){ 376 377 List init = node.getInit(); 379 Iterator it = init.iterator(); 380 while(it.hasNext()){ 381 AugmentedStmt as = (AugmentedStmt)it.next(); 382 Stmt s = as.get_Stmt(); 383 checkStatementUses(s,node); 384 } 385 386 ASTCondition cond = node.get_Condition(); 388 checkConditionalUses(cond,node); 389 390 391 List update = node.getUpdate(); 393 it = update.iterator(); 394 while(it.hasNext()){ 395 AugmentedStmt as = (AugmentedStmt)it.next(); 396 Stmt s = as.get_Stmt(); 397 checkStatementUses(s,node); 398 } 399 } 400 401 402 403 public void inASTStatementSequenceNode(ASTStatementSequenceNode node){ 404 List statements = node.getStatements(); 405 Iterator it = statements.iterator(); 406 407 while(it.hasNext()){ 408 AugmentedStmt as = (AugmentedStmt)it.next(); 409 Stmt s = as.get_Stmt(); 410 checkStatementUses(s,s); 414 } 415 } 416 417 418 424 public List getUDChain(Object node){ 425 return (List)uD.get(node); 426 } 427 428 429 432 public List getDUChain(Object node){ 433 return (List)dU.get(node); 434 } 435 436 public HashMap getDUHashMap(){ 437 return dU; 438 } 439 440 441 public void outASTMethodNode(ASTMethodNode node){ 442 } 444 445 public void print(){ 446 System.out.println("\n\n\nPRINTING uD dU CHAINS ______________________________"); 447 Iterator it = dU.keySet().iterator(); 448 while(it.hasNext()){ 449 DefinitionStmt s = (DefinitionStmt)it.next(); 450 System.out.println("*****The def "+s+" has following uses:"); 451 Object obj = dU.get(s); 452 if(obj !=null){ 453 ArrayList list = (ArrayList)obj; 454 Iterator tempIt = list.iterator(); 455 while(tempIt.hasNext()){ 456 Object tempUse = tempIt.next(); 457 System.out.println("-----------Use "+tempUse); 458 System.out.println("----------------Defs of this use: "+uD.get(tempUse)); 459 } 460 } 461 } 462 System.out.println("END --------PRINTING uD dU CHAINS ______________________________"); 463 } 464 465 } | Popular Tags |