1 19 20 25 26 27 package soot.toolkits.graph; 28 29 import soot.util.*; 30 import java.util.*; 31 import soot.*; 32 import soot.baf.*; 33 34 35 36 37 46 public class Block 47 { 48 private Unit mHead, mTail; 49 private Body mBody; 50 private List mPreds, mSuccessors; 51 private int mPredCount = 0, mBlockLength = 0, mIndexInMethod = 0; 52 private BlockGraph mBlockGraph; 53 54 72 public Block(Unit aHead, Unit aTail, Body aBody, int aIndexInMethod, int aBlockLength, BlockGraph aBlockGraph) 73 { 74 mHead = aHead; 75 mTail = aTail; 76 mBody = aBody; 77 mIndexInMethod = aIndexInMethod; 78 mBlockLength = aBlockLength; 79 mBlockGraph = aBlockGraph; 80 } 81 82 83 84 92 public Body getBody() 93 { 94 return mBody; 95 } 96 97 98 105 public Iterator iterator() 106 { 107 if(mBody != null) 108 { 109 Chain units = mBody.getUnits(); 110 return units.iterator(mHead, mTail); 111 } else { 112 return null; 113 } 114 } 115 116 126 public void insertBefore(Unit toInsert, Unit point) 127 { 128 if(point == mHead) 129 mHead = toInsert; 130 131 Chain methodBody = mBody.getUnits(); 132 methodBody.insertBefore(toInsert, point); 133 } 134 135 136 144 public void insertAfter(Unit toInsert, Unit point) 145 { 146 if(point == mTail) 147 mTail = toInsert; 148 149 Chain methodBody = mBody.getUnits(); 150 methodBody.insertAfter(toInsert, point); 151 } 152 153 154 155 162 public boolean remove(Unit item) 163 { 164 Chain methodBody = mBody.getUnits(); 165 166 if(item == mHead) 167 mHead = (Unit)methodBody.getSuccOf(item); 168 else if(item == mTail) 169 mTail = (Unit) methodBody.getPredOf(item); 170 171 return methodBody.remove(item); 172 } 173 174 182 public Unit getSuccOf(Unit aItem) 183 { 184 Chain methodBody = mBody.getUnits(); 185 if(aItem != mTail) 186 return (Unit) methodBody.getSuccOf(aItem); 187 else 188 return null; 189 } 190 191 198 public Unit getPredOf(Unit aItem) 199 { 200 Chain methodBody = mBody.getUnits(); 201 if(aItem != mHead) 202 return (Unit) methodBody.getPredOf(aItem); 203 else 204 return null; 205 } 206 207 215 public void setIndexInMethod(int aIndexInMethod) 216 { 217 mIndexInMethod = aIndexInMethod; 218 } 219 220 225 public int getIndexInMethod() 226 { 227 return mIndexInMethod; 228 } 229 230 234 public Unit getHead() 235 { 236 return mHead; 237 } 238 239 243 public Unit getTail() 244 { 245 return mTail; 246 } 247 248 255 public void setPreds(List preds) 256 { 257 mPreds = preds; 258 return; 259 } 260 261 266 public List getPreds() 267 { 268 return mPreds; 269 } 270 271 272 273 280 public void setSuccs(List succs) 281 { 282 mSuccessors = succs; 283 } 284 285 286 287 292 public List getSuccs() 293 { 294 return mSuccessors; 295 } 296 297 298 299 private Map buildMapForBlock() 300 { 301 Map m = new HashMap(); 302 List basicBlocks = mBlockGraph.getBlocks(); 303 Iterator it = basicBlocks.iterator(); 304 while(it.hasNext()) { 305 Block currentBlock = (Block) it.next(); 306 m.put(currentBlock.getHead(), "block" + (new Integer (currentBlock.getIndexInMethod()).toString())); 307 } 308 return m; 309 } 310 311 312 Map allMapToUnnamed = new AllMapTo("???"); 313 314 class AllMapTo extends AbstractMap 315 { 316 Object dest; 317 318 public AllMapTo(Object dest) 319 { 320 this.dest = dest; 321 } 322 323 public Object get(Object key) 324 { 325 return dest; 326 } 327 328 public Set entrySet() 329 { 330 throw new UnsupportedOperationException (); 331 } 332 } 333 334 335 336 public String toShortString() {return "Block #" + mIndexInMethod; } 337 338 public String toString() 339 { 340 StringBuffer strBuf = new StringBuffer (); 341 342 343 344 346 strBuf.append("Block " + mIndexInMethod + ":" + System.getProperty("line.separator")); 347 strBuf.append("[preds: "); 348 int count = 0; 349 if(mPreds != null) { 350 Iterator it = mPreds.iterator(); 351 while(it.hasNext()) { 352 353 strBuf.append(((Block) it.next()).getIndexInMethod()+ " "); 354 } 355 } 356 strBuf.append("] [succs: "); 357 if(mSuccessors != null) { 358 Iterator it = mSuccessors.iterator(); 359 while(it.hasNext()) { 360 361 strBuf.append(((Block) it.next()).getIndexInMethod() + " "); 362 } 363 364 } 365 366 strBuf.append("]" + System.getProperty("line.separator")); 367 368 369 370 372 Chain methodUnits = mBody.getUnits(); 373 Iterator basicBlockIt = methodUnits.iterator(mHead, mTail); 374 375 if(basicBlockIt.hasNext()) { 376 Unit someUnit = (Unit) basicBlockIt.next(); 377 strBuf.append(someUnit.toString() + ";" + System.getProperty("line.separator")); 378 while(basicBlockIt.hasNext()){ 379 someUnit = (Unit) basicBlockIt.next(); 380 if(someUnit == mTail) 381 break; 382 strBuf.append(someUnit.toString() + ";" + System.getProperty("line.separator")); 383 } 384 someUnit = mTail; 385 if(mTail == null) 386 strBuf.append("error: null tail found; block length: " + mBlockLength +"" + System.getProperty("line.separator")); 387 else if(mHead != mTail) 388 strBuf.append(someUnit.toString() + ";" + System.getProperty("line.separator")); 389 390 391 } 392 396 return strBuf.toString(); 397 } 398 399 } 400 | Popular Tags |