variable source tokens have a temp for the live variable name and a temp for the...
[IRC.git] / Robust / src / Analysis / MLP / VarSrcTokTable.java
1 package Analysis.MLP;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8 public class VarSrcTokTable {
9   
10   // the true set represents the set of (sese, variable, age)
11   // triples that are truly in the table
12   private HashSet<VariableSourceToken> trueSet;
13
14   // these hashtables provide an efficient retreival from the
15   // true set.  Note that a particular triple from the quick
16   // look up must be checked against the true set--remove ops
17   // can cause the hashtables to be inconsistent to each other
18   private Hashtable< TempDescriptor,    Set<VariableSourceToken> >  var2vst;
19   private Hashtable< FlatSESEEnterNode, Set<VariableSourceToken> > sese2vst;
20   private Hashtable< SVKey,             Set<VariableSourceToken> >   sv2vst;
21
22   // maximum age from aging operation
23   private Integer MAX_AGE = new Integer( 2 );
24
25
26   public VarSrcTokTable() {
27     trueSet = new HashSet<VariableSourceToken>();
28
29     sese2vst = new Hashtable< FlatSESEEnterNode, Set<VariableSourceToken> >();
30     var2vst  = new Hashtable< TempDescriptor,    Set<VariableSourceToken> >();
31     sv2vst   = new Hashtable< SVKey,             Set<VariableSourceToken> >();
32   }
33
34
35   public void add( VariableSourceToken vst ) {
36     trueSet.add( vst );
37
38     Set<VariableSourceToken> s;
39
40     s = sese2vst.get( vst.getSESE() );
41     if( s == null ) {
42       s = new HashSet<VariableSourceToken>();
43     }
44     s.add( vst );
45     sese2vst.put( vst.getSESE(), s );
46
47     s = var2vst.get( vst.getVarLive() );
48     if( s == null ) {
49       s = new HashSet<VariableSourceToken>();
50     }
51     s.add( vst );
52     var2vst.put( vst.getVarLive(), s );
53
54     SVKey key = new SVKey( vst.getSESE(), vst.getVarLive() );
55     s = sv2vst.get( key );
56     if( s == null ) {
57       s = new HashSet<VariableSourceToken>();
58     }
59     s.add( vst );
60     sv2vst.put( key, s );
61   }
62
63   public void addAll( Set<VariableSourceToken> s ) {
64     Iterator<VariableSourceToken> itr = s.iterator();
65     while( itr.hasNext() ) {
66       add( itr.next() );
67     }
68   }
69
70
71   public Set<VariableSourceToken> get() {
72     return trueSet;
73   }
74
75   public Set<VariableSourceToken> get( FlatSESEEnterNode sese ) {
76     Set<VariableSourceToken> s = sese2vst.get( sese );
77     if( s == null ) {
78       s = new HashSet<VariableSourceToken>();      
79       sese2vst.put( sese, s );
80     }
81     s.retainAll( trueSet );
82     return s;
83   }
84
85   public Set<VariableSourceToken> get( TempDescriptor var ) {
86     Set<VariableSourceToken> s = var2vst.get( var );
87     if( s == null ) {
88       s = new HashSet<VariableSourceToken>();
89       var2vst.put( var, s );
90     }
91     s.retainAll( trueSet );
92     return s;
93   }
94
95   public Set<VariableSourceToken> get( SVKey key ) {
96     Set<VariableSourceToken> s = sv2vst.get( key );
97     if( s == null ) {
98       s = new HashSet<VariableSourceToken>();
99       sv2vst.put( key, s );
100     }
101     s.retainAll( trueSet );
102     return s;
103   }
104
105
106   public void merge( VarSrcTokTable table ) {
107
108     if( table == null ) {
109       return;
110     }
111
112     trueSet.addAll( table.trueSet );
113
114     Iterator itr; 
115     Set s;
116
117     itr = sese2vst.entrySet().iterator();
118     while( itr.hasNext() ) {
119       Map.Entry                me   = (Map.Entry)                itr.next();
120       FlatSESEEnterNode        sese = (FlatSESEEnterNode)        me.getKey();
121       Set<VariableSourceToken> s1   = (Set<VariableSourceToken>) me.getValue();
122       Set<VariableSourceToken> s2   = table.sese2vst.get( sese );
123       
124       assert s1 != null;
125
126       if( s2 != null ) {
127         s1.addAll( s2 );
128       }           
129     }
130     s = table.sese2vst.entrySet();
131     s.removeAll( sese2vst.entrySet() );
132     sese2vst.putAll( table.sese2vst );
133
134     itr = var2vst.entrySet().iterator();
135     while( itr.hasNext() ) {
136       Map.Entry                me  = (Map.Entry)                itr.next();
137       TempDescriptor           var = (TempDescriptor)           me.getKey();
138       Set<VariableSourceToken> s1  = (Set<VariableSourceToken>) me.getValue();
139       Set<VariableSourceToken> s2  = table.var2vst.get( var );
140       
141       assert s1 != null;
142
143       if( s2 != null ) {
144         s1.addAll( s2 );
145       }           
146     }
147     s = table.var2vst.entrySet();
148     s.removeAll( var2vst.entrySet() );
149     var2vst.putAll( table.var2vst );
150
151     itr = sv2vst.entrySet().iterator();
152     while( itr.hasNext() ) {
153       Map.Entry                me  = (Map.Entry)                itr.next();
154       SVKey                    key = (SVKey)                    me.getKey();
155       Set<VariableSourceToken> s1  = (Set<VariableSourceToken>) me.getValue();
156       Set<VariableSourceToken> s2  = table.sv2vst.get( key );
157       
158       assert s1 != null;
159
160       if( s2 != null ) {
161         s1.addAll( s2 );
162       }           
163     }
164     s = table.sv2vst.entrySet();
165     s.removeAll( sv2vst.entrySet() );
166     sv2vst.putAll( table.sv2vst );
167   }
168
169
170   public void remove( FlatSESEEnterNode sese ) {
171     Set<VariableSourceToken> s = sese2vst.get( sese );
172     if( s == null ) {
173       return;
174     }
175     
176     trueSet.removeAll( s );        
177     sese2vst.remove( sese );
178   }
179
180   public void remove( TempDescriptor var ) {
181     Set<VariableSourceToken> s = var2vst.get( var );
182     if( s == null ) {
183       return;
184     }
185     
186     trueSet.removeAll( s );        
187     var2vst.remove( var );
188   }
189
190   public void remove( FlatSESEEnterNode sese,
191                       TempDescriptor    var  ) {
192
193     SVKey key = new SVKey( sese, var );
194     Set<VariableSourceToken> s = sv2vst.get( key );
195     if( s == null ) {
196       return;
197     }
198     
199     trueSet.removeAll( s );
200     sv2vst.remove( key );
201   }
202
203   public void remove( VariableSourceToken vst ) {
204     trueSet.remove( vst );
205   }
206
207
208   // return a new table based on this one and
209   // age tokens with respect to SESE curr, where
210   // any child becomes curr with age 0, and any
211   // curr tokens increase age by 1
212   public VarSrcTokTable age( FlatSESEEnterNode curr ) {
213
214     VarSrcTokTable out = new VarSrcTokTable();
215     /*
216     Iterator<VariableSourceToken> itr = trueSet.iterator();
217     while( itr.hasNext() ) {
218       VariableSourceToken vst = itr.next();
219       if( vst.getSESE().equals( curr ) ) {
220         Integer newAge = vst.getAge()+1;
221         if( newAge > MAX_AGE ) {
222           newAge = MAX_AGE;
223         }
224         out.add( new VariableSourceToken( curr, 
225                                           vst.getVarLive(), 
226                                           newAge ) );
227       } else {
228         assert curr.getChildren().contains( vst.getSESE() );
229         out.add( new VariableSourceToken( curr, 
230                                           vst.getVarLive(), 
231                                           new Integer( 1 ) ) );
232       }
233     }
234     */
235     return out;
236   }
237
238
239   public boolean equals( Object o ) {
240     if( o == null ) {
241       return false;
242     }
243
244     if( !(o instanceof VarSrcTokTable) ) {
245       return false;
246     }
247
248     VarSrcTokTable table = (VarSrcTokTable) o;
249     return trueSet.equals( table.trueSet );
250   }
251
252   public int hashCode() {
253     return trueSet.hashCode();
254   }
255
256   public Iterator<VariableSourceToken> iterator() {
257     return trueSet.iterator();
258   }
259
260   public String toString() {
261     return "trueSet ="+trueSet.toString()+"\n"+
262            "sese2vst="+sese2vst.toString()+"\n"+
263            "var2vst ="+var2vst.toString()+"\n"+
264            "sv2vst  ="+sv2vst.toString();
265   }
266 }