- public void fixClass(SootClass sc) {
- SootClass mc = getParent(sc);
- 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);
-
- 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(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(Jimple.v().newInstanceFieldRef(body.getThisLocal(), maxField.makeRef()), l4));
+
+ // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
+
+ public SootMethod createIncMethod(SootClass sc) {
+ String incFuncName = getGladiatorName(sc) + "$$inc";
+ SootClass arena = getArenaForGladiator(sc);
+ SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
+ arena.addMethod(method);
+ Body incBody = Jimple.v().newBody(method);
+ method.setActiveBody(incBody);
+ ((JimpleBody)incBody).insertIdentityStmts();
+ return method;
+ }
+
+ public void processGladiatorClass(SootClass sc) {
+
+ // Set up the Arena zero-arg constructor
+
+ SootClass arena = getArenaForGladiator(sc);
+ SootMethod arenaInitMethod = null;
+ for(Iterator it = arena.getMethods().iterator(); it.hasNext();) {
+ SootMethod m = (SootMethod)it.next();
+ if (m.getName().equals("<init>")) {
+ if (arenaInitMethod != null) throw new Error("class " + arena.getName() + " has two constructors");
+ arenaInitMethod = m;
+ }
+ }
+ JimpleBody arenaInitBody = (JimpleBody)arenaInitMethod.getActiveBody();
+
+ SootField maxField = new SootField(getGladiatorName(sc) + "$$max", IntType.v());
+ arena.addField(maxField);
+ assign(arenaInitBody, newIFR(arenaInitBody, maxField.makeRef()), IntConstant.v(initialSize),
+ arenaInitBody.getFirstNonIdentityStmt());
+
+ SootField sfr = new SootField(getGladiatorName(sc) + "$$size", IntType.v());
+ arena.addField(sfr);
+ assign(arenaInitBody, newIFR(arenaInitBody, sfr.makeRef()), IntConstant.v(0),
+ arenaInitBody.getFirstNonIdentityStmt());
+
+ SootMethod incMethod = createIncMethod(sc);
+ Body incBody = incMethod.getActiveBody();
+
+
+ // Now build the $$inc method
+
+ Local l = newLocal(incBody, IntType.v());
+ Local l2 = newLocal(incBody, IntType.v());
+ Local l3 = newLocal(incBody, IntType.v());
+ Local l4 = newLocal(incBody, IntType.v());
+
+ assign(incBody, l, newIFR(incBody, sfr.makeRef()));
+ assign(incBody, l2, Jimple.v().newAddExpr(l, IntConstant.v(1)));
+ assign(incBody, newIFR(incBody, sfr.makeRef()), l2);
+ assign(incBody, l3, newIFR(incBody, maxField.makeRef()));
+
+ Stmt returnStmt = Jimple.v().newReturnStmt(l2);
+ incBody.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
+
+ assign(incBody, l4, Jimple.v().newShlExpr(l3, IntConstant.v(1)));
+ assign(incBody, newIFR(incBody, maxField.makeRef()), l4);
+
+
+ // Finally, iterate over the Gladiator's fields, updating the $$inc method and Arena's zero-arg constructor as we go