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