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