Working towards dynamic source variables in MLP, not quite ready
[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
37   public void removeNext(FlatNode n) {
38     next.remove(n);
39   }
40   public void removePrev(FlatNode n) {
41     prev.remove(n);
42   }
43
44   /** This function modifies the graph */
45   public void setNext(int i, FlatNode n) {
46     FlatNode old=getNext(i);
47     next.set(i, n);
48     old.prev.remove(this);
49     n.addPrev(this);
50   }
51   /** This function modifies the graph */
52   public void setNewNext(int i, FlatNode n) {
53     if (next.size()<=i)
54         next.setSize(i+1);
55     next.set(i, n);
56     n.addPrev(this);
57   }
58   /** This function modifies the graph */
59   public void setprev(int i, FlatNode n) {
60     prev.set(i, n);
61   }
62   /** This function modifies the graph */
63   public void setnext(int i, FlatNode n) {
64     next.set(i, n);
65   }
66   public void addPrev(FlatNode p) {
67     prev.add(p);
68   }
69   public int kind() {
70     throw new Error();
71   }
72   public TempDescriptor [] readsTemps() {
73     return new TempDescriptor[0];
74   }
75   public TempDescriptor [] writesTemps() {
76     return new TempDescriptor[0];
77   }
78   public FlatNode clone(TempMap t) {
79     throw new Error("no clone method for"+this);
80   }
81
82   public void rewriteUse(TempMap t) {
83     System.out.println(toString());
84     throw new Error();
85   }
86
87   public void rewriteDef(TempMap t) {
88     System.out.println(toString());
89     throw new Error();
90   }
91
92   public Set<FlatNode> getReachableSet(Set<FlatNode> endset) {
93     HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
94     HashSet<FlatNode> visited=new HashSet<FlatNode>();
95     tovisit.add(this);
96     while(!tovisit.isEmpty()) {
97       FlatNode fn=tovisit.iterator().next();
98       tovisit.remove(fn);
99       visited.add(fn);
100       if (endset!=null&&!endset.contains(fn)) {
101         for(int i=0; i<fn.numNext(); i++) {
102           FlatNode nn=fn.getNext(i);
103           if (!visited.contains(nn))
104             tovisit.add(nn);
105         }
106       }
107     }
108     return visited;
109   }
110
111   public void replace(FlatNode fnnew) {
112     fnnew.prev.setSize(prev.size());
113     fnnew.next.setSize(next.size());
114     for(int i=0;i<prev.size();i++) {
115       FlatNode nprev=(FlatNode)prev.get(i);
116       fnnew.prev.set(i,nprev);
117       for(int j=0;j<nprev.numNext();j++) {
118         FlatNode n=nprev.getNext(j);
119         if (n==this)
120           nprev.next.set(j, fnnew);
121       }
122     }
123     for(int i=0;i<next.size();i++) {
124       FlatNode nnext=(FlatNode)next.get(i);
125       fnnew.next.set(i,nnext);
126       for(int j=0;j<nnext.numPrev();j++) {
127         FlatNode n=nnext.getPrev(j);
128         if (n==this)
129           nnext.prev.set(j, fnnew);
130       }
131     }
132     next=null;
133     prev=null;
134   }
135 }