KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > solver > OnFlyCallGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002,2003 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.jimple.spark.solver;
21 import soot.jimple.*;
22 import soot.jimple.spark.*;
23 import soot.jimple.spark.sets.*;
24 import soot.jimple.spark.pag.*;
25 import soot.jimple.spark.builder.*;
26 import soot.jimple.toolkits.callgraph.*;
27 import soot.*;
28 import java.util.*;
29 import soot.util.*;
30 import soot.util.queue.*;
31 import soot.options.SparkOptions;
32 import soot.options.CGOptions;
33 import soot.toolkits.scalar.Pair;
34
35
36 /** The interface between the pointer analysis engine and the on-the-fly
37  * call graph builder.
38  * @author Ondrej Lhotak
39  */

40
41 public class OnFlyCallGraph {
42     private OnFlyCallGraphBuilder ofcgb;
43     private ReachableMethods reachableMethods;
44     private QueueReader reachablesReader;
45     private QueueReader callEdges;
46     private CallGraph callGraph;
47
48     public ReachableMethods reachableMethods() { return reachableMethods; }
49     public CallGraph callGraph() { return callGraph; }
50
51     public OnFlyCallGraph( PAG pag ) {
52         this.pag = pag;
53         CGOptions options = new CGOptions( PhaseOptions.v().getPhaseOptions("cg") );
54         if( options.all_reachable() ) {
55             List entryPoints = new ArrayList();
56             entryPoints.addAll( EntryPoints.v().all() );
57             entryPoints.addAll( EntryPoints.v().methodsOfApplicationClasses() );
58             Scene.v().setEntryPoints( entryPoints );
59         }
60         callGraph = new CallGraph();
61         Scene.v().setCallGraph( callGraph );
62         ContextManager cm = CallGraphBuilder.makeContextManager(callGraph);
63         reachableMethods = Scene.v().getReachableMethods();
64         ofcgb = new OnFlyCallGraphBuilder( cm, reachableMethods );
65         reachablesReader = reachableMethods.listener();
66         callEdges = cm.callGraph().listener();
67     }
68     public void build() {
69         ofcgb.processReachables();
70         processReachables();
71         processCallEdges();
72     }
73     private void processReachables() {
74         reachableMethods.update();
75         while(reachablesReader.hasNext()) {
76             MethodOrMethodContext m = (MethodOrMethodContext) reachablesReader.next();
77             MethodPAG mpag = MethodPAG.v( pag, m.method() );
78             mpag.build();
79             mpag.addToPAG(m.context());
80         }
81     }
82     private void processCallEdges() {
83         Stmt s = null;
84         while(callEdges.hasNext()) {
85             Edge e = (Edge) callEdges.next();
86             MethodPAG amp = MethodPAG.v( pag, e.tgt() );
87             amp.build();
88             amp.addToPAG( e.tgtCtxt() );
89             pag.addCallTarget( e );
90         }
91     }
92
93     public OnFlyCallGraphBuilder ofcgb() { return ofcgb; }
94
95     public void updatedNode( VarNode vn ) {
96         Object JavaDoc r = vn.getVariable();
97         if( !(r instanceof Local) ) return;
98         final Local receiver = (Local) r;
99         final Context context = vn.context();
100
101         PointsToSetInternal p2set = vn.getP2Set().getNewSet();
102         if( ofcgb.wantTypes( receiver ) ) {
103             p2set.forall( new P2SetVisitor() {
104             public final void visit( Node n ) {
105                 ofcgb.addType( receiver, context, n.getType(), (AllocNode) n );
106             }} );
107         }
108         if( ofcgb.wantStringConstants( receiver ) ) {
109             p2set.forall( new P2SetVisitor() {
110             public final void visit( Node n ) {
111                 if( n instanceof StringConstantNode ) {
112                     String JavaDoc constant = ((StringConstantNode)n).getString();
113                     ofcgb.addStringConstant( receiver, context, constant );
114                 } else {
115                     ofcgb.addStringConstant( receiver, context, null );
116                 }
117             }} );
118         }
119     }
120
121     /** Node uses this to notify PAG that n2 has been merged into n1. */
122     public void mergedWith( Node n1, Node n2 ) {
123     }
124
125     /* End of public methods. */
126     /* End of package methods. */
127
128     private PAG pag;
129 }
130
131
132
133
Popular Tags