KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > shimple > toolkits > scalar > ShimpleLocalDefs


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Navindra Umanee <navindra@cs.mcgill.ca>
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.shimple.toolkits.scalar;
21
22 import soot.*;
23 import soot.util.*;
24 import soot.shimple.*;
25 import soot.toolkits.scalar.*;
26 import java.util.*;
27
28 /**
29  * This class implements the LocalDefs interface for Shimple.
30  * ShimpleLocalDefs can be used in conjunction with SimpleLocalUses to
31  * provide Definition/Use and Use/Definition chains in SSA.
32  *
33  * <p> This implementation can be considered a small demo for how SSA
34  * can be put to good use since it is much simpler than
35  * soot.toolkits.scalar.SimpleLocalDefs. Shimple can often be treated
36  * as Jimple with the added benefits of SSA assumptions.
37  *
38  * <p> In addition to the interface required by LocalDefs,
39  * ShimpleLocalDefs also provides a method for obtaining the
40  * definition Unit given only the Local.
41  *
42  * @author Navindra Umanee
43  * @see ShimpleLocalUses
44  * @see soot.toolkits.scalar.SimpleLocalDefs
45  * @see soot.toolkits.scalar.SimpleLocalUses
46  **/

47 public class ShimpleLocalDefs implements LocalDefs
48 {
49     protected Map localToDefs;
50
51     /**
52      * Build a LocalDefs interface from a ShimpleBody. Proper SSA
53      * form is required, otherwise correct behaviour is not
54      * guaranteed.
55      **/

56     public ShimpleLocalDefs(ShimpleBody sb)
57     {
58         // Instead of rebuilding the ShimpleBody without the
59
// programmer's knowledge, throw a RuntimeException
60
if(!sb.isSSA())
61             throw new RuntimeException JavaDoc("ShimpleBody is not in proper SSA form as required by ShimpleLocalDefs. You may need to rebuild it or use SimpleLocalDefs instead.");
62
63         // build localToDefs map simply by iterating through all the
64
// units in the body and saving the unique definition site for
65
// each local -- no need for fancy analysis
66
{
67             Chain unitsChain = sb.getUnits();
68             Iterator unitsIt = unitsChain.iterator();
69             localToDefs = new HashMap(unitsChain.size() * 2 + 1, 0.7f);
70         
71             while(unitsIt.hasNext()){
72                 Unit unit = (Unit) unitsIt.next();
73                 Iterator defBoxesIt = unit.getDefBoxes().iterator();
74                 while(defBoxesIt.hasNext()){
75                     Value value = ((ValueBox)defBoxesIt.next()).getValue();
76
77                     // only map locals
78
if(!(value instanceof Local))
79                         continue;
80                         
81                     localToDefs.put(value, new SingletonList(unit));
82                 }
83             }
84         }
85     }
86
87     /**
88      * Unconditionally returns the definition site of a local (as a
89      * singleton list).
90      *
91      * <p> This method is currently not required by the LocalDefs
92      * interface.
93      **/

94     public List getDefsOf(Local l)
95     {
96         List defs = (List) localToDefs.get(l);
97
98         if(defs == null)
99             throw new RuntimeException JavaDoc("Local not found in Body.");
100
101         return defs;
102     }
103
104     /**
105      * Returns the definition site for a Local at a certain point
106      * (Unit) in a method as a singleton list.
107      *
108      * @param l the Local in question.
109      * @param s a unit that specifies the method context (location) to
110      * query for the definitions of the Local.
111      * @return a singleton list containing the definition site.
112      **/

113     public List getDefsOfAt(Local l, Unit s)
114     {
115         // For consistency with SimpleLocalDefs, check that the local
116
// is indeed used in the given Unit. This neatly sidesteps
117
// the problem of checking whether the local is actually
118
// defined at the given point in the program.
119
{
120             Iterator boxIt = s.getUseBoxes().iterator();
121             boolean defined = false;
122
123             while(boxIt.hasNext()){
124                 Value value = ((ValueBox) boxIt.next()).getValue();
125                 if(value.equals(l)){
126                     defined = true;
127                     break;
128                 }
129             }
130
131             if(!defined)
132                 throw new RuntimeException JavaDoc("Illegal LocalDefs query; local " + l + " is not being used at " + s);
133         }
134
135         return getDefsOf(l);
136     }
137 }
138
Popular Tags