dbb793dab506d28e8a61c461784472bc96daf8ed
[IRC.git] / Robust / src / Analysis / Disjoint / SMFEState.java
1 package Analysis.Disjoint;
2
3 import java.util.*;
4 import java.io.*;
5
6 import IR.*;
7 import IR.Flat.*;
8
9 //////////////////////////////////////////////
10 //
11 //  SMFEState is part of a 
12 //  (S)tate (M)achine (F)or (E)ffects.
13 //
14 //  StateMachineForEffects describes an intial
15 //  state and the effect transtions a DFJ
16 //  traverser should make from the current state
17 //  when searching for possible runtime conflicts.
18 //
19 //////////////////////////////////////////////
20
21 public class SMFEState {
22
23   //  #####################
24   //  ## NOTE NOTE NOTE!!!!
25   //  #####################
26   //  When every state corresponds to exactly one
27   //  FlatNode (whereDefined attribute) then we can
28   //  use the FlatNode's id as an ID.  BUT BUT BUT, if
29   //  we merge nodes together in the future for
30   //  optimizations and whatnot, we need an alternate
31   //  system of unique IDs
32
33   // uniquely identifies this state  
34   protected int id;
35   protected int iHashCode;
36
37   // all possible effects in this state
38   protected Set<Effect> effects;
39   
40   //TODO Jim! get me the list of conflicts!
41   protected Set<Effect> conflicts;
42
43   // the given effect allows a transition to a
44   // set of new states
45   protected Hashtable< Effect, Set<SMFEState> > e2states;
46
47   // useful for knowing when a state can be inlined during
48   // code gen
49   protected int refCount;
50
51
52   
53   public SMFEState( FlatNode fnWhereDefined ) {
54
55     this.id         = fnWhereDefined.nodeid;
56     this.iHashCode  = fnWhereDefined.hashCode();
57
58     effects         = new HashSet<Effect>();
59     conflicts       = new HashSet<Effect>();
60     e2states        = new Hashtable< Effect, Set<SMFEState> >();
61     refCount        = 0;
62   }
63
64   public void addEffect( Effect e ) {
65     effects.add( e );
66   }
67
68   // the given effect allows the transition to the new state
69   public void addTransition( Effect    effect,
70                              SMFEState stateTo
71                              ) {
72
73     Set<SMFEState> states = e2states.get( effect );
74     if( states == null ) {
75       states = new HashSet<SMFEState>();
76       e2states.put( effect, states );
77     }
78     states.add( stateTo );
79     stateTo.refCount++;
80   }
81
82
83   public int getID() {
84     return id;
85   }
86
87   // once you get your hands on an SMFEState in the
88   // RuntimeConflictResolver side of things, this is how you
89   // find out what effects are possible in this state
90   public Set<Effect> getEffectsAllowed() {
91     return effects;
92   }
93   
94   public void addConflict(Effect e) {
95     conflicts.add(e);
96   }
97
98   public Set<Effect> getConflicts() {
99     return conflicts;
100   }
101   
102   public Set<Effect> getTransistionEffects() {
103     return this.e2states.keySet();
104   }
105
106   // some subset of the above effects may transition to
107   // other states
108   public Set<SMFEState> transitionsTo( Effect e ) {
109     Set<SMFEState> statesOut = e2states.get( e );
110     if( statesOut == null ) {
111       statesOut = new HashSet<SMFEState>();
112     }
113     return statesOut;
114   }
115
116   // some subset of the above effects may transition to
117   // other states
118   public Set<SMFEState> transitionsTo() {
119     Set<SMFEState> statesOut = new HashSet<SMFEState>();
120     for(Map.Entry<Effect, Set<SMFEState>> entry:e2states.entrySet()) {
121       statesOut.addAll(entry.getValue());
122     }
123     return statesOut;
124   }
125
126   public int getRefCount() {
127     return refCount;
128   }
129
130
131   public boolean equals( Object o ) {
132     if( o == null ) {
133       return false;
134     }
135
136     if( !(o instanceof SMFEState) ) {
137       return false;
138     }
139
140     SMFEState state = (SMFEState) o;
141
142     return id == state.id;
143   }
144
145   public int hashCode() {
146     return iHashCode;
147   }
148
149
150   public String toStringDOT() {
151     
152     // first create the state as a node in DOT graph
153     String s = "  "+id+"[shape=box,label=\"";
154
155     if( effects.size() == 1 ) {
156       s += effects.iterator().next().toString();
157
158     } else if( effects.size() > 1 ) {
159
160       Iterator<Effect> eItr = effects.iterator();
161       while( eItr.hasNext() ) {
162         Effect e = eItr.next();
163         s += e.toString();
164
165         if( eItr.hasNext() ) {
166           s += "\\n";
167         }
168       }
169     }
170
171     s += "\"];";
172
173     // then each transition is an edge
174     Iterator<Effect> eItr = e2states.keySet().iterator();
175     while( eItr.hasNext() ) {
176       Effect         e      = eItr.next();
177       Set<SMFEState> states = e2states.get( e );
178
179       Iterator<SMFEState> sItr = states.iterator();
180       while( sItr.hasNext() ) {
181         SMFEState state = sItr.next();
182
183         s += "\n  "+
184           id+" -> "+state.id+
185           "[label=\""+e+"\"];";
186       }
187     }
188
189     return s;
190   }
191
192 }