00001 package ca.mcgill.sable.soot.jimple;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 import ca.mcgill.sable.soot.*;
00083 import ca.mcgill.sable.util.*;
00084
00085 public class JTableSwitchStmt extends AbstractStmt implements TableSwitchStmt
00086 {
00087 UnitBox defaultTargetBox;
00088 ValueBox keyBox;
00089 int lowIndex;
00090 int highIndex;
00091 UnitBox[] targetBoxes;
00092
00093 List stmtBoxes;
00094
00095 JTableSwitchStmt(Value key, int lowIndex, int highIndex, List targets, Unit defaultTarget)
00096 {
00097 this(Jimple.v().newImmediateBox(key), lowIndex, highIndex,
00098 getTargetBoxesArray(targets),
00099 Jimple.v().newStmtBox(defaultTarget));
00100 }
00101 protected JTableSwitchStmt(ValueBox keyBox, int lowIndex, int highIndex,
00102 UnitBox[] targetBoxes, UnitBox defaultTargetBox)
00103 {
00104 this.keyBox = keyBox;
00105 this.defaultTargetBox = defaultTargetBox;
00106
00107 this.lowIndex = lowIndex;
00108 this.highIndex = highIndex;
00109
00110 this.targetBoxes = targetBoxes;
00111
00112
00113 {
00114 stmtBoxes = new ArrayList();
00115
00116 for(int i = 0; i < targetBoxes.length; i++)
00117 stmtBoxes.add(targetBoxes[i]);
00118
00119 stmtBoxes.add(defaultTargetBox);
00120 stmtBoxes = Collections.unmodifiableList(stmtBoxes);
00121 }
00122 }
00123 public void apply(Switch sw)
00124 {
00125 ((StmtSwitch) sw).caseTableSwitchStmt(this);
00126 }
00127 public Unit getDefaultTarget()
00128 {
00129 return defaultTargetBox.getUnit();
00130 }
00131 public UnitBox getDefaultTargetBox()
00132 {
00133 return defaultTargetBox;
00134 }
00135 public List getDefBoxes()
00136 {
00137 return emptyList;
00138 }
00139 public int getHighIndex()
00140 {
00141 return highIndex;
00142 }
00143 public Value getKey()
00144 {
00145 return keyBox.getValue();
00146 }
00147 public ValueBox getKeyBox()
00148 {
00149 return keyBox;
00150 }
00151 public int getLowIndex()
00152 {
00153 return lowIndex;
00154 }
00155 public Unit getTarget(int index)
00156 {
00157 return targetBoxes[index].getUnit();
00158 }
00159 public UnitBox getTargetBox(int index)
00160 {
00161 return targetBoxes[index];
00162 }
00163
00164 private static UnitBox[] getTargetBoxesArray(List targets)
00165 {
00166 UnitBox[] targetBoxes = new UnitBox[targets.size()];
00167
00168 for(int i = 0; i < targetBoxes.length; i++)
00169 targetBoxes[i] = Jimple.v().newStmtBox((Stmt) targets.get(i));
00170
00171 return targetBoxes;
00172 }
00173 public List getTargets()
00174 {
00175 List targets = new ArrayList();
00176
00177 for(int i = 0; i < targetBoxes.length; i++)
00178 targets.add(targetBoxes[i].getUnit());
00179
00180 return targets;
00181 }
00182 public List getUnitBoxes()
00183 {
00184 return stmtBoxes;
00185 }
00186 public List getUseBoxes()
00187 {
00188 List list = new ArrayList();
00189
00190 list.addAll(keyBox.getValue().getUseBoxes());
00191 list.add(keyBox);
00192
00193 return list;
00194 }
00195 public void setDefaultTarget(Unit defaultTarget)
00196 {
00197 defaultTargetBox.setUnit(defaultTarget);
00198 }
00199 public void setHighIndex(int highIndex)
00200 {
00201 this.highIndex = highIndex;
00202 }
00203 public void setKey(Value key)
00204 {
00205 keyBox.setValue(key);
00206 }
00207 public void setLowIndex(int lowIndex)
00208 {
00209 this.lowIndex = lowIndex;
00210 }
00211 public void setTarget(int index, Unit target)
00212 {
00213 targetBoxes[index].setUnit(target);
00214 }
00215 public void setTargets(List targets)
00216 {
00217 for(int i = 0; i < targets.size(); i++)
00218 targetBoxes[i].setUnit((Stmt) targets.get(i));
00219 }
00220 protected String toString(boolean isBrief, Map stmtToName, String indentation)
00221 {
00222 StringBuffer buffer = new StringBuffer();
00223 String endOfLine = (indentation.equals("")) ? " " : "\n";
00224
00225 buffer.append(indentation + "tableswitch(" + ((isBrief) ? ((ToBriefString) keyBox.getValue()).toBriefString() :
00226 keyBox.getValue().toString()) + ")" + endOfLine);
00227
00228 buffer.append(indentation + "{" + endOfLine);
00229
00230 for(int i = lowIndex; i <= highIndex; i++)
00231 {
00232 buffer.append(indentation + " case " + i + ": goto " + (String) stmtToName.get(getTarget(i - lowIndex)) + ";" + endOfLine);
00233 }
00234
00235 buffer.append(indentation + " default: goto " + (String) stmtToName.get(getDefaultTarget()) + ";" + endOfLine);
00236 buffer.append(indentation + "}");
00237
00238 return buffer.toString();
00239 }
00240 }