001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1999 Patrick Lam, Patrick Pominville and Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.baf;
027:
028: import soot.options.*;
029:
030: import soot.*;
031: import soot.jimple.*;
032: import java.util.*;
033:
034: public class BafBody extends Body {
035: public Object clone() {
036: Body b = new BafBody(getMethod());
037: b.importBodyContentsFrom(this );
038: return b;
039: }
040:
041: BafBody(SootMethod m) {
042: super (m);
043: }
044:
045: public BafBody(Body body, Map options) {
046: super (body.getMethod());
047:
048: if (Options.v().verbose())
049: G.v().out.println("[" + getMethod().getName()
050: + "] Constructing BafBody...");
051:
052: JimpleBody jimpleBody;
053:
054: if (body instanceof JimpleBody)
055: jimpleBody = (JimpleBody) body;
056: else
057: throw new RuntimeException(
058: "Can only construct BafBody's directly"
059: + " from JimpleBody's.");
060:
061: jimpleBody.validate();
062:
063: JimpleToBafContext context = new JimpleToBafContext(jimpleBody
064: .getLocalCount());
065:
066: // Convert all locals
067: {
068: Iterator localIt = jimpleBody.getLocals().iterator();
069:
070: while (localIt.hasNext()) {
071: Local l = (Local) localIt.next();
072: Type t = l.getType();
073: Local newLocal;
074:
075: newLocal = Baf.v().newLocal(l.getName(),
076: UnknownType.v());
077:
078: if (t.equals(DoubleType.v()) || t.equals(LongType.v()))
079: newLocal.setType(DoubleWordType.v());
080: else
081: newLocal.setType(WordType.v());
082:
083: context.setBafLocalOfJimpleLocal(l, newLocal);
084: getLocals().add(newLocal);
085: }
086: }
087:
088: Map<Stmt, Unit> stmtToFirstInstruction = new HashMap<Stmt, Unit>();
089:
090: // Convert all jimple instructions
091: {
092: Iterator stmtIt = jimpleBody.getUnits().iterator();
093:
094: while (stmtIt.hasNext()) {
095: Stmt s = (Stmt) stmtIt.next();
096: List<Unit> conversionList = new ArrayList<Unit>();
097:
098: context.setCurrentUnit(s);
099: ((ConvertToBaf) s)
100: .convertToBaf(context, conversionList);
101:
102: stmtToFirstInstruction.put(s, conversionList.get(0));
103: getUnits().addAll(conversionList);
104: }
105: }
106:
107: // Change all place holders
108: {
109: Iterator boxIt = getAllUnitBoxes().iterator();
110:
111: while (boxIt.hasNext()) {
112: UnitBox box = (UnitBox) boxIt.next();
113:
114: if (box.getUnit() instanceof PlaceholderInst) {
115: Unit source = ((PlaceholderInst) box.getUnit())
116: .getSource();
117: box.setUnit(stmtToFirstInstruction.get(source));
118: }
119: }
120: }
121:
122: // Convert all traps
123: {
124: Iterator trapIt = jimpleBody.getTraps().iterator();
125: while (trapIt.hasNext()) {
126: Trap trap = (Trap) trapIt.next();
127:
128: getTraps().add(
129: Baf.v().newTrap(
130: trap.getException(),
131: stmtToFirstInstruction.get(trap
132: .getBeginUnit()),
133: stmtToFirstInstruction.get(trap
134: .getEndUnit()),
135: stmtToFirstInstruction.get(trap
136: .getHandlerUnit())));
137: }
138: }
139:
140: PackManager.v().getPack("bb").apply(this);
141: }
142: }
|