checkpoint
authoradam <adam@megacz.com>
Tue, 3 May 2005 08:45:47 +0000 (08:45 +0000)
committeradam <adam@megacz.com>
Tue, 3 May 2005 08:45:47 +0000 (08:45 +0000)
darcs-hash:20050503084547-5007d-c855172f9b9ba198841d4737867633ccb7adb101.gz

src/edu/berkeley/cs/megacz/Test.java
src/edu/berkeley/cs/megacz/Transformer.java

index 8304159..a219300 100644 (file)
@@ -2,19 +2,22 @@ package edu.berkeley.cs.megacz;
 
 public class Test {
     public static void main(String[] s) {
+        new Test();
     }
 
-    private class Inner {
+    private class Inner implements Gladiator {
         public int foo;
     }
 
-    //public int arr_foo[] = null;
-    public static int[] arr_foo = null;
-    Inner z = new Inner();
-    
+    Inner z;
+
+    public void bar(Inner x) {
+    }
 
     public Test() {
-        z.foo = 3;
-        System.out.println(z.foo);
+        z = new Inner();
+        Inner q = z;
+        q.foo = 3;
+        System.out.println(q.foo);
     }
 }
index 6c52753..513e0fa 100644 (file)
@@ -22,46 +22,142 @@ public class Transformer extends BodyTransformer {
         soot.Main.main(args);
     }
 
-    public static Local viaLocal(Value v, Body b, Chain c, Unit u) {
-        Local l = Jimple.v().newLocal("tmpRef", v.getType());
+    static int tfr = 0;
+    public static Local viaLocal(Value v, Body b, Unit u) {
+        Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
         b.getLocals().add(l);
-        c.insertBefore(Jimple.v().newAssignStmt(l, v), u);
+        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("edu.berkeley.cs.megacz.Gladiator"); }
+    public boolean isGladiatorField(SootField f)
+        { return isGladiator(f.getDeclaringClass()) && !f.getName().equals("this") && f.getName().indexOf('$')==-1; }
+    public SootField getGladiatorField(SootField f) {
+        SootClass c  = f.getDeclaringClass();
+        SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
+        if (map.get(f) != null) return (SootField)map.get(f);
+        SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.getName(),
+                                     f.getType().makeArrayType(),
+                                     f.getModifiers());
+        oc.addField(nf);
+        Body body = oc.getMethod("<init>", new LinkedList()).getActiveBody();
+        Expr newArr = Jimple.v().newNewArrayExpr(f.getType(), IntConstant.v(1000));
+        Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.getType().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(f, nf);
+        return nf;
+    }
+
+    public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
+        SootClass mc = Scene.v().getMainClass();
+        String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "_size";
+        if (map.get(c) == null) {
+            SootField f = new SootField(name, IntType.v());
+            mc.addField(f);
+        Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
+        InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), 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 boolean isGladiatorType(Type t) {
+        return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass());
+    }
+
     protected void internalTransform(Body body, String phaseName, Map options) {
-        SootClass sClass = body.getMethod().getDeclaringClass();
-        SootField gotoCounter = null;
-        boolean addedLocals = false;
-        Local tmpRef = null, tmpLong = null;
+        SootClass c = body.getMethod().getDeclaringClass();
+
+        for(Iterator it = c.getFields().iterator(); it.hasNext();) {
+            SootField f = (SootField)it.next();
+            Type t = f.getType();
+            if (!(t instanceof RefType)) continue;
+            if (isGladiator(((RefType)t).getSootClass())) f.setType(IntType.v());
+        }
+
+        for(Iterator it = c.methodIterator(); it.hasNext();) {
+            SootMethod m = (SootMethod)it.next();
+            System.out.println(m.getName() + " -- " + m.getActiveBody());
+            /*
+            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);
+            }
+            m.setParameterTypes(l2);
+            Type t = m.getReturnType();
+            m.setReturnType(isGladiatorType(t) ? IntType.v() : t);
+            */
+        }
+
+        for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
+            Local l = (Local)it.next();
+            if (isGladiatorType(l.getType())) l.setType(IntType.v());
+        }
+
         Chain units = body.getUnits();
-        
-        boolean isMainMethod = body.getMethod().getSubSignature().equals("void main(java.lang.String[])");
         Iterator stmtIt = units.snapshotIterator();
         while(stmtIt.hasNext()) {
             Stmt s = (Stmt) stmtIt.next();
-            //if (isMainMethod) {
             List l = s.getUseAndDefBoxes();
             for(Iterator it = l.iterator(); it.hasNext();) {
                 Object o = it.next();
                 if (o instanceof ValueBox) {
                     ValueBox vb = (ValueBox)o;
                     Value v = vb.getValue();
-                    if (v instanceof CastExpr) {
-                        CastExpr ce = (CastExpr)v;
-                        //System.out.println(">>> cast " + ce.getOp().getType() + " -> " + ce.getCastType());
+
+                    if (v instanceof InvokeExpr) {
+                        InvokeExpr ie = (InvokeExpr)v;
+                        SootMethod m = ie.getMethod();
+                        if (isGladiator(m.getDeclaringClass())) {
+                            body.getUnits().remove(s);
+                            break;
+                        }
+
+                    } else if (v instanceof NewExpr) {
+                        NewExpr ne = (NewExpr)v;
+                        if (isGladiatorType(ne.getBaseType())) {
+                            System.out.println("******");
+                            SootClass mc = Scene.v().getMainClass();
+                            SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
+                            System.out.println(sc);
+                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(),
+                                                                                  getGladiatorFieldSizeRef(sc));
+                            Local ll = viaLocal(sfr, body, s);
+                            Local ll2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
+                            body.getLocals().add(ll2);
+                            Stmt stmt = Jimple.v().newAssignStmt(ll2, Jimple.v().newAddExpr(ll, IntConstant.v(1)));
+                            units.insertBefore(stmt, s);
+                            units.insertAfter(Jimple.v().newAssignStmt(sfr, ll2), stmt);
+                            vb.setValue(ll);
+                        }
+
                     } else if (v instanceof InstanceFieldRef) {
                         InstanceFieldRef ifr = (InstanceFieldRef)v;
+                        Type t = ifr.getType();
+                        if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
+                            SootClass tc = ((RefType)t).getSootClass();
+                            SootClass mc = Scene.v().getMainClass();
+                            SootFieldRef fr = Scene.v().makeFieldRef(mc, "z", IntType.v(), false);
+                            ifr.setFieldRef(fr);
+                        }
                         SootField f = ifr.getField();
-                        System.out.println(">>> getField " + f);
-                        if (f.getName().equals("foo")) {
+                        if (isGladiatorField(f)) {
+                            f = getGladiatorField(f);
                             SootClass mc = Scene.v().getMainClass();
-                            Type arrType = f.getType().makeArrayType();
-                            SootField sf = mc.getField("arr_" + f.getName(), arrType);
-                            StaticFieldRef sfr = Jimple.v().newStaticFieldRef(sf.makeRef());
-                            ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, units, s), IntConstant.v(3));
-                            System.out.println(">>>>> " + ar.getType());
-                            System.out.println(">>>>> " + v.getType());
+                            SootField sf = mc.getField(f.getName(), f.getType());
+                            InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf.makeRef());
+                            ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
                             vb.setValue(ar);
                         }
                     }