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