001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.cfg;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.tree.*;
026:
027: /**
028: * <tt>ReplaceTarget</tt> replaces the block that is the target of a
029: * <tt>JumpStmt</tt>, <tt>JsrStmt</tt>, <tt>RetStmt</tt>,
030: * <tt>GotoStmt</tt>, <tt>SwitchStmt</tt>, or <tt>IfStmt</tt> with
031: * another <tt>Block</tt>.
032: */
033: public class ReplaceTarget extends TreeVisitor {
034: Block oldDst;
035:
036: Block newDst;
037:
038: public ReplaceTarget(final Block oldDst, final Block newDst) {
039: this .oldDst = oldDst;
040: this .newDst = newDst;
041: }
042:
043: public void visitTree(final Tree tree) {
044: final Stmt last = tree.lastStmt();
045:
046: if (last instanceof JumpStmt) {
047: final JumpStmt stmt = (JumpStmt) last;
048:
049: if (FlowGraph.DEBUG) {
050: System.out.println(" Replacing " + oldDst + " with "
051: + newDst + " in " + stmt);
052: }
053:
054: if (stmt.catchTargets().remove(oldDst)) {
055: stmt.catchTargets().add(newDst);
056: }
057:
058: stmt.visit(this );
059: }
060: }
061:
062: public void visitJsrStmt(final JsrStmt stmt) {
063: if (stmt.sub().entry() == oldDst) {
064: if (FlowGraph.DEBUG) {
065: System.out.print(" replacing " + stmt);
066: }
067:
068: stmt.block().graph().setSubEntry(stmt.sub(), newDst);
069:
070: if (FlowGraph.DEBUG) {
071: System.out.println(" with " + stmt);
072: }
073: }
074: }
075:
076: public void visitRetStmt(final RetStmt stmt) {
077: final Iterator paths = stmt.sub().paths().iterator();
078:
079: while (paths.hasNext()) {
080: final Block[] path = (Block[]) paths.next();
081:
082: if (FlowGraph.DEBUG) {
083: System.out.println(" path = " + path[0] + " "
084: + path[1]);
085: }
086:
087: if (path[1] == oldDst) {
088: if (FlowGraph.DEBUG) {
089: System.out.println(" replacing ret to " + oldDst
090: + " with ret to " + newDst);
091: }
092:
093: path[1] = newDst;
094: ((JsrStmt) path[0].tree().lastStmt()).setFollow(newDst);
095: }
096: }
097: }
098:
099: public void visitGotoStmt(final GotoStmt stmt) {
100: if (stmt.target() == oldDst) {
101: if (FlowGraph.DEBUG) {
102: System.out.print(" replacing " + stmt);
103: }
104:
105: stmt.setTarget(newDst);
106:
107: if (FlowGraph.DEBUG) {
108: System.out.println(" with " + stmt);
109: }
110: }
111: }
112:
113: public void visitSwitchStmt(final SwitchStmt stmt) {
114: if (stmt.defaultTarget() == oldDst) {
115: if (FlowGraph.DEBUG) {
116: System.out.print(" replacing " + stmt);
117: }
118:
119: stmt.setDefaultTarget(newDst);
120:
121: if (FlowGraph.DEBUG) {
122: System.out.println(" with " + stmt);
123: }
124: }
125:
126: final Block[] targets = stmt.targets();
127:
128: for (int i = 0; i < targets.length; i++) {
129: if (targets[i] == oldDst) {
130: if (FlowGraph.DEBUG) {
131: System.out.print(" replacing " + stmt);
132: }
133:
134: targets[i] = newDst;
135:
136: if (FlowGraph.DEBUG) {
137: System.out.println(" with " + stmt);
138: }
139: }
140: }
141: }
142:
143: public void visitIfStmt(final IfStmt stmt) {
144: if (stmt.trueTarget() == oldDst) {
145: if (FlowGraph.DEBUG) {
146: System.out.print(" replacing " + stmt);
147: }
148:
149: stmt.setTrueTarget(newDst);
150:
151: if (FlowGraph.DEBUG) {
152: System.out.println(" with " + stmt);
153: }
154: }
155:
156: if (stmt.falseTarget() == oldDst) {
157: if (FlowGraph.DEBUG) {
158: System.out.print(" replacing " + stmt);
159: }
160:
161: stmt.setFalseTarget(newDst);
162:
163: if (FlowGraph.DEBUG) {
164: System.out.println(" with " + stmt);
165: }
166: }
167: }
168: }
|