checkpoint
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
index 84e2841..abf7552 100644 (file)
@@ -30,7 +30,6 @@ public class Main extends SceneTransformer {
             StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
             while(st.hasMoreTokens()) {
                 String goo = st.nextToken();
-                System.out.println("goo " + goo);
                 File f = new File(goo);
                 if (!f.exists() || !f.isDirectory()) continue;
                 String[] jars = f.list();
@@ -48,7 +47,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";
@@ -59,79 +57,10 @@ public class Main extends SceneTransformer {
         soot.Main.main(args2);
     }
 
-    static int tfr = 0;
-
-    public static Local newLocal(Body b, Type t) {
-        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
-        b.getLocals().add(l);
-        return l;
-    }
-
-    public static Local viaLocal(Value v, Body b) {
-        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
-        b.getLocals().add(l);
-        if (v instanceof IdentityRef) {
-            b.getUnits().addFirst(Jimple.v().newIdentityStmt(l, v));
-        } else {
-            b.getUnits().addFirst(Jimple.v().newAssignStmt(l, v));
-        }
-        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);
-        if (v instanceof IdentityRef) {
-            b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
-        } else {
-            b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
-        }
-        return l;
-    }
-
-    HashMap map = new HashMap();
-    HashSet size_added = new HashSet();
-
-    public boolean   implementsGladiator(SootClass c)  { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
-    public SootField getSliceForField(SootField f)    { return getSliceForField(f.makeRef()); }
-    public SootField getSliceForField(SootFieldRef f) {
-        SootClass c  = f.declaringClass();
-        SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
-        String sig = f.declaringClass().getName()+"."+f.name();
-        if (map.get(sig) != null) return (SootField)map.get(sig);
-        Type t = f.type();
-        if (t instanceof RefType && implementsGladiator(((RefType)t).getSootClass())) t = IntType.v();
-        SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
-                                     t.makeArrayType(),
-                                     0);
-        oc.addField(nf);
-
-        Body body = getInitBody(oc);
-        Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
-        Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
-        body.getLocals().add(newArrLocal);
-        InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
-        body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
-        body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
-
-        map.put(sig, nf);
-        return nf;
-    }
-
-    public Body getInitBody(SootClass c) {
-        c.setApplicationClass();
-        List list = new LinkedList();
-        SootMethod m = c.getMethod("<init>", list);
-        if (!m.hasActiveBody()) {
-            JimpleBody b = (JimpleBody)Jimple.v().newBody(m);
-            m.setActiveBody(b);
-        }
-        return m.getActiveBody();
-    }
-
-    public boolean implementsGladiator(Type t) { return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass()); }
+    // 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();) {
             SootClass sc = (SootClass)it.next();
@@ -139,138 +68,205 @@ 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: " + getParent(sc).getName());
-                sc.setApplicationClass();
-                getParent(sc).setApplicationClass();
-                ac.add(sc);
-                ac.add(getParent(sc));
-                for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
-                    SootMethod m = (SootMethod)i2.next();
-                    if (m.isConcrete()) m.retrieveActiveBody();
-                }
-                for(Iterator i2 = getParent(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();
-            if (sc.isInterface()) continue;
-            if (!implementsGladiator(sc)) continue;
-            System.out.println("fixing Gladiator class: " + sc);
-            fixClass(sc);
+            if (sc.isInterface() || !implementsGladiator(sc)) continue;
+            processGladiatorClass(sc);
         }
         for(Iterator it = ac.iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
             if (sc.isInterface()) continue;
-            System.out.println("        updating class: " + sc);
-            nuke(sc);
+            processClass(sc);
         }
     }
 
-    public SootClass getParent(SootClass sc) {
-        return Scene.v().getSootClass(sc.getName().substring(0, sc.getName().lastIndexOf('$')));
+    // Initializers //////////////////////////////////////////////////////////////////////////////
+
+    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);
+    }
+
+    // Helpers //////////////////////////////////////////////////////////////////////////////
+
+    private int tfr = 0;
+    public Local newLocal(Body b, Type t) {
+        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
+        b.getLocals().add(l);
+        return l;
+    }
+
+    public Local viaLocal(Value v, Body b, Unit u) {
+        Local l = newLocal(b, v.getType());
+        if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
+        else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
+        return l;
+    }
+
+    boolean          implementsGladiator(SootClass c)  { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
+    boolean          implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
+    String           getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
+    String           getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
+    SootClass        getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
+    InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
+    void             assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
+    void             assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
+    Type             getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
+    SootFieldRef     getSliceForField(SootField f)    { return getSliceForField(f.makeRef()); }
+    SootFieldRef     getSliceForField(SootFieldRef f) {
+        return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
+                                      getGladiatorName(f.declaringClass())+"$$"+f.name(),
+                                      getSliceElementType(f.type()).makeArrayType(),
+                                      false);
     }
 
-    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());
-        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);
-
-        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);
-
-        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);
-        
-        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));
+
+    // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
+
+    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 
+
+        SootClass            arena = getArenaForGladiator(sc);
+        SootMethod arenaInitMethod = null;
+        for(Iterator it = arena.getMethods().iterator(); it.hasNext();) {
+            SootMethod m = (SootMethod)it.next();
+            if (m.getName().equals("<init>")) {
+                if (arenaInitMethod != null) throw new Error("class " + arena.getName() + " has two constructors");
+                arenaInitMethod = m;
+            }
+        }
+        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);
+        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);
+
+
+        // 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 = 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);
+            SootField f = (SootField)it.next();
+            Type t      = getSliceElementType(f.getType());
+            arena.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
+
+            Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
+            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(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);
             args.add(IntConstant.v(0));
             args.add(ll);
             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));
+            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;
+            if (!m.isConcrete() || !m.hasActiveBody()) continue;
             boolean doremove = true;
-            Body mbody = m.getActiveBody();
+            Body mincBody = m.getActiveBody();
             if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+                System.out.println("processing ctor " + sc.getName() + "." + m.getSignature());
                 doremove = false;
                 SootClass c = m.getDeclaringClass();
-                String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
+                String name = "$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);
                 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;
+
+                for(Iterator it2 = mincBody.getUnits().snapshotIterator(); it2.hasNext(); ) {
+                    Unit u = (Unit)it2.next();
+                    if (u instanceof DefinitionStmt) {
+                        DefinitionStmt ds = (DefinitionStmt)u;
+                        if (ds.getLeftOp() instanceof ThisRef)
+                            mincBody.getUnits().remove(u);
+                        else if (ds.getLeftOp() instanceof FieldRef) {
+                            if (((FieldRef)ds.getLeftOp()).getFieldRef().name().endsWith("this$0"))
+                                mincBody.getUnits().remove(u);
+                        }
+                    } else if (u instanceof InvokeStmt) {
+                        InvokeExpr ie = ((InvokeStmt)u).getInvokeExpr();
+                        SootMethodRef meth = ie.getMethodRef();
+                        if (meth.declaringClass().getName().equals("java.lang.Object") && meth.name().equals("<init>"))
+                            mincBody.getUnits().remove(u);
+                    }
+                }
+
             } else {
                 System.out.println("examining " + sc.getName() + "." + m.getSignature());
                 m.retrieveActiveBody();
@@ -281,14 +277,13 @@ public class Main extends SceneTransformer {
             List list = new LinkedList();
             list.addAll(m.getParameterTypes());
             list.add(IntType.v());
-            for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
             SootMethod m2 = new SootMethod(name, list, m.getReturnType());
-            getParent(sc).addMethod(m2);
+            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++), getParent(sc).getType());
+            Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
             ab.getLocals().add(loc);
             // FIXME: insert assignment to this
 
@@ -301,7 +296,7 @@ public class Main extends SceneTransformer {
 
             Chain units = ab.getUnits();
             boolean touched = false;
-            Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
+            Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
             ab.getLocals().add(loc0);
             for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
                 Stmt s = (Stmt) stmtIt.next();
@@ -312,7 +307,7 @@ public class Main extends SceneTransformer {
                         left.setType(IntType.v());
                         is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
                         if (!touched) {
-                            units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getParent(sc).getType())));
+                            units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
                             touched = true;
                         }
                     }
@@ -330,7 +325,6 @@ public class Main extends SceneTransformer {
                         }
                         */
                         if (vb.getValue() instanceof ThisRef) {
-                            System.out.println(s);
                             vb.setValue(loc);
                         }
                     }
@@ -341,10 +335,12 @@ public class Main extends SceneTransformer {
             if (doremove) sc.removeMethod(m);
 
         }
-        body.getUnits().add(stmt);
+        incBody.getUnits().add(returnStmt);
     }
 
-    public void nuke(SootClass c) {
+    // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
+
+    public void processClass(SootClass c) {
         for(Iterator it = c.getFields().iterator(); it.hasNext();) {
             SootField f = (SootField)it.next();
             Type t = f.getType();
@@ -367,7 +363,7 @@ public class Main extends SceneTransformer {
             SootMethod m = (SootMethod)it.next();
             Body b = null;
             if (m.getName().endsWith("$$inc")) continue;
-            if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
+            if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
             List l2 = new LinkedList();
             List l = m.getParameterTypes();
             for(Iterator it2 = l.iterator(); it2.hasNext();) {
@@ -402,18 +398,19 @@ public class Main extends SceneTransformer {
 
     }
 
-    protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
+    protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
         if (body instanceof JimpleBody) {
             JimpleBody b2 = Jimple.v().newBody(smeth);
             b2.importBodyContentsFrom(body);
             body = b2;
         }
         Chain units = body.getUnits();
-        System.out.println("fixBody("+body.getMethod()+")");
         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
             Local l = (Local)it.next();
             if (implementsGladiator(l.getType())) l.setType(IntType.v());
         }
+        if (!smeth.isStatic())
+            body.getThisLocal().setType(ownerClass.getType());
         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
             Stmt s = (Stmt) stmtIt.next();
             if (s instanceof DefinitionStmt) {
@@ -426,7 +423,9 @@ public class Main extends SceneTransformer {
                 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
                     ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
             List l = s.getUseAndDefBoxes();
-            for(Iterator it = l.iterator(); it.hasNext();) {
+            List l2l = new LinkedList();
+            l2l.addAll(l);
+            for(Iterator it = l2l.iterator(); it.hasNext();) {
                 Object o = it.next();
                 if (o instanceof ValueBox) {
                     ValueBox vb = (ValueBox)o;
@@ -444,14 +443,15 @@ public class Main extends SceneTransformer {
                         NewExpr ne = (NewExpr)v;
                         if (implementsGladiator(ne.getBaseType())) {
                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
-                            SootClass mc = getParent(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);
                             v = ll;
-                            continue;
+                            qq = 0;
+                            break;
                         } 
                     } else
 
@@ -474,10 +474,10 @@ public class Main extends SceneTransformer {
                                 //pl.remove(0);
                             }
                             pl.add(IntType.v());
-                            li.add(viaLocal(iie.getBase(),body,s));
+                            li.add(/*viaLocal(*/iie.getBase()/*,body,s)*/);
                             SootClass sc = mr.declaringClass();
                             name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
-                            mr = Scene.v().makeMethodRef(getParent(sc),
+                            mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
                                                          name,
                                                          pl,
                                                          implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
@@ -527,7 +527,7 @@ public class Main extends SceneTransformer {
                     } else if (v instanceof CastExpr) {
                         CastExpr ce = (CastExpr)v;
                         if (implementsGladiator(ce.getCastType())) {
-                            SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
+                            SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
                             ce.setCastType(ic.getType());
 
@@ -553,24 +553,33 @@ public class Main extends SceneTransformer {
                             break;
                         }
 
-                    } else if (v instanceof InstanceFieldRef) {
-                        InstanceFieldRef ifr = (InstanceFieldRef)v;
+                    } else if (v instanceof FieldRef) {
+                        FieldRef ifr = (FieldRef)v;
                         SootFieldRef fr = ifr.getFieldRef();
                         Type t = fr.type();
                         if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
-                            System.out.println("******************");
                             vb.setValue(body.getThisLocal());
-                            System.out.println("s is " + s);
                         } else if (implementsGladiator(fr.declaringClass())) {
-                            SootClass mc = getParent(fr.declaringClass());
-                            SootFieldRef sf = getSliceForField(fr).makeRef();
-                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
-                            vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
+                            SootClass arena = getArenaForGladiator(fr.declaringClass());
+                            if (fr.isStatic()) {
+                                vb.setValue(newIFR(body, getSliceForField(fr)));
+                            } else {
+                                InstanceFieldRef sfr = newIFR(body, getSliceForField(fr));
+                                vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ((InstanceFieldRef)ifr).getBase()));
+                            }
                         }
                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
                             SootClass tc = ((RefType)t).getSootClass();
-                            SootClass mc = getParent(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(), fr.isStatic()));
+                        } else if (t instanceof ArrayType) {
+                            ArrayType at = (ArrayType)t;
+                            Type et = at.getElementType();
+                            if (et instanceof RefType && implementsGladiator(((RefType)et).getSootClass()))
+                                ifr.setFieldRef(Scene.v().makeFieldRef(fr.declaringClass(),
+                                                                       fr.name(),
+                                                                       IntType.v().makeArrayType(),
+                                                                       fr.isStatic()));
                         }
                     }