Changed a lot of things in RCR. It works for the most part (manually inspected with...
[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
80     ++stateTo.refCount;
81   }
82
83
84   public int getID() {
85     return id;
86   }
87
88   // once you get your hands on an SMFEState in the
89   // RuntimeConflictResolver side of things, this is how you
90   // find out what effects are possible in this state
91   public Set<Effect> getEffectsAllowed() {
92     return effects;
93   }
94   
95   public Set<Effect> getConflicts() {
96     //TODO JIM! Fix this when have a chance!
97     conflicts.addAll(effects);
98     return this.conflicts;
99   }
100   
101   public Set<Effect> getTransistionEffects() {
102     return this.e2states.keySet();
103   }
104
105   // some subset of the above effects may transition to
106   // other states
107   public Set<SMFEState> transitionsTo( Effect e ) {
108     Set<SMFEState> statesOut = e2states.get( e );
109     if( statesOut == null ) {
110       statesOut = new HashSet<SMFEState>();
111     }
112     return statesOut;
113   }
114
115   public int getRefCount() {
116     return refCount;
117   }
118
119
120   public boolean equals( Object o ) {
121     if( o == null ) {
122       return false;
123     }
124
125     if( !(o instanceof SMFEState) ) {
126       return false;
127     }
128
129     SMFEState state = (SMFEState) o;
130
131     return id == state.id;
132   }
133
134   public int hashCode() {
135     return iHashCode;
136   }
137
138
139   public String toStringDOT() {
140     
141     // first create the state as a node in DOT graph
142     String s = "  "+id+"[shape=box,label=\"";
143
144     if( effects.size() == 1 ) {
145       s += effects.iterator().next().toString();
146
147     } else if( effects.size() > 1 ) {
148
149       Iterator<Effect> eItr = effects.iterator();
150       while( eItr.hasNext() ) {
151         Effect e = eItr.next();
152         s += e.toString();
153
154         if( eItr.hasNext() ) {
155           s += "\\n";
156         }
157       }
158     }
159
160     s += "\"];";
161
162     // then each transition is an edge
163     Iterator<Effect> eItr = e2states.keySet().iterator();
164     while( eItr.hasNext() ) {
165       Effect         e      = eItr.next();
166       Set<SMFEState> states = e2states.get( e );
167
168       Iterator<SMFEState> sItr = states.iterator();
169       while( sItr.hasNext() ) {
170         SMFEState state = sItr.next();
171
172         s += "\n  "+
173           id+" -> "+state.id+
174           "[label=\""+e+"\"];";
175       }
176     }
177
178     return s;
179   }
180
181 }