Incrementing on definite reach analysis
[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     DefiniteReachState state = makeIn( fn );
54     state.store( x, f, y );
55     fn2state.put( fn, state ); 
56   }
57
58   public void newObject( FlatNode fn, 
59                          TempDescriptor x ) {
60     DefiniteReachState state = makeIn( fn );
61     state.newObject( x );
62     fn2state.put( fn, state ); 
63   }
64
65   // x is the return value, x = foo(...);
66   public void methodCall( FlatNode fn, 
67                           TempDescriptor x ) {
68     DefiniteReachState state = makeIn( fn );
69     state.methodCall( x );
70     fn2state.put( fn, state ); 
71   }
72
73
74   public void writeState( FlatNode fn, String outputName ) {
75     DefiniteReachState state = makeIn( fn );
76     try {
77       BufferedWriter bw = new BufferedWriter( new FileWriter( outputName+".txt" ) );
78       bw.write( state.toString() );
79       bw.close();
80     } catch( IOException e ) {
81       System.out.println( "ERROR writing definite reachability state:\n  "+e );
82     }
83   }
84
85
86   // get the current state for just after the given
87   // program point
88   private DefiniteReachState get( FlatNode fn ) {
89     DefiniteReachState state = fn2state.get( fn );
90     if( state == null ) {
91       state = new DefiniteReachState();
92       fn2state.put( fn, state );
93     }
94     return state;
95   }
96
97   // get the current state for the program point just
98   // before the given program point by merging the out
99   // states of the predecessor statements
100   private DefiniteReachState makeIn( FlatNode fn ) {
101     DefiniteReachState stateIn = new DefiniteReachState();
102     for( int i = 0; i < fn.numPrev(); ++i ) {
103       stateIn.merge( get( fn.getPrev( i ) ) );
104     }
105     return stateIn;
106   }
107 }