KickJava   Java API By Example, From Geeks To Geeks.

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


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.jimple.*;
25 import soot.shimple.*;
26 import soot.toolkits.scalar.*;
27 import java.util.*;
28
29 /**
30  * This class implements the LocalUses interface for Shimple.
31  * ShimpleLocalUses can be used in conjunction with SimpleLocalDefs to
32  * provide Definition/Use and Use/Definition chains in SSA.
33  *
34  * <p> In addition to the interface required by LocalUses,
35  * ShimpleLocalUses also provides a method for obtaining the list of
36  * uses given only the Local. Furthermore, unlike SimpleLocalUses, a
37  * LocalDefs object is not required when constructing
38  * ShimpleLocalUses.
39  *
40  * @author Navindra Umanee
41  * @see ShimpleLocalDefs
42  * @see soot.toolkits.scalar.SimpleLocalDefs
43  * @see soot.toolkits.scalar.SimpleLocalUses
44  **/

45 public class ShimpleLocalUses implements LocalUses
46 {
47     protected Map localToUses;
48
49     /**
50      * Build a LocalUses interface from a ShimpleBody. Proper SSA
51      * form is required, otherwise correct behaviour is not
52      * guaranteed.
53      **/

54     public ShimpleLocalUses(ShimpleBody sb)
55     {
56         // Instead of rebuilding the ShimpleBody without the
57
// programmer's knowledge, throw a RuntimeException
58
if(!sb.isSSA())
59             throw new RuntimeException JavaDoc("ShimpleBody is not in proper SSA form as required by ShimpleLocalUses. You may need to rebuild it or use SimpleLocalUses instead.");
60
61         // initialise the map
62
localToUses = new HashMap();
63         Iterator localsIt = sb.getLocals().iterator();
64         while(localsIt.hasNext()){
65             Local local = (Local) localsIt.next();
66             localToUses.put(local, new ArrayList());
67         }
68
69         // iterate through the units and save each Local use in the
70
// appropriate list -- due to SSA form, each Local has a
71
// unique def, and therefore one appropriate list.
72
Iterator unitsIt = sb.getUnits().iterator();
73         while(unitsIt.hasNext()){
74             Unit unit = (Unit) unitsIt.next();
75             Iterator boxIt = unit.getUseBoxes().iterator();
76
77             while(boxIt.hasNext()){
78                 ValueBox box = (ValueBox)boxIt.next();
79                 Value value = box.getValue();
80
81                 if(!(value instanceof Local))
82                     continue;
83
84                 List useList = (List) localToUses.get(value);
85                 useList.add(new UnitValueBoxPair(unit, box));
86             }
87         }
88     }
89
90     /**
91      * Returns all the uses of the given Local as a list of
92      * UnitValueBoxPairs, each containing a Unit that uses the local
93      * and the corresponding ValueBox containing the Local.
94      *
95      * <p> This method is currently not required by the LocalUses
96      * interface.
97      **/

98     public List getUsesOf(Local local)
99     {
100         List uses = (List) localToUses.get(local);
101         if(uses == null)
102             return Collections.EMPTY_LIST;
103         return uses;
104     }
105
106     /**
107      * If a Local is defined in the Unit, returns all the uses of that
108      * Local as a list of UnitValueBoxPairs, each containing a Unit
109      * that uses the local and the corresponding ValueBox containing
110      * the Local.
111      **/

112     public List getUsesOf(Unit unit)
113     {
114         List defBoxes = unit.getDefBoxes();
115         
116         switch(defBoxes.size()){
117         case 0:
118             return Collections.EMPTY_LIST;
119         case 1:
120             Value local = ((ValueBox)defBoxes.get(0)).getValue();
121             if(!(local instanceof Local))
122                 return Collections.EMPTY_LIST;
123             return getUsesOf((Local) local);
124         default:
125             G.v().out.println("Warning: Unit has multiple definition boxes?");
126             List usesList = new ArrayList();
127             Iterator defBoxesIt = defBoxes.iterator();
128             while(defBoxesIt.hasNext()){
129                 Value def = ((ValueBox)defBoxesIt.next()).getValue();
130                 if(def instanceof Local)
131                     usesList.addAll(getUsesOf((Local) def));
132             }
133             return usesList;
134         }
135     }
136 }
137
Popular Tags