checkpoint
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
index b16ea28..abf7552 100644 (file)
@@ -7,7 +7,7 @@ import java.util.*;
 
 public class Main extends SceneTransformer {    
 
-    public static final int initialSize = 100;
+    public static final int initialSize = 1000;
 
     private static Main instance = new Main();
     private Main() { }
@@ -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,113 +57,10 @@ public class Main extends SceneTransformer {
         soot.Main.main(args2);
     }
 
-    static int tfr = 0;
-    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 isGladiator(SootClass c) { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
-    public boolean isGladiatorField(SootField f)
-        { return isGladiator(f.getDeclaringClass()) && !f.getName().equals("this") && f.getName().indexOf('$')==-1; }
-    public boolean isGladiatorFieldRef(SootFieldRef f) {
-        return isGladiator(f.declaringClass()) && !f.name().equals("this") && f.name().indexOf('$')==-1;
-    }
-    public SootField getGladiatorField(SootField f) { return getGladiatorField(f.makeRef()); }
-    public SootField getGladiatorField(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 && isGladiator(((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(thisLocal(c,body), 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 Local thisLocal(SootClass c, Body b) { return viaLocal(Jimple.v().newThisRef(c.getType()), b); }
-    public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
-        SootClass mc = getParent(c);
-        String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$size";
-        if (map.get(name) == null) {
-            SootField f = new SootField(name, IntType.v());
-            mc.addField(f);
-
-            Body body = getInitBody(mc);
-            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
-            body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
-
-            map.put(c, f);
-        }
-        return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
-    }
-    public SootFieldRef getGladiatorFieldMaxRef(SootClass c) {
-        SootClass mc = getParent(c);
-        String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$max";
-        if (map.get(name) == null) {
-            SootField f = new SootField(name, IntType.v());
-            mc.addField(f);
-
-            Body body = getInitBody(mc);
-            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(thisLocal(c,body), f.makeRef());
-            body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(initialSize)));
-
-            map.put(c, f);
-        }
-        return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
-    }
+    // The main loop //////////////////////////////////////////////////////////////////////////////
 
-    public boolean isGladiatorType(Type t) { return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass()); }
-
-
-    
-
-    boolean done = false;
     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();
@@ -173,149 +68,236 @@ public class Main extends SceneTransformer {
         }
         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
             SootClass sc = (SootClass)it.next();
-            if (isGladiator(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 (!isGladiator(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);
+    }
+
+
+    // 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 fixClass(SootClass sc) {
-        SootClass mc = getParent(sc);
-        String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
-        SootMethod method = new SootMethod(incFuncName, new LinkedList(),
-                                           IntType.v(), Modifier.PRIVATE,
-                                           new LinkedList());
-        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);
-        InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldSizeRef(sc));
-        body.getUnits().add(Jimple.v().newAssignStmt(l, sfr));
-        body.getUnits().add(Jimple.v().newAssignStmt(l2, Jimple.v().newAddExpr(l, IntConstant.v(1))));
-        InstanceFieldRef maxField = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldMaxRef(sc));
-        body.getUnits().add(Jimple.v().newAssignStmt(l3, maxField));
-        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(maxField, l4));
+    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 = getGladiatorField((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 (isGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+            if (!m.isConcrete() || !m.hasActiveBody()) continue;
+            boolean doremove = true;
+            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.remove(0);
-                li.add(IntType.v());
                 c.removeMethod(m);
-                SootMethod nm = new SootMethod(name, li, isGladiatorType(m.getReturnType()) ? IntType.v() : m.getReturnType());
+                SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
                 JimpleBody bod = Jimple.v().newBody(nm);
-                getParent(c).addMethod(nm);
-                //bod.insertIdentityStmts();
                 bod.importBodyContentsFrom(m.getActiveBody());
                 nm.setActiveBody(bod);
-                continue;
+                m = nm;
+                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();
             }
-            System.out.println("examining " + sc.getName() + "." + m.getSignature());
-            m.retrieveActiveBody();
             if (m.isStatic()) continue;
 
             String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
             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(m.getActiveBody());
+            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
 
             for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
                 Local loc2 = (Local)z.next();
-                if (isGladiatorType(loc2.getType())) {
+                if (implementsGladiator(loc2.getType())) {
                     loc2.setType(IntType.v());
                 }
             }
 
             Chain units = ab.getUnits();
+            boolean touched = false;
+            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();
                 if (s instanceof IdentityStmt) {
@@ -324,6 +306,10 @@ public class Main extends SceneTransformer {
                     if (is.getRightOp() instanceof ThisRef) {
                         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(getArenaForGladiator(sc).getType())));
+                            touched = true;
+                        }
                     }
                 }
 
@@ -331,8 +317,14 @@ public class Main extends SceneTransformer {
                     Object o = i.next();
                     if (o instanceof ValueBox) {
                         ValueBox vb = (ValueBox)o;
+                        o = vb.getValue();
+                        /*
+                        if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
+                            System.out.println("thunking");
+                            vb.setValue(loc0);
+                        }
+                        */
                         if (vb.getValue() instanceof ThisRef) {
-                            System.out.println(s);
                             vb.setValue(loc);
                         }
                     }
@@ -340,27 +332,29 @@ public class Main extends SceneTransformer {
 
             }
             
-            sc.removeMethod(m);
+            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();
             if (t instanceof RefType) {
                 RefType rt = (RefType)t;
                 SootClass sc = rt.getSootClass();
-                if (isGladiator(sc)) f.setType(IntType.v());
+                if (implementsGladiator(sc)) f.setType(IntType.v());
             } else if (t instanceof ArrayType) {
                 ArrayType at = (ArrayType)t;
                 t = at.getElementType();
                 if (!(t instanceof RefType)) continue;                
                 RefType rt = (RefType)t;
                 SootClass sc = rt.getSootClass();
-                if (isGladiator(sc)) f.setType(IntType.v().makeArrayType());
+                if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
             }
         }
 
@@ -368,15 +362,16 @@ public class Main extends SceneTransformer {
         for(Iterator it = list.iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
             Body b = null;
-            if (m.hasActiveBody()) b = fixBody(m.getActiveBody(), c, m);
+            if (m.getName().endsWith("$$inc")) continue;
+            if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
             List l2 = new LinkedList();
             List l = m.getParameterTypes();
             for(Iterator it2 = l.iterator(); it2.hasNext();) {
                 Type t = (Type)it2.next();
-                l2.add(isGladiatorType(t) ? IntType.v() : t);
+                l2.add(implementsGladiator(t) ? IntType.v() : t);
             }
             Type t = m.getReturnType();
-            if (isGladiatorType(t)) {
+            if (implementsGladiator(t)) {
                 t = IntType.v();
                 if (m.hasActiveBody()) {
                     Body bod = m.getActiveBody();
@@ -391,7 +386,7 @@ public class Main extends SceneTransformer {
                 }
             }
             String name = m.getName();
-            SootMethod meth = new SootMethod(name, l2, isGladiatorType(t) ? IntType.v() : t, m.getModifiers());
+            SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
             if (b != null) {
                 JimpleBody b2 = Jimple.v().newBody(meth);
                 b2.importBodyContentsFrom(b);
@@ -403,19 +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.insertIdentityStmts();
             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 (isGladiatorType(l.getType())) l.setType(IntType.v());
+            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) {
@@ -424,8 +419,13 @@ public class Main extends SceneTransformer {
                     ds.getRightOpBox().setValue(IntConstant.v(-1));
                 }
             }
+            if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
+                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;
@@ -433,26 +433,25 @@ public class Main extends SceneTransformer {
                     
                     if (v instanceof BinopExpr) {
                         BinopExpr boe = (BinopExpr)v;
-                        if (boe.getOp1().getType() instanceof PrimType && boe.getOp2().getType() instanceof NullType) {
-                            boe.setOp2(IntConstant.v(-1));
-                        }
-                        if (boe.getOp2().getType() instanceof PrimType && boe.getOp1().getType() instanceof NullType) {
-                            boe.setOp1(IntConstant.v(-1));
-                        }
+                        Type t1 = boe.getOp1().getType();
+                        Type t2 = boe.getOp2().getType();
+                        if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
+                        if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
                     }
 
                     if (v instanceof NewExpr) {
                         NewExpr ne = (NewExpr)v;
-                        if (isGladiatorType(ne.getBaseType())) {
+                        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);
-                            Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(thisLocal(mc,body), smr);
+                            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
 
@@ -460,40 +459,43 @@ public class Main extends SceneTransformer {
                         InvokeExpr ie = (InvokeExpr)v;
                         SootMethodRef mr = ie.getMethodRef();
                         String name = mr.name();
-                        if (v instanceof InstanceInvokeExpr && isGladiator(mr.declaringClass())) {
+                        if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
                             InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
                             List li = new LinkedList();
                             li.addAll(iie.getArgs());
                             LinkedList pl = new LinkedList();
-                            pl.addAll(mr.parameterTypes());
-                            if (mr.name().equals("<init>") && isGladiator(mr.declaringClass())) {
+                            for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
+                                Type t = (Type)it2.next();
+                                pl.add(implementsGladiator(t) ? IntType.v() : t);
+                            }
+                            if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
                                 name = "$init";
-                                li.remove(0);
-                                pl.remove(0);
-                                //pl.addFirst(body.getThisLocal());
+                                //li.remove(0);
+                                //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,
-                                                         isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+                                                         implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
                                                          false);
-                            ie = Jimple.v().newVirtualInvokeExpr(viaLocal(thisLocal(getParent(sc),body),body,s), mr, li);
+                            ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
                             vb.setValue(v = ie);
-                        } else {
+
+                        } else if (!(v instanceof StaticInvokeExpr)) {
                             List l0 = mr.parameterTypes();
                             List l2 = new LinkedList();
                             for(Iterator it2 = l0.iterator(); it2.hasNext();) {
                                 Type t = (Type)it2.next();
-                                l2.add(isGladiatorType(t) ? IntType.v() : t);
+                                l2.add(implementsGladiator(t) ? IntType.v() : t);
                             }
                             mr = Scene.v().makeMethodRef(mr.declaringClass(),
                                                          mr.name(),
                                                          l2,
-                                                         isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
+                                                         implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
                                                          mr.isStatic());
                             ie.setMethodRef(mr);
                             vb.setValue(v = ie);                            
@@ -524,38 +526,63 @@ public class Main extends SceneTransformer {
 
                     } else if (v instanceof CastExpr) {
                         CastExpr ce = (CastExpr)v;
-                        if (isGladiatorType(ce.getCastType())) {
-                            SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass());
+                        if (implementsGladiator(ce.getCastType())) {
+                            SootClass arena = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
                             ce.setCastType(ic.getType());
-                            // FIXME deal with null
+
+                            Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
+                            Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
+
+                            Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
+                            body.getUnits().insertBefore(s2, s);
+
+                            Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
+                            body.getUnits().insertAfter(isNull, s2);
+
+                            Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
+                            body.getUnits().insertAfter(ifStmt, isNull);
+
                             SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
-                            InvokeExpr iie = Jimple.v().newVirtualInvokeExpr(viaLocal(ce, body, s),
-                                                                             mr,
-                                                                             new LinkedList());
-                            vb.setValue(viaLocal(iie, body, s));
-                            qq = 0;
+                            Stmt isNotNull =
+                                Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
+                            body.getUnits().insertAfter(isNotNull, ifStmt);
+
+                            vb.setValue(l2);
+                            qq = 0;  // ???
                             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 (isGladiatorFieldRef(fr)) {
-                            SootClass mc = getParent(fr.declaringClass());
-                            SootFieldRef sf = getGladiatorField(fr).makeRef();
-                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(thisLocal(mc, body), sf);
-                            System.out.println("s is " + s);
-                            ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
-                            vb.setValue(ar);
+                        if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
+                            vb.setValue(body.getThisLocal());
+                        } else if (implementsGladiator(fr.declaringClass())) {
+                            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) && isGladiator(((RefType)t).getSootClass())) {
+                        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()));
                         }
                     }
+
                 }
             }
         }