- 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);
- }