KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > graph > ClassicCompleteUnitGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 John Jorgensen
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
21 package soot.toolkits.graph;
22
23 import soot.*;
24 import soot.util.*;
25 import java.util.*;
26 import soot.options.Options;
27
28
29 /**
30  * <p> Represents a CFG for a Body instance where the nodes are
31  * {@link Unit} instances, and where edges are a conservative
32  * indication of unexceptional and exceptional control
33  * flow.</p>
34  *
35  * <p><tt>ClassicCompleteUnitGraph</tt> attempts to duplicate the
36  * results that would have been produced by Soot's
37  * <code>CompleteUnitGraph</code> in releases up to Soot 2.1.0 (the
38  * one known discrepancy is that the 2.1.0
39  * <code>CompleteUnitGraph</code> would include two edges joining one
40  * node to another
41  * {@link Unit}s if the first node both branched to and fell through to
42  * the second). It is included solely for testing purposes, and
43  * should not be used in actual analyses.</p>
44  *
45  * <p> There are two distinctions between the graphs produced by the
46  * <tt>ClassicCompleteUnitGraph</tt> and
47  * <tt>ExceptionalUnitGraph</tt>:
48  * <ol>
49  *
50  * <li><tt>ExceptionalUnitGraph</tt> only creates edges to a
51  * <tt>Trap</tt> handler for trapped <tt>Unit</tt>s that have the
52  * potential to throw the particular exception type caught by the
53  * handler, according to the {@link ThrowAnalysis} used to estimate
54  * which exceptions each {@link Unit} may throw.
55  * <tt>ClassicCompleteUnitGraph</tt> creates edges for all trapped
56  * <tt>Unit</tt>s, regardless of the types of exceptions they may
57  * throw.</li>
58  *
59  * <li> When <tt>ExceptionalUnitGraph</tt> creates edges for a
60  * trapped <tt>Unit</tt> that may throw a caught exception, it adds
61  * edges from each predecessor of the excepting <tt>Unit</tt> to the
62  * handler. If the excepting <tt>Unit</tt> itself has no potential
63  * side effects, <tt>ExceptionalUnitGraph</tt> may omit an edge from
64  * it to the handler, depending on the parameters
65  * passed to the <tt>ExceptionalUnitGraph<tt> constructor.
66  * <tt>ClassicCompleteUnitGraph</tt>, on the other hand, always adds
67  * an edge from the excepting <tt>Unit</tt> itself to the handler,
68  * and adds edges from the predecessor only of the first
69  * <tt>Unit</tt> covered by a <tt>Trap</tt> (in this one aspect
70  * <tt>ClassicCompleteUnitGraph</tt> is less conservative than
71  * <tt>ExceptionalUnitGraph</tt>, since it ignores the possibility of
72  * a branch into the middle of a protected area).</li>
73  *
74  * </ol></p>
75  */

76 public class ClassicCompleteUnitGraph extends TrapUnitGraph
77 {
78     /**
79      * Constructs the graph from a given Body instance.
80      * @param the Body instance from which the graph is built.
81      */

82     public ClassicCompleteUnitGraph(Body body)
83     {
84     // The TrapUnitGraph constructor will use our buildExceptionalEdges:
85
super(body);
86     }
87
88
89     /**
90      * Method to compute the edges corresponding to exceptional
91      * control flow.
92      *
93      * @param unitToSuccs A {@link Map} from {@link Unit}s to {@link
94      * List}s of {@link Unit}s. This is * an ``out
95      * parameter''; <tt>buildExceptionalEdges</tt>
96      * will add a mapping for every <tt>Unit</tt>
97      * within the scope of one or more {@link
98      * Trap}s to a <tt>List</tt> of the handler
99      * units of those <tt>Trap</tt>s.
100      *
101      * @param unitToPreds A {@link Map} from {@link Unit}s to
102      * {@link List}s of {@link Unit}s. This is an
103      * ``out parameter'';
104      * <tt>buildExceptionalEdges</tt> will add a
105      * mapping for every {@link Trap} handler to
106      * all the <tt>Unit</tt>s within the scope of
107      * that <tt>Trap</tt>.
108      */

109     protected void buildExceptionalEdges(Map unitToSuccs, Map unitToPreds) {
110     // First, add the same edges as TrapUnitGraph.
111
super.buildExceptionalEdges(unitToSuccs, unitToPreds);
112     // Then add edges from the predecessors of the first
113
// trapped Unit for each Trap.
114
for (Iterator trapIt = body.getTraps().iterator();
115          trapIt.hasNext(); ) {
116         Trap trap = (Trap) trapIt.next();
117         Unit firstTrapped = trap.getBeginUnit();
118         Unit catcher = trap.getHandlerUnit();
119         // Make a copy of firstTrapped's predecessors to iterate over,
120
// just in case we're about to add new predecessors to this
121
// very list, though that can only happen if the handler traps
122
// itself. And to really allow for that
123
// possibility, we should iterate here until we reach a fixed
124
// point; but the old UnitGraph that we are attempting to
125
// duplicate did not do that, so we won't either.
126
List origPredsOfTrapped = new ArrayList(getPredsOf(firstTrapped));
127         for (Iterator unitIt = origPredsOfTrapped.iterator();
128          unitIt.hasNext(); ) {
129         Unit pred = (Unit) unitIt.next();
130         addEdge(unitToSuccs, unitToPreds, pred, catcher);
131         }
132     }
133     }
134 }
135
Popular Tags