001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 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: package soot.jbco.bafTransformations;
021:
022: import java.util.*;
023:
024: import soot.*;
025: import soot.baf.*;
026: import soot.util.*;
027: import soot.jbco.IJbcoTransform;
028: import soot.jbco.util.*;
029:
030: /**
031: * @author Michael Batchelder
032: *
033: * Created on 24-May-2006
034: */
035: public class WrapSwitchesInTrys extends BodyTransformer implements
036: IJbcoTransform {
037:
038: int totaltraps = 0;
039:
040: public static String dependancies[] = new String[] {
041: "bb.jbco_ptss", "bb.jbco_ful", "bb.lp" };
042:
043: public String[] getDependancies() {
044: return dependancies;
045: }
046:
047: public static String name = "bb.jbco_ptss";
048:
049: public String getName() {
050: return name;
051: }
052:
053: public void outputSummary() {
054: out.println("Switches wrapped in Tries: " + totaltraps);
055: }
056:
057: protected void internalTransform(Body b, String phaseName,
058: Map options) {
059: int weight = soot.jbco.Main.getWeight(phaseName, b.getMethod()
060: .getSignature());
061: if (weight == 0)
062: return;
063:
064: int i = 0;
065: Unit handler = null;
066: Chain traps = b.getTraps();
067: PatchingChain units = b.getUnits();
068: Iterator it = units.snapshotIterator();
069: while (it.hasNext()) {
070: Unit u = (Unit) it.next();
071: if (u instanceof TableSwitchInst) {
072: TableSwitchInst twi = (TableSwitchInst) u;
073:
074: if (!BodyBuilder.isExceptionCaughtAt(units, twi, traps
075: .iterator())
076: && Rand.getInt(10) <= weight) {
077: if (handler == null) {
078: Iterator uit = units.snapshotIterator();
079: while (uit.hasNext()) {
080: Unit uthrow = (Unit) uit.next();
081: if (uthrow instanceof ThrowInst
082: && !BodyBuilder
083: .isExceptionCaughtAt(units,
084: uthrow, traps
085: .iterator())) {
086: handler = uthrow;
087: break;
088: }
089: }
090:
091: if (handler == null) {
092: handler = Baf.v().newThrowInst();
093: units.add(handler);
094: }
095: }
096:
097: int size = 4;
098: Unit succ = (Unit) units.getSuccOf(twi);
099: while (!BodyBuilder.isExceptionCaughtAt(units,
100: succ, traps.iterator())
101: && size-- > 0) {
102: Object o = units.getSuccOf(succ);
103: if (o != null)
104: succ = (Unit) o;
105: else
106: break;
107: }
108:
109: traps.add(Baf.v().newTrap(
110: ThrowSet.getRandomThrowable(), twi, succ,
111: handler));
112: i++;
113: }
114: }
115: }
116:
117: totaltraps += i;
118: if (i > 0 && debug) {
119: StackTypeHeightCalculator.calculateStackHeights(b);
120: }
121: }
122: }
|