checkpoint
authoradam <adam@megacz.com>
Tue, 10 May 2005 07:22:24 +0000 (07:22 +0000)
committeradam <adam@megacz.com>
Tue, 10 May 2005 07:22:24 +0000 (07:22 +0000)
darcs-hash:20050510072224-5007d-069f5e2f276b8ab7850fd92a152eb4212487fbf3.gz

src/org/ibex/arenaj/Main.java

index b16ea28..cde945a 100644 (file)
@@ -126,7 +126,7 @@ public class Main extends SceneTransformer {
         return m.getActiveBody();
     }
 
-    public Local thisLocal(SootClass c, Body b) { return viaLocal(Jimple.v().newThisRef(c.getType()), b); }
+    public Local thisLocal(SootClass c, Body b) { return b.getThisLocal(); }
     public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
         SootClass mc = getParent(c);
         String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$size";
@@ -274,23 +274,27 @@ public class Main extends SceneTransformer {
         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
             SootMethod m = (SootMethod)it.next();
             if (!m.isConcrete()) continue;
+            boolean doremove = true;
+            Body mbody = m.getActiveBody();
             if (isGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
+                doremove = false;
                 SootClass c = m.getDeclaringClass();
-                String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$$$init";
+                String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
                 List li = m.getParameterTypes();
-                li.remove(0);
-                li.add(IntType.v());
+                //li.add(IntType.v());
                 c.removeMethod(m);
                 SootMethod nm = new SootMethod(name, li, isGladiatorType(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;
+                mbody = bod;
+                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+                mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
+            } 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();
@@ -302,7 +306,7 @@ public class Main extends SceneTransformer {
             getParent(sc).addMethod(m2);
 
             JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
-            ab.importBodyContentsFrom(m.getActiveBody());
+            ab.importBodyContentsFrom(mbody);
             m2.setActiveBody(ab);
             Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
             ab.getLocals().add(loc);
@@ -316,6 +320,9 @@ public class Main extends SceneTransformer {
             }
 
             Chain units = ab.getUnits();
+            boolean touched = false;
+            Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getParent(sc).getType());
+            ab.getLocals().add(loc0);
             for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
                 Stmt s = (Stmt) stmtIt.next();
                 if (s instanceof IdentityStmt) {
@@ -324,6 +331,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(getParent(sc).getType())));
+                            touched = true;
+                        }
                     }
                 }
 
@@ -331,6 +342,13 @@ public class Main extends SceneTransformer {
                     Object o = i.next();
                     if (o instanceof ValueBox) {
                         ValueBox vb = (ValueBox)o;
+                        o = vb.getValue();
+                        /*
+                        if (o instanceof Local && isGladiatorType(((Local)o).getType())) {
+                            System.out.println("thunking");
+                            vb.setValue(loc0);
+                        }
+                        */
                         if (vb.getValue() instanceof ThisRef) {
                             System.out.println(s);
                             vb.setValue(loc);
@@ -340,7 +358,7 @@ public class Main extends SceneTransformer {
 
             }
             
-            sc.removeMethod(m);
+            if (doremove) sc.removeMethod(m);
 
         }
         body.getUnits().add(stmt);
@@ -406,7 +424,6 @@ public class Main extends SceneTransformer {
     protected Body fixBody(Body body, SootClass ownerClass, SootMethod smeth) {
         if (body instanceof JimpleBody) {
             JimpleBody b2 = Jimple.v().newBody(smeth);
-            //b2.insertIdentityStmts();
             b2.importBodyContentsFrom(body);
             body = b2;
         }
@@ -424,6 +441,11 @@ public class Main extends SceneTransformer {
                     ds.getRightOpBox().setValue(IntConstant.v(-1));
                 }
             }
+            if (isGladiatorType(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();) {
                 Object o = it.next();
@@ -465,12 +487,14 @@ public class Main extends SceneTransformer {
                             List li = new LinkedList();
                             li.addAll(iie.getArgs());
                             LinkedList pl = new LinkedList();
-                            pl.addAll(mr.parameterTypes());
+                            for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
+                                Type t = (Type)it2.next();
+                                pl.add(isGladiatorType(t) ? IntType.v() : t);
+                            }
                             if (mr.name().equals("<init>") && isGladiator(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));
@@ -481,9 +505,10 @@ public class Main extends SceneTransformer {
                                                          pl,
                                                          isGladiatorType(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();) {
@@ -542,7 +567,10 @@ public class Main extends SceneTransformer {
                         InstanceFieldRef ifr = (InstanceFieldRef)v;
                         SootFieldRef fr = ifr.getFieldRef();
                         Type t = fr.type();
-                        if (isGladiatorFieldRef(fr)) {
+                        if (isGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
+                            System.out.println("******************");
+                            vb.setValue(body.getThisLocal());
+                        } else if (isGladiatorFieldRef(fr)) {
                             SootClass mc = getParent(fr.declaringClass());
                             SootFieldRef sf = getGladiatorField(fr).makeRef();
                             InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(thisLocal(mc, body), sf);
@@ -556,6 +584,7 @@ public class Main extends SceneTransformer {
                             ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
                         }
                     }
+
                 }
             }
         }