d7e404f21ae8f18bdc4d6c88f75c48908230127c
[IRC.git] / Robust / src / Analysis / Disjoint / BuildStateMachines.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 //  BuildStateMachines builds a state machine
12 //  for every task/stall site and variable pair
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 BuildStateMachines {
22
23   // map a task or stall site (both a FlatNode) to a variable
24   // and then finally to a state machine
25   protected 
26     Hashtable< FlatNode, Hashtable<TempDescriptor, StateMachineForEffects> >
27     fn2var2smfe;
28
29   public BuildStateMachines() {
30     fn2var2smfe = new
31       Hashtable< FlatNode, Hashtable<TempDescriptor, StateMachineForEffects> >();
32   }
33
34   protected StateMachineForEffects getStateMachine( FlatNode       fn,
35                                                     TempDescriptor var ) {
36
37     Hashtable<TempDescriptor, StateMachineForEffects> var2smfe = fn2var2smfe.get( fn );
38     if( var2smfe == null ) {
39       var2smfe = new Hashtable<TempDescriptor, StateMachineForEffects>();
40       fn2var2smfe.put( fn, var2smfe );
41     }
42     
43     StateMachineForEffects smfe = var2smfe.get( var );
44     if( smfe == null ) {
45       smfe = new StateMachineForEffects( fn );
46       var2smfe.put( var, smfe );
47     }
48
49     return smfe;
50   }
51
52
53
54   public void addToStateMachine( Taint t, 
55                                  Effect e, 
56                                  FlatNode currentProgramPoint ) {
57     
58     FlatNode taskOrStallSite;
59     if( t.isStallSiteTaint() ) {
60       taskOrStallSite = t.getStallSite();
61     } else {
62       taskOrStallSite = t.getSESE();
63     }
64
65     TempDescriptor var = t.getVar();
66
67     StateMachineForEffects smfe = getStateMachine( taskOrStallSite, var );
68
69     FlatNode whereDefined = t.getWhereDefined();
70
71     smfe.addEffect( whereDefined, e );
72
73     // reads of pointers make a transition
74     if( e.getType() == Effect.read &&
75         e.getField().getType().isPtr() ) {
76       
77       smfe.addTransition( whereDefined, 
78                           currentProgramPoint,
79                           e );
80     }
81   }
82
83
84   public void writeStateMachines() {
85
86     Iterator<FlatNode> fnItr = fn2var2smfe.keySet().iterator();
87     while( fnItr.hasNext() ) {
88       FlatNode fn = fnItr.next();
89       
90       Hashtable<TempDescriptor, StateMachineForEffects> 
91         var2smfe = fn2var2smfe.get( fn );
92         
93       Iterator<TempDescriptor> varItr = var2smfe.keySet().iterator();
94       while( varItr.hasNext() ) {
95         TempDescriptor var = varItr.next();
96
97         StateMachineForEffects smfe = var2smfe.get( var );
98
99         smfe.writeAsDOT( "statemachine_"+fn.toString()+var.toString() );
100       }
101     }
102   }
103 }