1 19 20 21 24 25 26 package soot.dava.toolkits.base.AST.structuredAnalysis; 27 28 import soot.*; 29 import java.util.*; 30 import soot.jimple.*; 31 import soot.dava.internal.AST.*; 33 import soot.dava.toolkits.base.AST.traversals.*; 35 36 46 47 73 74 public class ReachingDefs extends StructuredAnalysis{ 75 Object toAnalyze; 76 77 public ReachingDefs(Object analyze){ 78 super(); 79 toAnalyze = analyze; 80 DavaFlowSet temp = (DavaFlowSet)process(analyze,new DavaFlowSet()); 81 } 82 83 84 88 public Object newInitialFlow(){ 89 DavaFlowSet initial = new DavaFlowSet(); 90 AllDefinitionsFinder defFinder = new AllDefinitionsFinder(); 92 ((ASTNode)toAnalyze).apply(defFinder); 93 List allDefs = defFinder.getAllDefs(); 94 Iterator defIt = allDefs.iterator(); 96 while(defIt.hasNext()) 97 initial.add(defIt.next()); 98 99 return initial; 101 } 102 103 104 105 109 public void setMergeType(){ 110 MERGETYPE=UNION; 111 } 112 113 114 115 public Object cloneFlowSet(Object flowSet){ 116 if(flowSet instanceof DavaFlowSet){ 117 return ((DavaFlowSet)flowSet).clone(); 118 } 119 else 120 throw new RuntimeException ("cloneFlowSet not implemented for other flowSet types"); 121 } 122 123 124 125 126 127 128 129 133 public Object processUnaryBinaryCondition(ASTUnaryBinaryCondition cond, Object input){ 134 if(!(input instanceof DavaFlowSet)){ 135 throw new RuntimeException ("processCondition is not implemented for other flowSet types"); 136 } 137 DavaFlowSet inSet = (DavaFlowSet)input; 138 return inSet; 139 } 140 141 142 146 public Object processSynchronizedLocal(Local local,Object input){ 147 if(!(input instanceof DavaFlowSet)){ 148 throw new RuntimeException ("processCondition is not implemented for other flowSet types"); 149 } 150 DavaFlowSet inSet = (DavaFlowSet)input; 151 return inSet; 152 } 153 154 155 159 public Object processSwitchKey(Value key,Object input){ 160 if(!(input instanceof DavaFlowSet)){ 161 throw new RuntimeException ("processCondition is not implemented for other flowSet types"); 162 } 163 DavaFlowSet inSet = (DavaFlowSet)input; 164 return inSet; 165 } 166 167 168 169 170 174 public Object processStatement(Stmt s, Object input){ 175 if(!(input instanceof DavaFlowSet)){ 176 throw new RuntimeException ("processStatement is not implemented for other flowSet types"); 177 } 178 DavaFlowSet inSet = (DavaFlowSet)input; 179 180 183 if(inSet == NOPATH){ 184 return inSet; 185 } 186 187 if(s instanceof DefinitionStmt){ 188 DavaFlowSet toReturn = (DavaFlowSet)cloneFlowSet(inSet); 189 193 Value leftOp = ((DefinitionStmt)s).getLeftOp(); 194 195 if(leftOp instanceof Local){ 196 kill(toReturn,(Local)leftOp); 198 gen(toReturn,(DefinitionStmt)s); 200 return toReturn; 201 } } 203 return input; 204 } 205 206 207 208 public void gen(DavaFlowSet in, DefinitionStmt s){ 209 in.add(s); 211 } 212 213 public void kill(DavaFlowSet in, Local redefined){ 214 String redefinedLocalName = redefined.getName(); 215 216 List list = in.toList(); 218 Iterator listIt = list.iterator(); 219 while(listIt.hasNext()){ 220 DefinitionStmt tempStmt = (DefinitionStmt)listIt.next(); 221 Value leftOp = tempStmt.getLeftOp(); 222 if(leftOp instanceof Local){ 223 String storedLocalName = ((Local)leftOp).getName(); 224 if(redefinedLocalName.compareTo(storedLocalName)==0){ 225 in.remove(tempStmt); 228 } 229 } 230 } 231 } 232 233 234 public List getReachingDefs(Local local,Object node){ 235 ArrayList toReturn = new ArrayList(); 236 237 Object beforeSet=null; 239 242 if(node instanceof ASTWhileNode || node instanceof ASTDoWhileNode || node instanceof ASTUnconditionalLoopNode 243 || node instanceof ASTForLoopNode) 244 beforeSet = getAfterSet(node); 245 else 246 beforeSet=getBeforeSet(node); 247 248 249 if(beforeSet == null){ 250 throw new RuntimeException ("Could not get reaching defs of node"); 251 } 252 if(!(beforeSet instanceof DavaFlowSet)){ 253 throw new RuntimeException ("Reaching def set is not a Dava FlowSet"); 254 } 255 DavaFlowSet beforeSetReaching = (DavaFlowSet)beforeSet; 256 257 258 List allReachingDefs = beforeSetReaching.toList(); 260 261 262 Iterator it = allReachingDefs.iterator(); 263 while(it.hasNext()){ 264 Object temp = it.next(); 266 if(!(temp instanceof DefinitionStmt)) 267 throw new RuntimeException ("Not an instanceof DefinitionStmt"+temp); 268 DefinitionStmt stmt = (DefinitionStmt)temp; 269 Value leftOp = stmt.getLeftOp(); 270 if(leftOp.toString().compareTo(local.toString())==0){ 271 toReturn.add(stmt); 272 } 273 } 274 return toReturn; 275 } 276 277 278 279 280 281 282 283 284 285 286 287 public void reachingDefsToString(Object node){ 288 ArrayList toReturn = new ArrayList(); 289 290 Object beforeSet=null; 292 295 if(node instanceof ASTWhileNode || node instanceof ASTDoWhileNode || node instanceof ASTUnconditionalLoopNode 296 || node instanceof ASTForLoopNode) 297 beforeSet = getAfterSet(node); 298 else 299 beforeSet=getBeforeSet(node); 300 301 302 303 if(beforeSet == null){ 304 throw new RuntimeException ("Could not get reaching defs of node"); 305 } 306 if(!(beforeSet instanceof DavaFlowSet)){ 307 throw new RuntimeException ("Reaching def set is not a Dava FlowSet"); 308 } 309 DavaFlowSet beforeSetReaching = (DavaFlowSet)beforeSet; 310 311 List allReachingDefs = beforeSetReaching.toList(); 313 Iterator it = allReachingDefs.iterator(); 314 while(it.hasNext()){ 315 System.out.println("Reaching def:"+it.next()); 316 } 317 } 318 } | Popular Tags |