KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrice Pominville, Raja Vallee-Rai
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  * Modified by the Sable Research Group and others 1997-2003.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.toolkits.graph;
28
29 import soot.*;
30 import soot.util.*;
31 import java.util.*;
32 import soot.options.Options;
33
34
35 /**
36  * <p>
37  * Represents a CFG for a {@link Body} instance where the nodes are
38  * {@link Unit} instances, and where, in additional to unexceptional
39  * control flow edges, edges are added from every trapped {@link
40  * Unit} to the {@link Trap}'s handler <code>Unit</code>, regardless
41  * of whether the trapped <code>Unit</code>s may actually throw the
42  * exception caught by the <code>Trap</code>.</p>
43  *
44  * <p>
45  * There are three distinctions between the exceptional edges added
46  * in <code>TrapUnitGraph</code> and the exceptional edges added in
47  * {@link ExceptionalUnitGraph}:
48  * <ol>
49  * <li>
50  * In <code>ExceptionalUnitGraph</code>, the edges to
51  * <code>Trap</code>s are associated with <code>Unit</code>s which
52  * may actually throw an exception which the <code>Trap</code>
53  * catches (according to the {@link
54  * soot.toolkits.exceptions.ThrowAnalysis ThrowAnalysis} used in the
55  * construction of the graph). In <code>TrapUnitGraph</code>, there
56  * are edges from every trapped <code>Unit</code> to the
57  * <code>Trap</code>, regardless of whether it can throw an exception
58  * caught by the <code>Trap</code>.
59  * </li>
60  * <li>
61  * In <code>ExceptionalUnitGraph</code>, when a <code>Unit</code> may
62  * throw an exception that is caught by a <code>Trap</code> there
63  * are edges from every predecessor of the excepting
64  * <code>Unit</code> to the <code>Trap</code>'s handler. In
65  * <code>TrapUnitGraph</code>, edges are not added from the
66  * predecessors of excepting <code>Unit</code>s.</li>
67  * <li>
68  * In <code>ExceptionalUnitGraph</code>, when a <code>Unit</code> may
69  * throw an exception that is caught by a <code>Trap</code>, there
70  * may be no edge from the excepting <code>Unit</code> itself to the
71  * <code>Trap</code> (depending on the possibility of side effects
72  * and the setting of the <code>omitExceptingUnitEdges</code>
73  * parameter). In <code>TrapUnitGraph</code>, there is always an edge
74  * from the excepting <code>Unit</code> to the
75  * <code>Trap</code>.</li>
76  * </ol>
77  */

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

84     public TrapUnitGraph(Body body)
85     {
86         super(body);
87     int size = unitChain.size();
88
89         if(Options.v().time())
90             Timers.v().graphTimer.start();
91
92     unitToSuccs = new HashMap(size * 2 + 1, 0.7f);
93     unitToPreds = new HashMap(size * 2 + 1, 0.7f);
94     buildUnexceptionalEdges(unitToSuccs, unitToPreds);
95     buildExceptionalEdges(unitToSuccs, unitToPreds);
96     makeMappedListsUnmodifiable(unitToSuccs);
97     makeMappedListsUnmodifiable(unitToPreds);
98
99     buildHeadsAndTails();
100
101         if(Options.v().time())
102             Timers.v().graphTimer.end();
103
104     soot.util.PhaseDumper.v().dumpGraph(this, body);
105     }
106
107
108     /**
109      * Method to compute the edges corresponding to exceptional
110      * control flow.
111      *
112      * @param unitToSuccs A <code>Map</code> from {@link Unit}s to {@link
113      * List}s of <code>Unit</code>s. This is an &ldquo;out
114      * parameter&rdquo;; <code>buildExceptionalEdges</code>
115      * will add a mapping for every <code>Unit</code>
116      * within the scope of one or more {@link
117      * Trap}s to a <code>List</code> of the handler
118      * units of those <code>Trap</code>s.
119      *
120      * @param unitToPreds A <code>Map</code> from <code>Unit</code>s to
121      * <code>List</code>s of <code>Unit</code>s. This is an
122      * &ldquo;out parameter&rdquo;;
123      * <code>buildExceptionalEdges</code> will add a
124      * mapping for every <code>Trap</code> handler to
125      * all the <code>Unit</code>s within the scope of
126      * that <code>Trap</code>.
127      */

128     protected void buildExceptionalEdges(Map unitToSuccs, Map unitToPreds) {
129     for (Iterator trapIt = body.getTraps().iterator();
130          trapIt.hasNext(); ) {
131         Trap trap = (Trap) trapIt.next();
132         Unit first = trap.getBeginUnit();
133         Unit last = (Unit) unitChain.getPredOf(trap.getEndUnit());
134         Unit catcher = trap.getHandlerUnit();
135         for (Iterator unitIt = unitChain.iterator(first, last);
136          unitIt.hasNext(); ) {
137         Unit trapped = (Unit) unitIt.next();
138         addEdge(unitToSuccs, unitToPreds, trapped, catcher);
139         }
140     }
141     }
142 }
143
Popular Tags