big update--bringing implementation of new analysis into focus
[IRC.git] / Robust / src / Analysis / Disjoint / AllocSite.java
1 package Analysis.Disjoint;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6
7 // allocation sites are independent of any particular
8 // reachability graph, unlike most of the other elements
9 // of the reachability analysis.  An allocation site is
10 // simply a collection of heap region identifiers that
11 // are associated with a single allocation site in the
12 // program under analysis.
13
14 // So two different reachability graphs may incorporate
15 // nodes that represent the memory from one allocation
16 // site.  In this case there are two different sets of
17 // HeapRegionNode objects, but they have the same
18 // node identifiers, and there is one AllocSite
19 // object associated with the FlatNew node that gives
20 // the graphs the identifiers in question.
21
22 // note that an allocsite extends Canonical because they
23 // are Canonical, but specifically so an AllocSite can
24 // be an operand to a CanonicalOp
25
26 public class AllocSite extends Canonical {
27
28   static protected int uniqueIDcount = 0;
29
30   public static final int AGE_notInThisSite = 100;
31   public static final int AGE_in_I          = 101;
32   public static final int AGE_oldest        = 102;
33   public static final int AGE_summary       = 103;
34   public static final int AGE_siteSummary   = 200;
35
36   public static final int SHADOWAGE_notInThisSite = -100;
37   public static final int SHADOWAGE_in_I          = -101;
38   public static final int SHADOWAGE_oldest        = -102;
39   public static final int SHADOWAGE_summary       = -103;
40   public static final int SHADOWAGE_siteSummary   = -200;
41
42   protected Integer         id;
43   protected int             allocationDepth;
44   protected Vector<Integer> ithOldest;
45   protected Integer         summary;
46   protected Integer         siteSummary;
47   protected FlatNew         flatNew;
48   protected String          disjointId;
49   protected boolean         flag;
50
51
52   public AllocSite( int     allocationDepth, 
53                     FlatNew flatNew, 
54                     String  disjointId
55                     ) {
56
57     assert allocationDepth >= 1;
58
59     this.allocationDepth = allocationDepth;
60     this.flatNew         = flatNew;
61     this.disjointId      = disjointId;
62     this.flag            = false;
63
64     ithOldest = new Vector<Integer>( allocationDepth );
65     id        = generateUniqueAllocSiteID();
66   }
67
68   static public Integer generateUniqueAllocSiteID() {
69     ++uniqueIDcount;
70     return new Integer( uniqueIDcount );
71   }
72
73   public String getDisjointAnalysisId() {
74     return disjointId;
75   }
76
77
78   public int getAllocationDepth() {
79     return allocationDepth;
80   }
81
82   public void setIthOldest( int i, Integer id ) {
83     assert i  >= 0;
84     assert i  <  allocationDepth;
85     assert id != null;
86
87     ithOldest.add( i, id );
88   }
89
90   public Integer getIthOldest( int i ) {
91     assert i >= 0;
92     assert i <  allocationDepth;
93
94     return ithOldest.get( i );
95   }
96
97   public Integer getIthOldestShadow( int i ) {
98     assert i >= 0;
99     assert i <  allocationDepth;
100
101     return -ithOldest.get( i );
102   }
103
104   public Integer getOldest() {
105     return ithOldest.get( allocationDepth - 1 );
106   }
107
108   public Integer getOldestShadow() {
109     return -ithOldest.get( allocationDepth - 1 );
110   }
111
112   public void setSummary( Integer id ) {
113     assert id != null;
114     summary = id;
115   }
116
117   public Integer getSummary() {
118     return summary;
119   }
120
121   public Integer getSummaryShadow() {
122     return -summary;
123   }
124
125   public void setSiteSummary( Integer id ) {
126     assert id != null;
127     siteSummary = id;
128   }
129
130   public FlatNew getFlatNew() {
131     return flatNew;
132   }
133
134   public TypeDescriptor getType() {
135     return flatNew.getType();
136   }
137
138   public int getAgeCategory( Integer id ) {
139
140     if( id.equals( summary ) ) {
141       return AGE_summary;
142     }
143
144     if( id.equals( getOldest() ) ) {
145       return AGE_oldest;
146     }
147
148     for( int i = 0; i < allocationDepth - 1; ++i ) {
149       if( id.equals( ithOldest.get( i ) ) ) {
150         return AGE_in_I;
151       }
152     }
153
154     return AGE_notInThisSite;
155   }
156
157   public Integer getAge( Integer id ) {
158     for( int i = 0; i < allocationDepth - 1; ++i ) {
159       if( id.equals( ithOldest.get( i ) ) ) {
160         return new Integer( i );
161       }
162     }
163
164     return null;
165   }
166
167   public int getShadowAgeCategory( Integer id ) {
168     if( id.equals( -summary ) ) {
169       return SHADOWAGE_summary;
170     }
171
172     if( id.equals( getOldestShadow() ) ) {
173       return SHADOWAGE_oldest;
174     }
175
176     for( int i = 0; i < allocationDepth - 1; ++i ) {
177       if( id.equals( getIthOldestShadow( i ) ) ) {
178         return SHADOWAGE_in_I;
179       }
180     }
181
182     return SHADOWAGE_notInThisSite;
183   }
184
185   public Integer getShadowAge( Integer id ) {
186     for( int i = 0; i < allocationDepth - 1; ++i ) {
187       if( id.equals( getIthOldestShadow( i ) ) ) {
188         return new Integer( -i );
189       }
190     }
191
192     return null;
193   }
194
195
196   public String toString() {
197     if( disjointId == null ) {
198       return "allocSite"+id;
199     }
200     return "allocSite "+disjointId+" ("+id+")";
201   }
202
203   public String toStringVerbose() {
204     if( disjointId == null ) {
205       return "allocSite"+id+" "+
206         flatNew.getType().toPrettyString();
207     }
208     return "allocSite "+disjointId+" ("+id+") "+
209       flatNew.getType().toPrettyString();
210   }
211
212   public String toStringForDOT() {
213     if( disjointId != null ) {
214       return "disjoint "+disjointId+"\\n"+toString()+
215         "\\n"+getType().toPrettyString();
216     } else {
217       return                              toString()+
218         "\\n"+getType().toPrettyString();
219     }
220   }
221
222   public String toStringWithIDs() {
223     String s = "allocSite ";
224     for( int i = 0; i < ithOldest.size(); ++i ) {
225       s += i+"("+ithOldest.get( i )+") ";
226     }
227     s += "summary("+summary+")";
228     return s;
229   }
230
231
232   public int hashCodeSpecific() {
233     return id;
234   }
235   
236   public void setFlag( boolean flag ) {
237     this.flag = flag;
238   }
239   
240   public boolean getFlag() {
241     return flag;
242   }
243 }