simplify ExecutionGraph code
[IRC.git] / Robust / src / Analysis / TaskStateAnalysis / FlagState.java
1 package Analysis.TaskStateAnalysis;
2 import Analysis.TaskStateAnalysis.*;
3 import IR.*;
4 import IR.Tree.*;
5 import IR.Flat.*;
6 import java.util.*;
7 import java.io.*;
8 import Util.GraphNode;
9
10 /** This class is used to hold the flag states that a class in the Bristlecone 
11  *  program can exist in, during runtime.
12  */
13 public class FlagState extends GraphNode {
14     public static final int ONETAG=1;
15     public static final int NOTAGS=0;
16     public static final int MULTITAGS=-1;
17     
18     private int uid;
19     private static int nodeid=0;
20
21     private final HashSet flagstate;
22     private final ClassDescriptor cd;
23     private final Hashtable<TagDescriptor,Integer> tags;
24     private boolean issourcenode;
25     private Vector tasks;
26
27     /** Class constructor
28      *  Creates a new flagstate with all flags set to false.
29      *  @param cd ClassDescriptor
30      */
31     public FlagState(ClassDescriptor cd) {
32         this.flagstate=new HashSet();
33         this.cd=cd;
34         this.tags=new Hashtable<TagDescriptor,Integer>();
35         this.uid=FlagState.nodeid++;
36         this.issourcenode=false;
37     }
38
39     /** Class constructor
40      *  Creates a new flagstate with flags set according to the HashSet.
41      *  If the flag exists in the hashset, it's set to true else set to false.
42      *  @param cd ClassDescriptor
43      *  @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
44      */
45     private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
46         this.flagstate=flagstate;
47         this.cd=cd;
48         this.tags=tags;
49         this.uid=FlagState.nodeid++;
50         this.issourcenode=false;
51         
52     }
53    
54     public int getuid() {
55         return uid;
56     }
57
58     /** Accessor method
59       *  @param fd FlagDescriptor
60       *  @return true if the flagstate contains fd else false.
61       */
62     public boolean get(FlagDescriptor fd) {
63         return flagstate.contains(fd);
64     }
65     
66     /** Checks if the flagstate is a source node. 
67      *  @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
68      */
69       
70     public boolean isSourceNode(){
71             return issourcenode;
72         }
73         
74     /**  Sets the flagstate as a source node. 
75      */
76     public void setAsSourceNode(){
77         if(!issourcenode){
78             issourcenode=true;
79             this.tasks=new Vector();
80         }
81     }
82     
83     public void addAllocatingTask(TaskDescriptor task){
84         tasks.add(task);
85     }
86     
87     public Vector getAllocatingTasks(){
88         return tasks;
89     }
90     
91     
92     public String toString() {
93         return cd.toString()+getTextLabel();
94     }
95
96     /** @return Iterator over the flags in the flagstate.
97      */
98      
99     public Iterator getFlags() {
100         return flagstate.iterator();
101     }
102
103     public int numFlags(){
104         return flagstate.size();
105     }
106     
107     public FlagState[] setTag(TagDescriptor tag){
108         HashSet newset1=(HashSet)flagstate.clone();
109         Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
110             
111         if (tags.containsKey(tag)){
112             //Code could try to remove flag that doesn't exist
113             
114             switch (tags.get(tag).intValue()){
115             case ONETAG:
116                 newtags1.put(tag,new Integer(MULTITAGS));
117                 return new FlagState[] {this, new FlagState(newset1, cd, newtags1)};
118             case MULTITAGS:
119                 return new FlagState[] {this};
120             default:
121                 throw new Error();
122             }
123         } else {
124             newtags1.put(tag,new Integer(ONETAG));
125             return new FlagState[] {new FlagState(newset1,cd,newtags1)};
126         }
127     }
128
129     public int getTagCount(String tagtype){
130         for (Enumeration en=getTags();en.hasMoreElements();){
131             TagDescriptor td=(TagDescriptor)en.nextElement();
132             if (tagtype.equals(td.getSymbol()))
133                 return tags.get(td).intValue();   //returns either ONETAG or MULTITAG
134         }
135         return NOTAGS;
136     }
137     
138     public FlagState[] clearTag(TagDescriptor tag){
139         if (tags.containsKey(tag)){
140             switch(tags.get(tag).intValue()){
141             case ONETAG:
142                 HashSet newset=(HashSet)flagstate.clone();
143                 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
144                 newtags.remove(tag);
145                 return new FlagState[]{new FlagState(newset,cd,newtags)};
146                 
147             case MULTITAGS:
148                 //two possibilities - count remains 2 or becomes 1
149                 //2 case
150                 HashSet newset1=(HashSet)flagstate.clone();
151                 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
152
153                 //1 case
154                 HashSet newset2=(HashSet)flagstate.clone();
155                 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
156                 newtags1.put(tag,new Integer(ONETAG));
157                 return new FlagState[] {new FlagState(newset1, cd, newtags2),
158                                         new FlagState(newset2, cd, newtags2)};
159             default:
160                 throw new Error();
161             }
162         } else {
163             throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
164         }
165     }
166     
167     /** Creates a string description of the flagstate
168      *  e.g.  a flagstate with five flags could look like 01001
169      *  @param flags an array of flagdescriptors.
170      *  @return string representation of the flagstate.
171      */
172         public String toString(FlagDescriptor[] flags)
173         {
174                 StringBuffer sb = new StringBuffer(flagstate.size());
175                 for(int i=0;i < flags.length; i++)
176                 {
177                         if (get(flags[i]))
178                                 sb.append(1);
179                         else
180                                 sb.append(0);
181                 }
182                         
183                 return new String(sb);
184         }
185
186         /** Accessor method
187          *  @return returns the classdescriptor of the flagstate.
188          */
189          
190     public ClassDescriptor getClassDescriptor(){
191         return cd;
192     }
193
194         /** Sets the status of a specific flag in a flagstate after cloning it.
195          *  @param      fd FlagDescriptor of the flag whose status is being set.
196          *  @param  status boolean value
197          *  @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
198          */
199          
200     public FlagState setFlag(FlagDescriptor fd, boolean status) {
201         HashSet newset=(HashSet) flagstate.clone();
202         Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
203         if (status)
204             newset.add(fd);
205         else if (newset.contains(fd)){
206             newset.remove(fd);
207         }
208         
209         return new FlagState(newset, cd, newtags);
210     }
211     
212     /** Tests for equality of two flagstate objects.
213     */
214     
215     public boolean equals(Object o) {
216         if (o instanceof FlagState) {
217             FlagState fs=(FlagState)o;
218             if (fs.cd!=cd)
219                 return false;
220             return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
221         }
222         return false;
223     }
224
225     public int hashCode() {
226         return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
227     }
228
229     public String getLabel() {
230         return "N"+uid;
231     }
232     
233     
234         
235
236     public String getTextLabel() {
237         String label=null;
238         for(Iterator it=getFlags();it.hasNext();) {
239             FlagDescriptor fd=(FlagDescriptor) it.next();
240             if (label==null)
241                 label=fd.toString();
242             else
243                 label+=", "+fd.toString();
244         }
245         for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
246                 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
247                 switch (tags.get(td).intValue()){
248                 case ONETAG:
249                     if (label==null)
250                         label=td.toString()+"(1)";
251                     else
252                         label+=", "+td.toString()+"(1)";
253                     break;
254                 case MULTITAGS:
255                     if (label==null)
256                         label=td.toString()+"(n)";
257                     else
258                         label+=", "+td.toString()+"(n)";
259                     break;
260                 default:
261                     break;
262                 }
263         }
264         if (label==null)
265             return " ";
266         return label;
267     }
268     
269     public Enumeration getTags(){
270             return tags.keys();
271     }
272 }