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