try to get runtimeconflictresolver working...and cleaning up the code...
[IRC.git] / Robust / src / Analysis / Disjoint / StateMachineForEffects.java
1 package Analysis.Disjoint;
2
3 import java.util.*;
4 import java.io.*;
5
6 import IR.*;
7 import IR.Flat.*;
8 import Util.Pair;
9
10 //////////////////////////////////////////////
11 //
12 //  StateMachineForEffects describes an intial
13 //  state and the effect transtions a DFJ
14 //  traverser should make from the current state
15 //  when searching for possible runtime conflicts.
16 //
17 //////////////////////////////////////////////
18
19 public class StateMachineForEffects {
20   public final static FlatNode startNode=new FlatNop();
21   protected HashMap<Pair<Alloc, FieldDescriptor>, Integer> effectsMap;
22
23   // states in the machine are uniquely identified 
24   // by a flat node (program point)
25   protected Hashtable<FlatNode, SMFEState> fn2state;
26   
27   //TODO Jim! Jim! Give me the weakly connected group number here!
28   protected Hashtable<FlatNode, Integer> fn2weaklyConnectedGroupID;
29
30   protected SMFEState initialState;
31   protected FlatNode fn;
32   protected int id=0;
33
34   public StateMachineForEffects( FlatNode fnInitial ) {
35     fn2state = new Hashtable<FlatNode, SMFEState>();
36     effectsMap = new HashMap<Pair<Alloc, FieldDescriptor>, Integer>();
37     initialState = getState( startNode );
38     this.fn=fnInitial;
39   }
40
41   public Set<SMFEState> getStates() {
42     HashSet<SMFEState> set=new HashSet<SMFEState>();
43     set.addAll(fn2state.values());
44     return set;
45   }
46
47   public FlatNode getStallorSESE() {
48     return fn;
49   }
50
51   public int getEffects(Alloc affectedNode, FieldDescriptor fd) {
52     Integer type=effectsMap.get(new Pair<Alloc, FieldDescriptor>(affectedNode, fd));
53     if (type==null)
54       return 0;
55     else
56       return type.intValue();
57   }
58
59   public void addEffect( FlatNode fnState,
60                          Effect e ) {
61     if (fnState==null)
62       fnState=startNode;
63     SMFEState state = getState( fnState );
64     state.addEffect( e );
65     Pair<Alloc, FieldDescriptor> p=new Pair<Alloc, FieldDescriptor>(e.getAffectedAllocSite(), e.getField());
66     int type=e.getType();
67     if (!effectsMap.containsKey(p))
68       effectsMap.put(p, new Integer(type));
69     else
70       effectsMap.put(p, new Integer(type|effectsMap.get(p).intValue()));
71   }
72
73   public void addTransition( FlatNode fnFrom,
74                              FlatNode fnTo,
75                              Effect e ) {
76     if (fnFrom==null)
77       fnFrom=startNode;
78     
79     assert fn2state.containsKey( fnFrom );
80     SMFEState stateFrom = getState( fnFrom );
81     SMFEState stateTo   = getState( fnTo );
82     
83     stateFrom.addTransition( e, stateTo );
84   }
85
86   public SMFEState getInitialState() {
87     return initialState;
88   }
89
90
91   protected SMFEState getState( FlatNode fn ) {
92     SMFEState state = fn2state.get( fn );
93     if( state == null ) {
94       state = new SMFEState( fn ,id++ );
95       fn2state.put( fn, state );
96     }
97     return state;
98   }
99   
100   public Integer getWeaklyConnectedGroupID(FlatNode fn) {
101     //TODO stubby stubby!
102     return 0;
103   }
104
105   public void writeAsDOT( String graphName ) {
106     graphName = graphName.replaceAll( "[\\W]", "" );
107
108     try {
109       BufferedWriter bw = 
110         new BufferedWriter( new FileWriter( graphName+".dot" ) );
111
112       bw.write( "digraph "+graphName+" {\n" );
113
114       Iterator<FlatNode> fnItr = fn2state.keySet().iterator();
115       while( fnItr.hasNext() ) {
116         SMFEState state = fn2state.get( fnItr.next() );
117         bw.write( state.toStringDOT()+"\n" );
118       }
119
120       bw.write( "}\n" );
121       bw.close();
122       
123     } catch( IOException e ) {
124       throw new Error( "Error writing out DOT graph "+graphName );
125     }
126   }
127
128 }