checkpoint
authoradam <adam@megacz.com>
Thu, 19 May 2005 03:27:24 +0000 (03:27 +0000)
committeradam <adam@megacz.com>
Thu, 19 May 2005 03:27:24 +0000 (03:27 +0000)
darcs-hash:20050519032724-5007d-9bcaa8a57dc656b191f31718d57adacbadaba2ee.gz

src/org/ibex/arenaj/Main.java

index ea31f02..78e1c58 100644 (file)
@@ -48,7 +48,6 @@ public class Main extends SceneTransformer {
         args2[args.length + 6] = "-p";
         args2[args.length + 7] = "jtp";
         args2[args.length + 8] = "enabled:false";
-        //args2[args.length + 9] = "-keep-line-number";
         args2[args.length + 9] = "-allow-phantom-refs";
         args2[args.length + 10] = "-allow-phantom-refs";
         args2[args.length + 11] = "-cp";
@@ -62,7 +61,6 @@ public class Main extends SceneTransformer {
     // The main loop //////////////////////////////////////////////////////////////////////////////
 
     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();) {
@@ -71,24 +69,13 @@ public class Main extends SceneTransformer {
         }
         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
-            if (implementsGladiator(sc)) {
-                System.out.println("  application class: " + sc.getName());
-                System.out.println("  application class: " + getArenaForGladiator(sc).getName());
-                sc.setApplicationClass();
-                getArenaForGladiator(sc).setApplicationClass();
-                ac.add(sc);
-                ac.add(getArenaForGladiator(sc));
-                /*
-                for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
-                    SootMethod m = (SootMethod)i2.next();
-                    if (m.isConcrete()) m.retrieveActiveBody();
-                }
-                for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
-                    SootMethod m = (SootMethod)i2.next();
-                    if (m.isConcrete()) m.retrieveActiveBody();
-                }
-                */
-            }
+            if (!implementsGladiator(sc)) continue;
+            System.out.println("  application class: " + sc.getName());
+            System.out.println("  application class: " + getArenaForGladiator(sc).getName());
+            sc.setApplicationClass();
+            getArenaForGladiator(sc).setApplicationClass();
+            ac.add(sc);
+            ac.add(getArenaForGladiator(sc));
         }
         for(Iterator it = ac.iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
@@ -158,60 +145,74 @@ public class Main extends SceneTransformer {
 
     // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
 
-    public void processGladiatorClass(SootClass sc) {
-        SootClass mc = getArenaForGladiator(sc);
-        String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
-        String incFuncName = subname + "$$inc";
+    public SootMethod createIncMethod(SootClass sc) {
+        String incFuncName = getGladiatorName(sc) + "$$inc";
+        SootClass arena = getArenaForGladiator(sc);
         SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
+        arena.addMethod(method);
+        Body incBody = Jimple.v().newBody(method);
+        method.setActiveBody(incBody);
+        ((JimpleBody)incBody).insertIdentityStmts();
+        return method;
+    }
+
+    public void processGladiatorClass(SootClass sc) {
+
+        // Set up the Arena zero-arg constructor 
 
-        mc.setApplicationClass();
-        SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
-        if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
-        Body mcInitBody = initMethod.getActiveBody();
-
-        mc.addMethod(method);
-        Body body = Jimple.v().newBody(method);
-        method.setActiveBody(body);
-        ((JimpleBody)body).insertIdentityStmts();
-        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);
-        assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
-
-        SootField sfr = new SootField(subname + "$$size", IntType.v());
-        mc.addField(sfr);
-        assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
-        
-        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()));
+        SootClass            arena = getArenaForGladiator(sc);
+        SootMethod arenaInitMethod = arena.getMethod("<init>", new LinkedList());
+        JimpleBody   arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
+
+        SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
+        arena.addField(maxField);
+        assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
+               arenaInitBody.getFirstNonIdentityStmt());
+
+        SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
+        arena.addField(sfr);
+        assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
+               arenaInitBody.getFirstNonIdentityStmt());
+
+        SootMethod incMethod = createIncMethod(sc);
+        Body         incBody = incMethod.getActiveBody();
+
+
+        // Now build the $$inc method
+
+        Local l  =  newLocal(incBody, IntType.v());
+        Local l2 =  newLocal(incBody, IntType.v());
+        Local l3 =  newLocal(incBody, IntType.v());
+        Local l4 =  newLocal(incBody, IntType.v());
+       
+        assign(incBody, l,                                 newIFR(incBody, sfr.makeRef()));
+        assign(incBody, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
+        assign(incBody, newIFR(incBody, sfr.makeRef()),       l2);
+        assign(incBody, l3,                                newIFR(incBody, maxField.makeRef()));
 
         Stmt returnStmt = Jimple.v().newReturnStmt(l2);
-        body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+        incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+
+        assign(incBody,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
+        assign(incBody,  newIFR(incBody, maxField.makeRef()), l4);
+
 
-        assign(body,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
-        assign(body,  newIFR(body, maxField.makeRef()), l4);
+        // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go
 
         for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
             SootField f = (SootField)it.next();
             Type t      = getSliceElementType(f.getType());
-            mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
+            arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
 
             Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
-            Local newArrLocal = newLocal(mcInitBody, f.getType());
-            mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
-            mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
+            Local newArrLocal = newLocal(arenaInitBody, f.getType());
+            arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(arenaInitBody, f.makeRef()), newArrLocal));
+            arenaInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
 
-            Local ll0 = newLocal(body, f.getType());
-            Local ll = newLocal(body, f.getType());
-            assign(body, ll0, newIFR(body,  f.makeRef()));
-            assign(body, ll,  Jimple.v().newNewArrayExpr(t, l4));
+            Local ll0 = newLocal(incBody, f.getType());
+            Local ll = newLocal(incBody, f.getType());
+            assign(incBody, ll0, newIFR(incBody,  f.makeRef()));
+            assign(incBody, ll,  Jimple.v().newNewArrayExpr(t, l4));
 
             List args = new LinkedList();
             args.add(ll0);
@@ -219,15 +220,15 @@ public class Main extends SceneTransformer {
             args.add(ll);
             args.add(IntConstant.v(0));
             args.add(l3);
-            body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
-            assign(body, newIFR(body,  f.makeRef()), ll);
+            incBody.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
+            assign(incBody, newIFR(incBody,  f.makeRef()), ll);
         }
 
         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
             if (!m.isConcrete()) continue;
             boolean doremove = true;
-            Body mbody = m.getActiveBody();
+            Body mincBody = m.getActiveBody();
             if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
                 doremove = false;
                 SootClass c = m.getDeclaringClass();
@@ -239,9 +240,9 @@ public class Main extends SceneTransformer {
                 bod.importBodyContentsFrom(m.getActiveBody());
                 nm.setActiveBody(bod);
                 m = nm;
-                mbody = bod;
-                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
-                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+                mincBody = bod;
+                mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
+                mincBody.getUnits().remove(((JimpleBody)mincBody).getFirstNonIdentityStmt());
             } else {
                 System.out.println("examining " + sc.getName() + "." + m.getSignature());
                 m.retrieveActiveBody();
@@ -257,7 +258,7 @@ public class Main extends SceneTransformer {
             getArenaForGladiator(sc).addMethod(m2);
 
             JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
-            ab.importBodyContentsFrom(mbody);
+            ab.importBodyContentsFrom(mincBody);
             m2.setActiveBody(ab);
             Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
             ab.getLocals().add(loc);
@@ -312,7 +313,7 @@ public class Main extends SceneTransformer {
             if (doremove) sc.removeMethod(m);
 
         }
-        body.getUnits().add(returnStmt);
+        incBody.getUnits().add(returnStmt);
     }
 
     // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
@@ -417,9 +418,9 @@ public class Main extends SceneTransformer {
                         NewExpr ne = (NewExpr)v;
                         if (implementsGladiator(ne.getBaseType())) {
                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
-                            SootClass mc = getArenaForGladiator(sc);
+                            SootClass arena = getArenaForGladiator(sc);
                             String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
-                            SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
+                            SootMethodRef smr = Scene.v().makeMethodRef(arena, incFuncName, new LinkedList(), IntType.v(), false);
                             Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
                             Local ll = viaLocal(invokeExpr, body, s);
                             vb.setValue(ll);
@@ -500,7 +501,7 @@ public class Main extends SceneTransformer {
                     } else if (v instanceof CastExpr) {
                         CastExpr ce = (CastExpr)v;
                         if (implementsGladiator(ce.getCastType())) {
-                            SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
+                            SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
                             ce.setCastType(ic.getType());
 
@@ -535,14 +536,14 @@ public class Main extends SceneTransformer {
                             vb.setValue(body.getThisLocal());
                             System.out.println("s is " + s);
                         } else if (implementsGladiator(fr.declaringClass())) {
-                            SootClass mc = getArenaForGladiator(fr.declaringClass());
+                            SootClass arena = getArenaForGladiator(fr.declaringClass());
                             InstanceFieldRef sfr = newIFR(body,  getSliceForField(fr));
                             vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
                         }
                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
                             SootClass tc = ((RefType)t).getSootClass();
-                            SootClass mc = getArenaForGladiator(tc);
-                            ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
+                            SootClass arena = getArenaForGladiator(tc);
+                            ifr.setFieldRef(Scene.v().makeFieldRef(arena, fr.name(), IntType.v(), false));
                         }
                     }