From 9c9f43fda59d062c8fc037a32657ade57d4e91ed Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 19 May 2005 02:12:10 +0000 Subject: [PATCH] checkpoint darcs-hash:20050519021210-5007d-3beeb2dedf2a092525f636b6a67a65120341e988.gz --- src/org/ibex/arenaj/Main.java | 177 ++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 101 deletions(-) diff --git a/src/org/ibex/arenaj/Main.java b/src/org/ibex/arenaj/Main.java index a18e9aa..84e2841 100644 --- a/src/org/ibex/arenaj/Main.java +++ b/src/org/ibex/arenaj/Main.java @@ -7,7 +7,7 @@ import java.util.*; public class Main extends SceneTransformer { - public static final int initialSize = 10000; + public static final int initialSize = 1000; private static Main instance = new Main(); private Main() { } @@ -60,6 +60,13 @@ public class Main extends SceneTransformer { } 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); @@ -84,20 +91,15 @@ public class Main extends SceneTransformer { 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) { + 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 && isGladiator(((RefType)t).getSootClass())) t = IntType.v(); + 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); @@ -107,7 +109,7 @@ public class Main extends SceneTransformer { 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()); + 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)); @@ -126,44 +128,8 @@ public class Main extends SceneTransformer { return m.getActiveBody(); } - 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"; - if (map.get(name) == null) { - SootField f = new SootField(name, IntType.v()); - mc.addField(f); + public boolean implementsGladiator(Type t) { return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass()); } - 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); - } - - 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"); List ac = new LinkedList(); @@ -173,7 +139,7 @@ public class Main extends SceneTransformer { } for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) { SootClass sc = (SootClass)it.next(); - if (isGladiator(sc)) { + if (implementsGladiator(sc)) { System.out.println(" application class: " + sc.getName()); System.out.println(" application class: " + getParent(sc).getName()); sc.setApplicationClass(); @@ -193,7 +159,7 @@ public class Main extends SceneTransformer { for(Iterator it = ac.iterator(); it.hasNext();) { SootClass sc = (SootClass)it.next(); if (sc.isInterface()) continue; - if (!isGladiator(sc)) continue; + if (!implementsGladiator(sc)) continue; System.out.println("fixing Gladiator class: " + sc); fixClass(sc); } @@ -211,36 +177,49 @@ public class Main extends SceneTransformer { public void fixClass(SootClass sc) { SootClass mc = getParent(sc); - String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc"; + 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); - InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldSizeRef(sc)); - InstanceFieldRef maxField = Jimple.v().newInstanceFieldRef(body.getThisLocal(), getGladiatorFieldMaxRef(sc)); - body.getUnits().add(Jimple.v().newAssignStmt(l, sfr)); + 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(sfr, l2)); - body.getUnits().add(Jimple.v().newAssignStmt(l3, maxField)); + 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(maxField, l4)); + body.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newInstanceFieldRef(body.getThisLocal(), maxField.makeRef()), l4)); for(Iterator it = sc.getFields().iterator(); it.hasNext();) { - SootField f = getGladiatorField((SootField)it.next()); + 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); @@ -277,14 +256,14 @@ public class Main extends SceneTransformer { if (!m.isConcrete()) continue; boolean doremove = true; Body mbody = m.getActiveBody(); - if (isGladiator(m.getDeclaringClass()) && m.getName().equals("")) { + if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("")) { doremove = false; SootClass c = m.getDeclaringClass(); String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init"; List li = m.getParameterTypes(); //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); bod.importBodyContentsFrom(m.getActiveBody()); nm.setActiveBody(bod); @@ -315,7 +294,7 @@ public class Main extends SceneTransformer { 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()); } } @@ -345,7 +324,7 @@ public class Main extends SceneTransformer { ValueBox vb = (ValueBox)o; o = vb.getValue(); /* - if (o instanceof Local && isGladiatorType(((Local)o).getType())) { + if (o instanceof Local && implementsGladiator(((Local)o).getType())) { System.out.println("thunking"); vb.setValue(loc0); } @@ -372,14 +351,14 @@ public class Main extends SceneTransformer { 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()); } } @@ -387,15 +366,16 @@ public class Main extends SceneTransformer { for(Iterator it = list.iterator(); it.hasNext();) { SootMethod m = (SootMethod)it.next(); Body b = null; + if (m.getName().endsWith("$$inc")) continue; if (m.hasActiveBody()) b = fixBody(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(); @@ -410,7 +390,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); @@ -432,7 +412,7 @@ public class Main extends SceneTransformer { 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()); } for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) { Stmt s = (Stmt) stmtIt.next(); @@ -442,11 +422,9 @@ 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) { + 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();) { Object o = it.next(); @@ -456,22 +434,20 @@ 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); 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); + Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr); Local ll = viaLocal(invokeExpr, body, s); vb.setValue(ll); v = ll; @@ -483,16 +459,16 @@ 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(); for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) { Type t = (Type)it2.next(); - pl.add(isGladiatorType(t) ? IntType.v() : t); + pl.add(implementsGladiator(t) ? IntType.v() : t); } - if (mr.name().equals("") && isGladiator(mr.declaringClass())) { + if (mr.name().equals("") && implementsGladiator(mr.declaringClass())) { name = "$init"; //li.remove(0); //pl.remove(0); @@ -504,7 +480,7 @@ public class Main extends SceneTransformer { mr = Scene.v().makeMethodRef(getParent(sc), name, pl, - isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(), + implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(), false); ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li); vb.setValue(v = ie); @@ -514,12 +490,12 @@ public class Main extends SceneTransformer { 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); @@ -550,7 +526,7 @@ public class Main extends SceneTransformer { } else if (v instanceof CastExpr) { CastExpr ce = (CastExpr)v; - if (isGladiatorType(ce.getCastType())) { + if (implementsGladiator(ce.getCastType())) { SootClass mc = getParent(((RefType)ce.getCastType()).getSootClass()); SootClass ic = Scene.v().getSootClass("java.lang.Integer"); ce.setCastType(ic.getType()); @@ -581,18 +557,17 @@ public class Main extends SceneTransformer { InstanceFieldRef ifr = (InstanceFieldRef)v; SootFieldRef fr = ifr.getFieldRef(); Type t = fr.type(); - if (isGladiator(fr.declaringClass()) && fr.name().equals("this$0")) { + if (implementsGladiator(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); System.out.println("s is " + s); - ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()); - vb.setValue(ar); + } 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())); } - 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)); -- 1.7.10.4