cb69890cadd700c1f822407cd94079973bb5ab2e
[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     fn2state = new HashMap<FlatNode, DefiniteReachState>();
19   }
20
21
22   public void methodEntry( FlatNode fn, 
23                            Set<TempDescriptor> parameters ) {
24     DefiniteReachState state = get( fn );
25     state.methodEntry( parameters );
26     fn2state.put( fn, state );
27   }
28
29   public void copy( FlatNode fn, 
30                     TempDescriptor x,
31                     TempDescriptor y ) {
32     DefiniteReachState state = makeIn( fn );
33     state.copy( x, y );
34     fn2state.put( fn, state ); 
35   }
36
37   public void load( FlatNode fn, 
38                     TempDescriptor  x,
39                     TempDescriptor  y,
40                     FieldDescriptor f ) {
41     DefiniteReachState state = makeIn( fn );
42     state.load( x, y, f );
43     fn2state.put( fn, state ); 
44   }
45
46   public void store( FlatNode fn, 
47                      TempDescriptor  x,
48                      FieldDescriptor f,
49                      TempDescriptor  y ) {
50     DefiniteReachState state = makeIn( fn );
51     state.store( x, f, y );
52     fn2state.put( fn, state ); 
53   }
54
55   public void newObject( FlatNode fn, 
56                          TempDescriptor x ) {
57     DefiniteReachState state = makeIn( fn );
58     state.newObject( x );
59     fn2state.put( fn, state ); 
60   }
61
62   // x is the return value, x = foo(...);
63   public void methodCall( FlatNode fn, 
64                           TempDescriptor x ) {
65     DefiniteReachState state = makeIn( fn );
66     state.methodCall( x );
67     fn2state.put( fn, state ); 
68   }
69
70
71   public void writeState( FlatNode fn, String outputName ) {
72     DefiniteReachState state = makeIn( fn );
73     try {
74       BufferedWriter bw = new BufferedWriter( new FileWriter( outputName+".txt" ) );
75       bw.write( state.toString() );
76       bw.close();
77     } catch( IOException e ) {
78       System.out.println( "ERROR writing definite reachability state:\n  "+e );
79     }
80   }
81
82
83   // get the current state for just after the given
84   // program point
85   private DefiniteReachState get( FlatNode fn ) {
86     DefiniteReachState state = fn2state.get( fn );
87     if( state == null ) {
88       state = new DefiniteReachState();
89       fn2state.put( fn, state );
90     }
91     return state;
92   }
93
94   // get the current state for the program point just
95   // before the given program point by merging the out
96   // states of the predecessor statements
97   private DefiniteReachState makeIn( FlatNode fn ) {
98     DefiniteReachState stateIn = new DefiniteReachState();
99     for( int i = 0; i < fn.numPrev(); ++i ) {
100       stateIn.merge( get( fn.getPrev( i ) ) );
101     }
102     return stateIn;
103   }
104 }