00001 package edu.ksu.cis.bandera.prog;
00002
00003 import ca.mcgill.sable.soot.*;
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 import ca.mcgill.sable.soot.SootMethod;
00038 import ca.mcgill.sable.soot.jimple.*;
00039 import java.util.*;
00040 import edu.ksu.cis.bandera.jext.*;
00041 import java.util.Hashtable;
00042 public class RenameExpression extends AbstractBanderaValueSwitch {
00043 static int count = 0;
00044 int id = ++count;
00045 String methodString;
00046 List parameters;
00047 Hashtable locals;
00048 public static List fields = new Vector();
00049 public ca.mcgill.sable.soot.SootMethod topMethod;
00050 protected ca.mcgill.sable.soot.SootMethod currentMethod;
00051 public static java.util.Hashtable newExpr2original = new Hashtable();
00052 public RenameExpression(String methodString, List parameters, Hashtable locals, SootMethod topMethod, SootMethod currentMethod) {
00053 this.methodString = methodString;
00054 this.parameters = parameters;
00055 this.locals = locals;
00056 this.topMethod = topMethod;
00057 this.currentMethod = currentMethod;
00058 }
00059 public void caseAddExpr(AddExpr v) {
00060 setResult(Jimple.v().newAddExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00061 }
00062 public void caseAndExpr(AndExpr v) {
00063 setResult(Jimple.v().newAndExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00064 }
00065 public void caseArrayRef(ArrayRef v) {
00066 setResult(Jimple.v().newArrayRef(renameExpr(v.getBase()), renameExpr(v.getIndex())));
00067 }
00068 public void caseCastExpr(CastExpr v) {
00069 setResult(Jimple.v().newCastExpr(renameExpr(v.getOp()), v.getCastType()));
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 public void caseCaughtExceptionRef(CaughtExceptionRef v) {
00079 setResult(v);
00080
00081 }
00082
00083
00084
00085
00086 public void caseChooseExpr(ChooseExpr v) {
00087 setResult(v);
00088 }
00089 public void caseCmpExpr(CmpExpr v) {
00090 setResult(Jimple.v().newCmpExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00091 }
00092 public void caseCmpgExpr(CmpgExpr v) {
00093 setResult(Jimple.v().newCmpgExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00094 }
00095 public void caseCmplExpr(CmplExpr v) {
00096 setResult(Jimple.v().newCmplExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00097 }
00098
00099
00100
00101
00102 public void caseComplementExpr(ComplementExpr expr) {
00103 System.out.println("Don't know what to do.");
00104 setResult(expr);
00105 }
00106 public void caseDivExpr(DivExpr v) {
00107 setResult(Jimple.v().newDivExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00108 }
00109 public void caseDoubleConstant(DoubleConstant v) {
00110 setResult(DoubleConstant.v(v.value));
00111 }
00112 public void caseEqExpr(EqExpr v) {
00113 setResult(Jimple.v().newEqExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00114 }
00115 public void caseFloatConstant(FloatConstant v) {
00116 setResult(FloatConstant.v(v.value));
00117 }
00118 public void caseGeExpr(GeExpr v) {
00119 setResult(Jimple.v().newGeExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00120 }
00121 public void caseGtExpr(GtExpr v) {
00122 setResult(Jimple.v().newGtExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00123 }
00124 public void caseInstanceFieldRef(InstanceFieldRef v) {
00125 if (!fields.contains(v.getField())) {
00126 fields.add(v.getField());
00127 }
00128 Value base = renameExpr(v.getBase());
00129
00130
00131 setResult(Jimple.v().newInstanceFieldRef(base, v.getField()));
00132 }
00133 public void caseInstanceOfExpr(InstanceOfExpr v) {
00134 RefType type = (RefType) v.getCheckType();
00135 setResult(Jimple.v().newInstanceOfExpr(renameExpr(v.getOp()), RefType.v(type.className)));
00136 }
00137 public void caseIntConstant(IntConstant v) {
00138 setResult(IntConstant.v(v.value));
00139 }
00140 public void caseInterfaceInvokeExpr(InterfaceInvokeExpr v) {
00141 defaultCase(v);
00142
00143 }
00144 public void caseLeExpr(LeExpr v) {
00145 setResult(Jimple.v().newLeExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00146 }
00147 public void caseLengthExpr(LengthExpr v) {
00148 setResult(Jimple.v().newLengthExpr(renameExpr(v.getOp())));
00149 }
00150 public void caseLocal(Local v) {
00151 if ((Inline.typeTable.size() > 0) && (Inline.typeTable.get(v) == null) && Inline.isMethodCompiled) {
00152 throw new RuntimeException("Cannot find abstraction for local <" + currentMethod + ", " + v + ">");
00153 }
00154 Local l;
00155 if ((l = (Local) locals.get(methodString + v.getName() )) == null) {
00156 if (methodString.equals(""))
00157 l = Jimple.v().newLocal(v.getName(), v.getType());
00158 else
00159 l = Jimple.v().newLocal(methodString +v.getName(), v.getType());
00160 locals.put(methodString + v.getName() , l);
00161 Inline.newLocal2original.put(l, v);
00162 if ((Inline.typeTable.size() > 0) && Inline.isMethodCompiled)
00163 Inline.typeTable.put(l, Inline.typeTable.get(v));
00164 edu.ksu.cis.bandera.jjjc.CompilationManager.getAnnotationManager().putInlineLocal(new LocalExpr(topMethod, l), new LocalExpr(currentMethod, v));
00165 } else
00166 l = l;
00167 setResult(l);
00168 }
00169
00170
00171
00172
00173 public void caseLogicalAndExpr(LogicalAndExpr expr) {
00174 System.out.println("Don't know what to do.");
00175 setResult(expr);
00176 }
00177
00178
00179
00180
00181 public void caseLogicalOrExpr(LogicalOrExpr expr) {
00182 System.out.println("Don't know what to do.");
00183 setResult(expr);
00184 }
00185 public void caseLongConstant(LongConstant v) {
00186 setResult(LongConstant.v(v.value));
00187 }
00188 public void caseLtExpr(LtExpr v) {
00189 setResult(Jimple.v().newLtExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00190 }
00191 public void caseMulExpr(MulExpr v) {
00192 setResult(Jimple.v().newMulExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00193 }
00194 public void caseNeExpr(NeExpr v) {
00195 setResult(Jimple.v().newNeExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00196 }
00197 public void caseNegExpr(NegExpr v) {
00198 setResult(Jimple.v().newNegExpr(renameExpr(v.getOp())));
00199 }
00200 public void caseNewArrayExpr(NewArrayExpr v) {
00201 setResult(Jimple.v().newNewArrayExpr(v.getBaseType(), renameExpr(v.getSize())));
00202 }
00203 public void caseNewExpr(NewExpr v) {
00204 setResult(v);
00205 }
00206 public void caseNewMultiArrayExpr(NewMultiArrayExpr v) {
00207 defaultCase(v);
00208 }
00209 public void caseNullConstant(NullConstant v) {
00210 setResult(NullConstant.v());
00211 }
00212 public void caseOrExpr(OrExpr v) {
00213 setResult(Jimple.v().newOrExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00214 }
00215 public void caseParameterRef(ParameterRef v) {
00216 if (parameters == null || parameters.size() == 0)
00217 setResult(v);
00218 else {
00219 setResult((Value) parameters.get(0));
00220 parameters.remove(0);
00221 }
00222 }
00223 public void caseRemExpr(RemExpr v) {
00224 setResult(Jimple.v().newRemExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00225 }
00226 public void caseShlExpr(ShlExpr v) {
00227 setResult(Jimple.v().newShlExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00228 }
00229 public void caseShrExpr(ShrExpr v) {
00230 setResult(Jimple.v().newShrExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00231 }
00232 public void caseSpecialInvokeExpr(SpecialInvokeExpr v) {
00233
00234 ca.mcgill.sable.util.List l = new ca.mcgill.sable.util.VectorList();
00235 int i;
00236 for (i = 0; i < v.getArgCount(); i++)
00237 l.add(renameExpr(v.getArg(i)));
00238 setResult(Jimple.v().newSpecialInvokeExpr((Local) renameExpr(v.getBase()), v.getMethod(), l));
00239
00240 }
00241 public void caseStaticFieldRef(StaticFieldRef v) {
00242 if (!fields.contains(v.getField())) {
00243 fields.add(v.getField());
00244 }
00245 setResult(v);
00246 }
00247 public void caseStaticInvokeExpr(StaticInvokeExpr v) {
00248 ca.mcgill.sable.util.List l = new ca.mcgill.sable.util.VectorList();
00249 int i;
00250 for (i = 0; i < v.getArgCount(); i++)
00251 l.add(renameExpr(v.getArg(i)));
00252 setResult(Jimple.v().newStaticInvokeExpr(v.getMethod(), l));
00253 }
00254 public void caseStringConstant(StringConstant v) {
00255 setResult(StringConstant.v(v.value));
00256 }
00257 public void caseSubExpr(SubExpr v) {
00258 setResult(Jimple.v().newSubExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00259 }
00260 public void caseThisRef(ThisRef v) {
00261 if (parameters == null || parameters.size() == 0)
00262 setResult(v);
00263 else {
00264 setResult((Value) parameters.get(0));
00265 parameters.remove(0);
00266 }
00267 }
00268 public void caseUshrExpr(UshrExpr v) {
00269 setResult(Jimple.v().newUshrExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00270 }
00271 public void caseVirtualInvokeExpr(VirtualInvokeExpr v) {
00272 Value base = renameExpr(v.getBase());
00273
00274
00275 ca.mcgill.sable.util.List l = new ca.mcgill.sable.util.VectorList();
00276 for (int i = 0; i < v.getArgCount(); i++)
00277 l.add(renameExpr(v.getArg(i)));
00278 setResult(Jimple.v().newVirtualInvokeExpr((Local) base, v.getMethod(), l));
00279 }
00280 public void caseXorExpr(XorExpr v) {
00281 setResult(Jimple.v().newXorExpr(renameExpr(v.getOp1()), renameExpr(v.getOp2())));
00282 }
00283 public void defaultCase(Object v) {
00284 throw new RuntimeException("Unhandled expression in" + " inline " + v.getClass() + " " + v);
00285 }
00286 public Value renameExpr(Value v) {
00287 v.apply(this);
00288 newExpr2original.put(getResult(), v);
00289 return (Value) this.getResult();
00290 }
00291 public String toString() {
00292 return "RE " + id;
00293 }
00294 }