checkpoint
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
index 84e2841..a6da2f6 100644 (file)
@@ -68,8 +68,7 @@ public class Main extends SceneTransformer {
     }
 
     public static Local viaLocal(Value v, Body b) {
-        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
-        b.getLocals().add(l);
+        Local l = newLocal(b, v.getType());
         if (v instanceof IdentityRef) {
             b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
         } else {
@@ -78,8 +77,7 @@ public class Main extends SceneTransformer {
         return l;
     }
     public static Local viaLocal(Value v, Body b, Unit u) {
-        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
-        b.getLocals().add(l);
+        Local l = newLocal(b, v.getType());
         if (v instanceof IdentityRef) {
             b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
         } else {
@@ -132,6 +130,7 @@ public class Main extends SceneTransformer {
 
     public void internalTransform(String phaseName, Map options) {
         System.out.println("begun");
+        init();
         List ac = new LinkedList();
         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
@@ -175,73 +174,74 @@ public class Main extends SceneTransformer {
         return Scene.v().getSootClass(sc.getName().substring(0, sc.getName().lastIndexOf('$')));
     }
 
+    public static InstanceFieldRef newIFR(Body body, SootFieldRef fr) {
+        return Jimple.v().newInstanceFieldRef(body.getThisLocal(), fr);
+    }
+
+    public Type objectType;
+    public List arrayCopyTypes = new LinkedList();
+    public SootMethodRef arrayCopy;
+    public void init() {
+        objectType = Scene.v().getSootClass("java.lang.Object").getType();
+        arrayCopyTypes.add(objectType);
+        arrayCopyTypes.add(IntType.v());
+        arrayCopyTypes.add(objectType);
+        arrayCopyTypes.add(IntType.v());
+        arrayCopyTypes.add(IntType.v());
+        arrayCopy = 
+            Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
+                                    "arraycopy",
+                                    arrayCopyTypes,
+                                    VoidType.v(),
+                                    true);
+    }
+
+    public static void assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
+    public static void assign(Body b, Value left, Value right, Stmt where) {
+        b.getUnits().insertBefore(Jimple.v().newAssignStmt(left, right), where);
+    }
+
     public void fixClass(SootClass sc) {
         SootClass mc = getParent(sc);
         String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
         String incFuncName = subname + "$$inc";
-        SootMethod method = new SootMethod(incFuncName, new LinkedList(),
-                                           IntType.v(), Modifier.PRIVATE,
-                                           new LinkedList());
+        SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
         Body mcInitBody = getInitBody(mc);
         mc.addMethod(method);
         Body body = Jimple.v().newBody(method);
         method.setActiveBody(body);
         ((JimpleBody)body).insertIdentityStmts();
-        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());   body.getLocals().add(l);
-        Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());  body.getLocals().add(l2);
-        Local l3 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());  body.getLocals().add(l3);
+        Local l  =  newLocal(body, IntType.v());
+        Local l2 =  newLocal(body, IntType.v());
+        Local l3 =  newLocal(body, IntType.v());
+        Local l4 =  newLocal(body, IntType.v());
 
         Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
         SootField maxField = new SootField(subname + "$$max", IntType.v());
         mc.addField(maxField);
-        mcInitBody.getUnits()
-            .insertBefore(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(mcInitBody.getThisLocal(), maxField.makeRef()),
-                                                  IntConstant.v(initialSize)),
-                          where);
+        assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
 
         SootField sfr = new SootField(subname + "$$size", IntType.v());
         mc.addField(sfr);
-        mcInitBody.getUnits()
-            .insertBefore(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(mcInitBody.getThisLocal(), sfr.makeRef()),
-                                                  IntConstant.v(0)),
-                          where);
+        assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
         
-        body.getUnits().add(Jimple.v().newAssignStmt(l, Jimple.v().newInstanceFieldRef(body.getThisLocal(), sfr.makeRef())));
-        body.getUnits().add(Jimple.v().newAssignStmt(l2, Jimple.v().newAddExpr(l, IntConstant.v(1))));
-        body.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(body.getThisLocal(),sfr.makeRef()),l2));
-        body.getUnits().add(Jimple.v().newAssignStmt(l3,Jimple.v().newInstanceFieldRef(body.getThisLocal(),maxField.makeRef())));
-        Stmt stmt = Jimple.v().newReturnStmt(l2);
-        body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), stmt));
-
-        Local l4 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
-        body.getLocals().add(l4);
-        body.getUnits().add(Jimple.v().newAssignStmt(l4, Jimple.v().newShlExpr(l3, IntConstant.v(1))));
-        body.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(body.getThisLocal(), maxField.makeRef()), l4));
+        assign(body, l,                                 newIFR(body, sfr.makeRef()));
+        assign(body, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
+        assign(body, newIFR(body, sfr.makeRef()),       l2);
+        assign(body, l3,                                newIFR(body, maxField.makeRef()));
+
+        Stmt returnStmt = Jimple.v().newReturnStmt(l2);
+        body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+
+        assign(body,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
+        assign(body,  newIFR(body, maxField.makeRef()), l4);
 
         for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
             SootField f = getSliceForField((SootField)it.next());
-            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
-            Local ll0 = Jimple.v().newLocal("tmpRef" + (tfr++), f.getType());
-            body.getLocals().add(ll0);
-            body.getUnits().add(Jimple.v().newAssignStmt(ll0, ifr));
-            Local ll = Jimple.v().newLocal("tmpRef" + (tfr++), f.getType());
-            body.getLocals().add(ll);
-            body.getUnits().add(Jimple.v().newAssignStmt(ll,
-                                                         Jimple.v().newNewArrayExpr(((ArrayType)f.getType()).getElementType(),
-                                                                                    l4)));
-            Type ot = Scene.v().getSootClass("java.lang.Object").getType();
-            List types = new LinkedList();
-            types.add(ot);
-            types.add(IntType.v());
-            types.add(ot);
-            types.add(IntType.v());
-            types.add(IntType.v());
-            SootMethodRef arrayCopy =
-                Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
-                                        "arraycopy",
-                                        types,
-                                        VoidType.v(),
-                                        true);
+            Local ll0 = newLocal(body, f.getType());
+            assign(body, ll0, newIFR(body,  f.makeRef()));
+            Local ll = newLocal(body, f.getType());
+            assign(body, ll, Jimple.v().newNewArrayExpr(((ArrayType)f.getType()).getElementType(), l4));
             List args = new LinkedList();
             args.add(ll0);
             args.add(IntConstant.v(0));
@@ -249,7 +249,7 @@ public class Main extends SceneTransformer {
             args.add(IntConstant.v(0));
             args.add(l3);
             body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
-            body.getUnits().add(Jimple.v().newAssignStmt(ifr,ll));
+            body.getUnits().add(Jimple.v().newAssignStmt(newIFR(body,  f.makeRef()), ll));
         }
         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
@@ -261,7 +261,6 @@ public class Main extends SceneTransformer {
                 SootClass c = m.getDeclaringClass();
                 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
                 List li = m.getParameterTypes();
-                //li.add(IntType.v());
                 c.removeMethod(m);
                 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
                 JimpleBody bod = Jimple.v().newBody(nm);
@@ -341,7 +340,7 @@ public class Main extends SceneTransformer {
             if (doremove) sc.removeMethod(m);
 
         }
-        body.getUnits().add(stmt);
+        body.getUnits().add(returnStmt);
     }
 
     public void nuke(SootClass c) {
@@ -564,7 +563,7 @@ public class Main extends SceneTransformer {
                         } else if (implementsGladiator(fr.declaringClass())) {
                             SootClass mc = getParent(fr.declaringClass());
                             SootFieldRef sf = getSliceForField(fr).makeRef();
-                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
+                            InstanceFieldRef sfr = newIFR(body,  sf);
                             vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
                         }
                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {