beginning of points-to analysis
[IRC.git] / Robust / src / Analysis / Pointer / BasicBlock.java
1 package Analysis.Pointer;
2 import Analysis.Disjoint.PointerMethod;
3 import java.util.*;
4 import IR.Flat.*;
5
6 public class BasicBlock {
7   public BBlock start;
8   public BBlock exit;
9
10   public BasicBlock(BBlock start, BBlock exit) {
11     this.start=start;
12     this.exit=exit;
13   }
14
15   public BBlock getStart() {
16     return start;
17   }
18
19   public BBlock getExit() {
20     return exit;
21   }
22
23   public static class BBlock {
24     Vector<FlatNode> nodes;
25     Vector<BBlock> prevb;
26     Vector<BBlock> nextb;
27
28     public BBlock() {
29       nodes=new Vector<FlatNode>();
30       prevb=new Vector<BBlock>();
31       nextb=new Vector<BBlock>();
32     }
33
34     public Vector<FlatNode> nodes() {
35       return nodes;
36     }
37     public Vector<BBlock> next() {
38       return nextb;
39     }
40     public Vector<BBlock> prev() {
41       return prevb;
42     }
43   }
44
45   public static BasicBlock getBBlock(FlatMethod fm) {
46     BBlock exit=null;
47     Stack<FlatNode> toprocess=new Stack<FlatNode>();
48     HashMap<FlatNode, BBlock> map=new HashMap<FlatNode, BBlock>();
49     PointerMethod pm=new PointerMethod();
50     pm.analyzeMethod(fm);
51     toprocess.add(fm);
52     map.put(fm, new BBlock());
53
54     while(!toprocess.isEmpty()) {
55       FlatNode fn=toprocess.pop();
56       BBlock block=map.get(fn);
57       block.nodes.add(fn);
58       if (fn.kind()==FKind.FlatExit)
59         exit=block;
60       do {
61         if (pm.numNext(fn)!=1) {
62           for(int i=0;i<pm.numNext(fn);i++) {
63             FlatNode fnext=pm.getNext(fn,i);
64             if (!map.containsKey(fnext)) {
65               map.put(fn, new BBlock());
66               toprocess.add(fn);
67             }
68             //link block in
69             if (!block.nextb.contains(map.get(fn))) {
70               block.nextb.add(map.get(fn));
71               map.get(fn).prevb.add(block);
72             }
73           }
74           break;
75         }
76         fn=pm.getNext(fn,0);
77         if (fn.numPrev()>1) {
78           //new basic block
79           if (!map.containsKey(fn)) {
80             map.put(fn, new BBlock());
81             toprocess.add(fn);
82           }
83           //link block in
84           if (!block.nextb.contains(map.get(fn))) {
85             block.nextb.add(map.get(fn));
86             map.get(fn).prevb.add(block);
87           }
88           break;
89         }
90         block.nodes.add(fn);
91       } while(true);
92     }
93
94     return new BasicBlock(map.get(fm), exit);
95   }
96 }