going to start with just enough definite reach analysis to do a simple example
[IRC.git] / Robust / src / Analysis / Disjoint / DefiniteReachAnalysis.java
1 package Analysis.Disjoint;
2
3 import java.io.*;
4 import java.util.*;
5
6 import IR.*;
7 import IR.Flat.*;
8 import Util.*;
9
10
11 public class DefiniteReachAnalysis {
12   
13   // keep a state of definite reachability analysis for
14   // every program point
15   private Map<FlatNode, DefiniteReachState> fn2state;
16   
17   public DefiniteReachAnalysis() {
18     // a class-wide initialization
19     DefiniteReachState.initBuilders();
20
21     fn2state = new HashMap<FlatNode, DefiniteReachState>();
22   }
23
24
25   public void methodEntry( FlatNode fn, 
26                            Set<TempDescriptor> parameters ) {
27     DefiniteReachState state = get( fn );
28     state.methodEntry( parameters );
29     fn2state.put( fn, state );
30   }
31
32   public void copy( FlatNode fn, 
33                     TempDescriptor x,
34                     TempDescriptor y ) {
35     DefiniteReachState state = makeIn( fn );
36     state.copy( x, y );
37     fn2state.put( fn, state ); 
38   }
39
40   public void load( FlatNode fn, 
41                     TempDescriptor  x,
42                     TempDescriptor  y,
43                     FieldDescriptor f ) {
44     DefiniteReachState state = makeIn( fn );
45     state.load( x, y, f );
46     fn2state.put( fn, state ); 
47   }
48
49   public void store( FlatNode fn, 
50                      TempDescriptor  x,
51                      FieldDescriptor f,
52                      TempDescriptor  y,
53                     Set<EdgeKey> edgeKeysRemoved ) {
54     DefiniteReachState state = makeIn( fn );
55     state.store( x, f, y, edgeKeysRemoved );
56     fn2state.put( fn, state ); 
57   }
58
59   public void newObject( FlatNode fn, 
60                          TempDescriptor x ) {
61     DefiniteReachState state = makeIn( fn );
62     state.newObject( x );
63     fn2state.put( fn, state ); 
64   }
65
66   public void methodCall( FlatNode fn, 
67                           TempDescriptor retVal ) {
68     DefiniteReachState state = makeIn( fn );
69     state.methodCall( retVal );
70     fn2state.put( fn, state ); 
71   }
72
73
74
75
76   public void writeState( FlatNode fn, String outputName ) {
77     DefiniteReachState state = makeIn( fn );
78     try {
79       BufferedWriter bw = new BufferedWriter( new FileWriter( outputName+".txt" ) );
80       bw.write( state.toString() );
81       bw.close();
82     } catch( IOException e ) {
83       System.out.println( "ERROR writing definite reachability state:\n  "+e );
84     }
85   }
86
87
88   // get the current state for just after the given
89   // program point
90   private DefiniteReachState get( FlatNode fn ) {
91     DefiniteReachState state = fn2state.get( fn );
92     if( state == null ) {
93       state = new DefiniteReachState();
94       fn2state.put( fn, state );
95     }
96     return state;
97   }
98
99   // get the current state for the program point just
100   // before the given program point by merging the out
101   // states of the predecessor statements
102   private DefiniteReachState makeIn( FlatNode fn ) {
103     DefiniteReachState stateIn = new DefiniteReachState();
104     for( int i = 0; i < fn.numPrev(); ++i ) {
105       stateIn.merge( get( fn.getPrev( i ) ) );
106     }
107     return stateIn;
108   }
109 }