KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > base > Zonation


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 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-1999.
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
28
29
30 package soot.jimple.toolkits.base;
31
32 import soot.*;
33 import soot.jimple.*;
34 import soot.util.*;
35 import java.util.*;
36
37 public class Zonation
38 {
39     private int zoneCount;
40     private Map unitToZone;
41     
42     public Zonation(StmtBody body)
43     {
44         Chain units = body.getUnits();
45         Map unitToTrapBoundaries = new HashMap();
46         
47         // Initialize each unit to an empty set
48
{
49             Iterator unitIt = units.iterator();
50             
51             while(unitIt.hasNext())
52             {
53                 Unit u = (Unit) unitIt.next();
54                 
55                 unitToTrapBoundaries.put(u, new ArrayList());
56             }
57         }
58             
59         // Build trap boundaries
60
{
61             Iterator trapIt = body.getTraps().iterator();
62             
63             while(trapIt.hasNext())
64             {
65                 Trap t = (Trap) trapIt.next();
66                 
67                 List boundary = (List) unitToTrapBoundaries.get(t.getBeginUnit());
68                 boundary.add(t);
69                 
70                 boundary = (List) unitToTrapBoundaries.get(t.getEndUnit());
71                 boundary.add(t);
72             }
73         }
74         
75         // Traverse units, assigning each to a zone
76
{
77             Map trapListToZone = new HashMap(10, 0.7f);
78             List currentTraps = new ArrayList();
79             Zone currentZone;
80             
81             zoneCount = 0;
82             unitToZone = new HashMap(units.size() * 2 + 1, 0.7f);
83                         
84             // Initialize first empty zone
85
currentZone = new Zone("0");
86                 trapListToZone.put(new ArrayList(), currentZone);
87                 
88             Iterator unitIt = units.iterator();
89             
90             while(unitIt.hasNext())
91             {
92                 Unit u = (Unit) unitIt.next();
93                 
94                 // Process trap boundaries
95
{
96                     List trapBoundaries = (List) unitToTrapBoundaries.get(u);
97
98                     if(trapBoundaries.size() != 0)
99                     {
100                         Iterator trapIt = trapBoundaries.iterator();
101                         
102                         while(trapIt.hasNext())
103                         {
104                             Trap trap = (Trap) trapIt.next();
105                             
106                             if(currentTraps.contains(trap))
107                                 currentTraps.remove(trap);
108                             else
109                                 currentTraps.add(trap);
110                         }
111                                           
112                         if(trapListToZone.containsKey(currentTraps))
113                             currentZone = (Zone) trapListToZone.get(currentTraps);
114                         else
115                         {
116                             // Create a new zone
117

118                             zoneCount++;
119                             currentZone = new Zone(new Integer JavaDoc(zoneCount).toString());
120                             
121                             trapListToZone.put(currentTraps, currentZone);
122                         }
123                                                     
124                     }
125                 }
126                     
127                 unitToZone.put(u, currentZone);
128             }
129         }
130         
131     }
132     
133     public Zone getZoneOf(Unit u)
134     {
135         Zone z = (Zone) unitToZone.get(u);
136         
137         if(z == null)
138             throw new RuntimeException JavaDoc("null zone!");
139
140         return z;
141     }
142     
143     public int getZoneCount()
144     {
145         return zoneCount;
146     }
147 }
148
Popular Tags