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();
+ 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('$')));
- 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());
+ 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 = 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());
+ Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(100000));
+ 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(f, nf);
+ map.put(sig, nf);
return nf;
}
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)));
+
+ 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);
}
System.out.println("============= " + phaseName);
if (done) return;
done = true;
- nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test$Inner"));
- nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test"));
+ //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test$Inner"));
+ //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test"));
+ nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh"));
+ nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh$Triangle"));
}
public void nuke(SootClass c) {
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());
+ if (t instanceof RefType) {
+ RefType rt = (RefType)t;
+ SootClass sc = rt.getSootClass();
+ if (isGladiator(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());
+ }
}
List list = new LinkedList(); list.addAll(c.getMethods());
}
Type t = m.getReturnType();
c.removeMethod(m);
- SootMethod meth = new SootMethod(m.getName(), l2, isGladiatorType(t) ? IntType.v() : t, m.getModifiers());
+ if (isGladiatorType(t)) {
+ t = IntType.v();
+ Body bod = m.getActiveBody();
+ for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
+ Stmt s = (Stmt) stmtIt.next();
+ if (s instanceof ReturnStmt) {
+ if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
+ ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
+ }
+ }
+ }
+ }
+ SootMethod meth = new SootMethod(m.getName(), l2, t, m.getModifiers());
meth.setActiveBody(m.getActiveBody());
c.addMethod(meth);
}
protected Body fixBody(Body body) {
this.body = body;
Chain units = body.getUnits();
- Iterator stmtIt = units.snapshotIterator();
for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
Local l = (Local)it.next();
if (isGladiatorType(l.getType())) l.setType(IntType.v());
}
- while(stmtIt.hasNext()) {
+ for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
Stmt s = (Stmt) stmtIt.next();
List l = s.getUseAndDefBoxes();
+ if (s instanceof DefinitionStmt) {
+ DefinitionStmt ds = (DefinitionStmt)s;
+ if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
+ System.out.println("*************************");
+ ds.getRightOpBox().setValue(IntConstant.v(-1));
+ }
+ }
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 BinopExpr) {
+ BinopExpr boe = (BinopExpr)v;
+ if (boe.getOp1().getType() instanceof PrimType && boe.getOp2().getType() instanceof NullType) {
+ System.out.println("*************************");
+ boe.setOp2(IntConstant.v(-1));
+ }
+ if (boe.getOp2().getType() instanceof PrimType && boe.getOp1().getType() instanceof NullType) {
+ System.out.println("*************************");
+ boe.setOp1(IntConstant.v(-1));
+ }
+ }
if (v instanceof InvokeExpr) {
InvokeExpr ie = (InvokeExpr)v;
body.getUnits().remove(s);
break;
}
- ie.setMethodRef(convert(mr));
+ mr = convert(mr);
+ ie.setMethodRef(mr);
+ for(int i=0; i<ie.getArgCount(); i++) {
+ ValueBox b = ie.getArgBox(i);
+ Value val = b.getValue();
+ if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
+ SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
+ List typelist = new LinkedList();
+ typelist.add(IntType.v());
+ SootMethod intMethod = intClass.getMethod("<init>", typelist);
+ Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
+ List list = new LinkedList();
+ list.add(val);
+ units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
+ intMethod.makeRef(),
+ list)),
+ s);
+ b.setValue(loc);
+ }
+ if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
+ b.setValue(IntConstant.v(-1));
+ }
+ }
} else if (v instanceof NewExpr) {
NewExpr ne = (NewExpr)v;
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();
+ } else if (v instanceof CastExpr) {
+ CastExpr ce = (CastExpr)v;
+ if (isGladiatorType(ce.getCastType())) {
+ //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
+ //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
SootClass mc = Scene.v().getMainClass();
- SootFieldRef fr = Scene.v().makeFieldRef(mc, "z", IntType.v(), false);
- ifr.setFieldRef(fr);
+ LinkedList list = new LinkedList();
+ list.add(Scene.v().getSootClass("java.lang.Object").getType());
+ SootMethodRef mr = Scene.v().makeMethodRef(mc, "zap", list, IntType.v(), true);
+ Local loc = viaLocal(ce.getOp(), body, s);
+ List list2 = new LinkedList();
+ list2.add(loc);
+ vb.setValue(viaLocal(Jimple.v().newStaticInvokeExpr(mr, list2), body, s));
}
- SootField f = ifr.getField();
- if (isGladiatorField(f)) {
- f = getGladiatorField(f);
+
+ } else if (v instanceof InstanceFieldRef) {
+ InstanceFieldRef ifr = (InstanceFieldRef)v;
+ SootFieldRef fr = ifr.getFieldRef();
+ Type t = fr.type();
+ if (isGladiatorFieldRef(fr)) {
SootClass mc = Scene.v().getMainClass();
- SootField sf = mc.getField(f.getName(), f.getType());
- InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf.makeRef());
+ SootFieldRef sf = getGladiatorField(fr).makeRef();
+ InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
vb.setValue(ar);
}
+ if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
+ SootClass tc = ((RefType)t).getSootClass();
+ SootClass mc = Scene.v().getMainClass();
+ ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
+ }
}
}
}