cleaning up OoOJava and related systems, touching lots of files, these systems are...
[IRC.git] / Robust / src / IR / Flat / FlatSESEEnterNode.java
1 package IR.Flat;
2 import java.util.HashSet;
3 import java.util.Hashtable;
4 import java.util.Set;
5 import java.util.Vector;
6 import java.util.Iterator;
7 import java.util.Collection;
8 import Analysis.OoOJava.VariableSourceToken;
9 import Analysis.OoOJava.SESEandAgePair;
10 import Analysis.OwnershipAnalysis.HeapRegionNode;
11 import IR.ClassDescriptor;
12 import IR.FieldDescriptor;
13 import IR.MethodDescriptor;
14 import IR.TypeDescriptor;
15 import IR.Tree.SESENode;
16
17 public class FlatSESEEnterNode extends FlatNode {
18   
19   // SESE class identifiers should be numbered
20   // sequentially from 0 to 1-(total # SESE's)
21   private static int identifier=0;
22
23   private   int               id;
24   protected FlatSESEExitNode  exit;
25   protected SESENode          treeNode;
26   protected Integer           oldestAgeToTrack;
27
28   // a leaf tasks simply has no children, ever
29   protected static final int ISLEAF_UNINIT = 1;
30   protected static final int ISLEAF_FALSE  = 2;
31   protected static final int ISLEAF_TRUE   = 3;
32   protected int isLeafSESE;
33
34   // there is only one main sese that is implicit
35   // (spliced in by the compiler around whole program)
36   protected boolean isMainSESE;
37
38   // all children tasks, INCLUDING those that are reachable
39   // by calling methods
40   protected Set<FlatSESEEnterNode> children;
41   
42   // all possible parents
43   protected Set<FlatSESEEnterNode> parents;
44
45   // sometimes it is useful to know the locally defined
46   // parent or children of an SESE for various analysis,
47   // and by local it is one SESE nested within another
48   // in a single method context
49   protected Set<FlatSESEEnterNode> localChildren;  
50   protected FlatSESEEnterNode localParent;
51
52
53   protected Set<TempDescriptor> inVars;
54   protected Set<TempDescriptor> outVars;
55
56   protected Set<SESEandAgePair> needStaticNameInCode;
57
58   protected Set<SESEandAgePair> staticInVarSrcs;
59
60   protected Set<TempDescriptor> readyInVars;
61   protected Set<TempDescriptor> staticInVars;
62   protected Set<TempDescriptor> dynamicInVars;  
63
64   protected Set<TempDescriptor> dynamicVars;
65
66   protected Hashtable<TempDescriptor, VariableSourceToken> staticInVar2src;
67   
68
69   // a subset of the in-set variables that shouuld be traversed during
70   // the dynamic coarse grained conflict strategy, remember them here so
71   // buildcode can be dumb and just gen the traversals
72   protected Vector<TempDescriptor> inVarsForDynamicCoarseConflictResolution;
73
74   // scope info for this SESE
75   protected FlatMethod       fmEnclosing;
76   protected MethodDescriptor mdEnclosing;
77   protected ClassDescriptor  cdEnclosing;
78
79   // structures that allow SESE to appear as
80   // a normal method to code generation
81   protected FlatMethod       fmBogus;
82   protected MethodDescriptor mdBogus;
83
84   // used during code generation to calculate an offset
85   // into the SESE-specific record, specifically to the
86   // first field in a sequence of pointers to other SESE
87   // records which is relevant to garbage collection
88   protected String firstDepRecField;
89   protected int    numDepRecs;
90   
91
92   public FlatSESEEnterNode( SESENode sn ) {
93     this.id              = identifier++;
94     treeNode             = sn;
95     oldestAgeToTrack     = new Integer( 0 );
96     children             = new HashSet<FlatSESEEnterNode>();
97     parents              = new HashSet<FlatSESEEnterNode>();
98     localChildren        = new HashSet<FlatSESEEnterNode>();
99     localParent          = null;
100     inVars               = new HashSet<TempDescriptor>();
101     outVars              = new HashSet<TempDescriptor>();
102     needStaticNameInCode = new HashSet<SESEandAgePair>();
103     staticInVarSrcs      = new HashSet<SESEandAgePair>();
104     readyInVars          = new HashSet<TempDescriptor>();
105     staticInVars         = new HashSet<TempDescriptor>();
106     dynamicInVars        = new HashSet<TempDescriptor>();
107     dynamicVars          = new HashSet<TempDescriptor>();
108
109     inVarsForDynamicCoarseConflictResolution = new Vector<TempDescriptor>();
110     
111     staticInVar2src = new Hashtable<TempDescriptor, VariableSourceToken>();
112     
113     fmEnclosing = null;
114     mdEnclosing = null;
115     cdEnclosing = null;
116
117     isLeafSESE = ISLEAF_UNINIT;
118
119     isMainSESE = false;
120
121     firstDepRecField = null;
122     numDepRecs       = 0;
123   }
124
125   public void rewriteUse() {
126   }
127
128   public void rewriteDef() {
129   }
130
131   public void setFlatExit( FlatSESEExitNode fsexn ) {
132     exit = fsexn;
133   }
134
135   public FlatSESEExitNode getFlatExit() {
136     return exit;
137   }
138
139   public void setIsMainSESE() {
140     isMainSESE = true;
141   }
142
143   public boolean getIsMainSESE() {
144     return isMainSESE;
145   }
146
147   public int kind() {
148     return FKind.FlatSESEEnterNode;
149   }
150
151   public SESENode getTreeNode() {
152     return treeNode;
153   }
154
155   public int getIdentifier() {
156     return id;
157   }
158
159   public String getPrettyIdentifier() {
160     if( treeNode.getID() != null ) {
161       return treeNode.getID();
162     }     
163     return ""+id;
164   }
165
166   public String toString() {
167     return "sese "+getPrettyIdentifier()+" enter";
168   }
169   
170   public String toPrettyString() {
171     return "sese "+getPrettyIdentifier()+getIdentifier();
172   }
173
174
175
176   public void addParent( FlatSESEEnterNode parent ) {
177     parents.add( parent );
178   }
179
180   public Set<FlatSESEEnterNode> getParents() {
181     return parents;
182   }
183
184   public void setLocalParent( FlatSESEEnterNode parent ) {
185     localParent = parent;
186   }
187
188   public FlatSESEEnterNode getLocalParent() {
189     return localParent;
190   }
191
192   public void addChild( FlatSESEEnterNode child ) {
193     children.add( child );
194   }
195
196   public void addChildren( Set<FlatSESEEnterNode> batch ) {
197     children.addAll( batch );
198   }
199
200   public Set<FlatSESEEnterNode> getChildren() {
201     return children;
202   }
203
204   public void addLocalChild( FlatSESEEnterNode child ) {
205     localChildren.add( child );
206   }
207
208   public Set<FlatSESEEnterNode> getLocalChildren() {
209     return localChildren;
210   }
211
212
213
214   public void addInVar( TempDescriptor td ) {
215     if (!inVars.contains(td))
216       inVars.add( td );
217   }
218
219   public void addOutVar( TempDescriptor td ) {
220     outVars.add( td );
221   }
222
223   public void addInVarSet( Set<TempDescriptor> s ) {
224     inVars.addAll(s);
225   }
226
227   public void addOutVarSet( Set<TempDescriptor> s ) {
228     outVars.addAll( s );
229   }
230
231   public Set<TempDescriptor> getInVarSet() {
232     return inVars;
233   }
234
235   Vector<TempDescriptor> vecinVars;
236   void buildvarVec() {
237     HashSet<TempDescriptor> paramset=new HashSet<TempDescriptor>();
238     paramset.addAll(inVars);
239     paramset.addAll(outVars);
240     vecinVars=new Vector<TempDescriptor>();
241     vecinVars.addAll(paramset);
242   }
243
244   public TempDescriptor getParameter(int i) {
245     if (vecinVars==null) {
246       buildvarVec();
247     }
248     return vecinVars.get(i);
249   }
250
251   public int numParameters() {
252     if (vecinVars==null) {
253       buildvarVec();
254     }
255     return vecinVars.size();
256   }
257
258   public Set<FlatNode> getNodeSet() {
259     HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
260     HashSet<FlatNode> visited=new HashSet<FlatNode>();
261     tovisit.add(this);
262     while(!tovisit.isEmpty()) {
263       FlatNode fn=tovisit.iterator().next();
264       tovisit.remove(fn);
265       visited.add(fn);
266       
267       if (fn!=exit) {
268         for(int i=0; i<fn.numNext(); i++) {
269           FlatNode nn=fn.getNext(i);
270           if (!visited.contains(nn))
271             tovisit.add(nn);
272         }
273       }
274     }
275     return visited;
276   }
277
278   public Set<TempDescriptor> getOutVarSet() {
279     return outVars;
280   }
281
282   public void addNeededStaticName( SESEandAgePair p ) {
283     needStaticNameInCode.add( p );
284   }
285
286   public Set<SESEandAgePair> getNeededStaticNames() {
287     return needStaticNameInCode;
288   }
289
290   public void addStaticInVarSrc( SESEandAgePair p ) {
291     staticInVarSrcs.add( p );
292   }
293
294   public Set<SESEandAgePair> getStaticInVarSrcs() {
295     return staticInVarSrcs;
296   }
297
298   public void addReadyInVar( TempDescriptor td ) {
299     readyInVars.add( td );
300   }
301
302   public Set<TempDescriptor> getReadyInVarSet() {
303     return readyInVars;
304   }
305
306   public void addStaticInVar( TempDescriptor td ) {
307     staticInVars.add( td );
308   }
309
310   public Set<TempDescriptor> getStaticInVarSet() {
311     return staticInVars;
312   }
313
314   public void putStaticInVar2src( TempDescriptor staticInVar,
315                                   VariableSourceToken vst ) {
316     staticInVar2src.put( staticInVar, vst );
317   }
318
319   public VariableSourceToken getStaticInVarSrc( TempDescriptor staticInVar ) {
320     return staticInVar2src.get( staticInVar );
321   }
322
323   public void addDynamicInVar( TempDescriptor td ) {
324     dynamicInVars.add( td );
325   }
326
327   public Set<TempDescriptor> getDynamicInVarSet() {
328     return dynamicInVars;
329   }
330
331   public void addDynamicVar( TempDescriptor td ) {
332     dynamicVars.add( td );
333   }
334
335   public Set<TempDescriptor> getDynamicVarSet() {
336     return dynamicVars;
337   }
338
339   public void mustTrackAtLeastAge( Integer age ) {
340     if( age > oldestAgeToTrack ) {
341       oldestAgeToTrack = new Integer( age );
342     }    
343   }
344
345   public Integer getOldestAgeToTrack() {
346     return oldestAgeToTrack;
347   }
348
349   public void setfmEnclosing( FlatMethod fm ) { fmEnclosing = fm; }
350   public FlatMethod getfmEnclosing() { return fmEnclosing; }
351
352   public void setmdEnclosing( MethodDescriptor md ) { mdEnclosing = md; }
353   public MethodDescriptor getmdEnclosing() { return mdEnclosing; }
354
355   public void setcdEnclosing( ClassDescriptor cd ) { cdEnclosing = cd; }
356   public ClassDescriptor getcdEnclosing() { return cdEnclosing; }
357
358   public void setfmBogus( FlatMethod fm ) { fmBogus = fm; }
359   public FlatMethod getfmBogus() { return fmBogus; }
360
361   public void setmdBogus( MethodDescriptor md ) { mdBogus = md; }
362   public MethodDescriptor getmdBogus() { return mdBogus; }
363
364   public String getSESEmethodName() {
365     assert cdEnclosing != null;
366     assert mdBogus != null;
367
368     return 
369       cdEnclosing.getSafeSymbol()+
370       mdBogus.getSafeSymbol()+
371       "_"+
372       mdBogus.getSafeMethodDescriptor();
373   }
374
375   public String getSESErecordName() {
376     assert cdEnclosing != null;
377     assert mdBogus != null;
378
379     return
380       "struct "+
381       cdEnclosing.getSafeSymbol()+
382       mdBogus.getSafeSymbol()+
383       "_"+
384       mdBogus.getSafeMethodDescriptor()+
385       "_SESErec";
386   }
387
388   public boolean equals( Object o ) {
389     if( o == null ) {
390       return false;
391     }
392
393     if( !(o instanceof FlatSESEEnterNode) ) {
394       return false;
395     }
396
397     FlatSESEEnterNode fsen = (FlatSESEEnterNode) o;
398     return id == fsen.id;
399   }
400
401   public int hashCode() {
402     return 31*id;
403   }
404   
405
406
407   public void setFirstDepRecField( String field ) {
408     firstDepRecField = field;
409   }
410
411   public String getFirstDepRecField() {
412     return firstDepRecField;
413   }
414
415   public void incNumDepRecs() {
416     ++numDepRecs;
417   }
418
419   public int getNumDepRecs() {
420     return numDepRecs;
421   }
422   
423   public Vector<TempDescriptor> getInVarsForDynamicCoarseConflictResolution() {
424     return inVarsForDynamicCoarseConflictResolution;
425   }
426   
427   public void addInVarForDynamicCoarseConflictResolution(TempDescriptor inVar) {
428     if (!inVarsForDynamicCoarseConflictResolution.contains(inVar))
429       inVarsForDynamicCoarseConflictResolution.add(inVar);
430   }
431   
432   public void setIsLeafSESE( boolean isLeaf ) {
433     if( isLeaf ) {
434       isLeafSESE = ISLEAF_TRUE;
435     } else {
436       isLeafSESE = ISLEAF_FALSE;
437     }
438   }
439
440   public boolean getIsLeafSESE() {
441     if( isLeafSESE == ISLEAF_UNINIT ) {
442       throw new Error( "isLeafSESE uninitialized" );
443     }
444
445     return isLeafSESE == ISLEAF_TRUE;
446   }
447
448 }