KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > baf > BafBody


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam, Patrick Pominville and 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.baf;
31 import soot.options.*;
32
33 import soot.*;
34 import soot.jimple.*;
35 import soot.baf.toolkits.base.*;
36 import soot.toolkits.scalar.*;
37
38 import soot.util.*;
39 import java.util.*;
40 import java.io.*;
41
42 public class BafBody extends Body
43 {
44     public Object JavaDoc clone()
45     {
46         Body b = new BafBody(getMethod());
47         b.importBodyContentsFrom(this);
48         return b;
49     }
50
51     BafBody(SootMethod m)
52     {
53         super(m);
54     }
55
56     public BafBody(Body body, Map options)
57     {
58         super(body.getMethod());
59
60         if(Options.v().verbose())
61             G.v().out.println("[" + getMethod().getName() + "] Constructing BafBody...");
62
63         JimpleBody jimpleBody;
64
65         if(body instanceof JimpleBody)
66             jimpleBody = (JimpleBody) body;
67         else
68             throw new RuntimeException JavaDoc("Can only construct BafBody's directly"
69               + " from JimpleBody's.");
70
71         jimpleBody.validate();
72                
73         JimpleToBafContext context = new JimpleToBafContext(jimpleBody.getLocalCount());
74            
75         // Convert all locals
76
{
77             Iterator localIt = jimpleBody.getLocals().iterator();
78             
79             while(localIt.hasNext())
80             {
81                 Local l = (Local) localIt.next();
82                 Type t = l.getType();
83                 Local newLocal;
84                 
85                 newLocal = Baf.v().newLocal(l.getName(), UnknownType.v());
86                 
87                 if(t.equals(DoubleType.v()) || t.equals(LongType.v()))
88                     newLocal.setType(DoubleWordType.v());
89                 else
90                     newLocal.setType(WordType.v());
91         
92                 context.setBafLocalOfJimpleLocal(l, newLocal);
93                 getLocals().add(newLocal);
94             }
95         }
96     
97         Map stmtToFirstInstruction = new HashMap();
98             
99         // Convert all jimple instructions
100
{
101             Iterator stmtIt = jimpleBody.getUnits().iterator();
102             
103             while(stmtIt.hasNext())
104             {
105                 Stmt s = (Stmt) stmtIt.next();
106                 List conversionList = new ArrayList();
107
108                 context.setCurrentUnit(s);
109                 ((ConvertToBaf) s).convertToBaf(context, conversionList);
110                
111                 stmtToFirstInstruction.put(s, conversionList.get(0));
112                 getUnits().addAll(conversionList);
113             }
114         }
115         
116         // Change all place holders
117
{
118             Iterator boxIt = getAllUnitBoxes().iterator();
119             
120             while(boxIt.hasNext())
121             {
122                 UnitBox box = (UnitBox) boxIt.next();
123                 
124                 if(box.getUnit() instanceof PlaceholderInst)
125                 {
126                     Unit source = ((PlaceholderInst) box.getUnit()).getSource();
127                     box.setUnit((Unit) stmtToFirstInstruction.get(source));
128                 }
129             }
130         }
131
132         // Convert all traps
133
{
134             Iterator trapIt = jimpleBody.getTraps().iterator();
135             while (trapIt.hasNext())
136             {
137                 Trap trap = (Trap) trapIt.next();
138
139                 getTraps().add(Baf.v().newTrap(trap.getException(),
140                      (Unit)stmtToFirstInstruction.get(trap.getBeginUnit()),
141                      (Unit)stmtToFirstInstruction.get(trap.getEndUnit()),
142                      (Unit)stmtToFirstInstruction.get(trap.getHandlerUnit())));
143             }
144         }
145         
146         PackManager.v().getPack( "bb" ).apply( this );
147     }
148 }
149
Popular Tags