lots of bug fixes
[IRC.git] / Robust / src / IR / Flat / FlatNode.java
1 package IR.Flat;
2 import java.util.Vector;
3 import java.util.HashSet;
4 import java.util.Set;
5 import java.util.Iterator;
6
7 public class FlatNode {
8   public Vector next;
9   protected Vector prev;
10
11   public FlatNode() {
12     next=new Vector();
13     prev=new Vector();
14   }
15
16   public String toString() {
17     throw new Error(this.getClass().getName() + "does not implement toString!");
18   }
19   public int numNext() {
20     return next.size();
21   }
22   public FlatNode getNext(int i) {
23     return (FlatNode) next.get(i);
24   }
25
26   public int numPrev() {
27     return prev.size();
28   }
29   public FlatNode getPrev(int i) {
30     return (FlatNode) prev.get(i);
31   }
32   public void addNext(FlatNode n) {
33     next.add(n);
34     n.addPrev(this);
35   }
36   public void removePrev(FlatNode n) {
37     prev.remove(n);
38   }
39   /** This function modifies the graph */
40   public void setNext(int i, FlatNode n) {
41     FlatNode old=getNext(i);
42     next.set(i, n);
43     old.prev.remove(this);
44     n.addPrev(this);
45   }
46   /** This function modifies the graph */
47   public void setNewNext(int i, FlatNode n) {
48     if (next.size()<=i)
49         next.setSize(i+1);
50     next.set(i, n);
51     n.addPrev(this);
52   }
53   /** This function modifies the graph */
54   public void setprev(int i, FlatNode n) {
55     prev.set(i, n);
56   }
57   /** This function modifies the graph */
58   public void setnext(int i, FlatNode n) {
59     next.set(i, n);
60   }
61   public void addPrev(FlatNode p) {
62     prev.add(p);
63   }
64   public int kind() {
65     throw new Error();
66   }
67   public TempDescriptor [] readsTemps() {
68     return new TempDescriptor[0];
69   }
70   public TempDescriptor [] writesTemps() {
71     return new TempDescriptor[0];
72   }
73   public FlatNode clone(TempMap t) {
74     throw new Error("no clone method for"+this);
75   }
76
77   public void rewriteUse(TempMap t) {
78     System.out.println(toString());
79     throw new Error();
80   }
81
82   public void rewriteDef(TempMap t) {
83     System.out.println(toString());
84     throw new Error();
85   }
86
87   public Set<FlatNode> getReachableSet(Set<FlatNode> endset) {
88     HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
89     HashSet<FlatNode> visited=new HashSet<FlatNode>();
90     tovisit.add(this);
91     while(!tovisit.isEmpty()) {
92       FlatNode fn=tovisit.iterator().next();
93       tovisit.remove(fn);
94       visited.add(fn);
95       if (endset!=null&&!endset.contains(fn)) {
96         for(int i=0; i<fn.numNext(); i++) {
97           FlatNode nn=fn.getNext(i);
98           if (!visited.contains(nn))
99             tovisit.add(nn);
100         }
101       }
102     }
103     return visited;
104   }
105
106   public void replace(FlatNode fnnew) {
107     fnnew.prev.setSize(prev.size());
108     fnnew.next.setSize(next.size());
109     for(int i=0;i<prev.size();i++) {
110       FlatNode nprev=(FlatNode)prev.get(i);
111       fnnew.prev.set(i,nprev);
112       for(int j=0;j<nprev.numNext();j++) {
113         FlatNode n=nprev.getNext(j);
114         if (n==this)
115           nprev.next.set(j, fnnew);
116       }
117     }
118     for(int i=0;i<next.size();i++) {
119       FlatNode nnext=(FlatNode)next.get(i);
120       fnnew.next.set(i,nnext);
121       for(int j=0;j<nnext.numPrev();j++) {
122         FlatNode n=nnext.getPrev(j);
123         if (n==this)
124           nnext.prev.set(j, fnnew);
125       }
126     }
127     next=null;
128     prev=null;
129   }
130 }