ea31f02fb2dab10b49717d6fc6e6495b904f6c53
[org.ibex.arenaj.git] / src / org / ibex / arenaj / Main.java
1 package org.ibex.arenaj;
2 import soot.*;
3 import soot.jimple.*;
4 import soot.util.*;
5 import java.io.*;
6 import java.util.*;
7
8 public class Main extends SceneTransformer {    
9
10     public static final int initialSize = 1000;
11
12     private static Main instance = new Main();
13     private Main() { }
14     public static Main v() { return instance; }
15    
16     public static void main(String[] args) throws Exception {
17         if(args.length == 0) {
18             System.out.println("Syntax: java " + v().getClass().getName() + " [soot options]");
19             System.exit(0);
20         }
21         PackManager.v().getPack("wjtp").add(new Transform("wjtp.tx", Main.v()));
22         String[] args2 = new String[args.length + 14];
23         System.arraycopy(args, 0, args2, 0, args.length-1);
24         String sootcp =
25             System.getProperty("java.class.path") + 
26             File.pathSeparator +
27             System.getProperty("sun.boot.class.path");
28         String extDirs = System.getProperty("java.ext.dirs");
29         if (extDirs != null) {
30             StringTokenizer st = new StringTokenizer(extDirs, File.pathSeparatorChar+"");
31             while(st.hasMoreTokens()) {
32                 String goo = st.nextToken();
33                 System.out.println("goo " + goo);
34                 File f = new File(goo);
35                 if (!f.exists() || !f.isDirectory()) continue;
36                 String[] jars = f.list();
37                 for(int i=0; i<jars.length; i++)
38                     sootcp += File.pathSeparator + f.getPath() + File.separatorChar + jars[i];
39             }
40         }
41         args2[args.length - 1] = "-w";
42         args2[args.length + 0] = "-p";
43         args2[args.length + 1] = "cg";
44         args2[args.length + 2] = "enabled:false";
45         args2[args.length + 3] = "-p";
46         args2[args.length + 4] = "wjap";
47         args2[args.length + 5] = "enabled:false";
48         args2[args.length + 6] = "-p";
49         args2[args.length + 7] = "jtp";
50         args2[args.length + 8] = "enabled:false";
51         //args2[args.length + 9] = "-keep-line-number";
52         args2[args.length + 9] = "-allow-phantom-refs";
53         args2[args.length + 10] = "-allow-phantom-refs";
54         args2[args.length + 11] = "-cp";
55         args2[args.length + 12] = sootcp;
56         System.out.println("sootcp => " + sootcp);
57         args2[args.length + 13] = args[args.length-1];
58         for(int i=0; i<args2.length; i++) System.out.println("args2["+i+"] = " + args2[i]);
59         soot.Main.main(args2);
60     }
61
62     // The main loop //////////////////////////////////////////////////////////////////////////////
63
64     public void internalTransform(String phaseName, Map options) {
65         System.out.println("begun");
66         init();
67         List ac = new LinkedList();
68         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
69             SootClass sc = (SootClass)it.next();
70             sc.setLibraryClass();
71         }
72         for(Iterator it = Scene.v().getClasses().iterator(); it.hasNext();) {
73             SootClass sc = (SootClass)it.next();
74             if (implementsGladiator(sc)) {
75                 System.out.println("  application class: " + sc.getName());
76                 System.out.println("  application class: " + getArenaForGladiator(sc).getName());
77                 sc.setApplicationClass();
78                 getArenaForGladiator(sc).setApplicationClass();
79                 ac.add(sc);
80                 ac.add(getArenaForGladiator(sc));
81                 /*
82                 for(Iterator i2 = sc.getMethods().iterator(); i2.hasNext();) {
83                     SootMethod m = (SootMethod)i2.next();
84                     if (m.isConcrete()) m.retrieveActiveBody();
85                 }
86                 for(Iterator i2 = getArenaForGladiator(sc).getMethods().iterator(); i2.hasNext();) {
87                     SootMethod m = (SootMethod)i2.next();
88                     if (m.isConcrete()) m.retrieveActiveBody();
89                 }
90                 */
91             }
92         }
93         for(Iterator it = ac.iterator(); it.hasNext();) {
94             SootClass sc = (SootClass)it.next();
95             if (sc.isInterface() || !implementsGladiator(sc)) continue;
96             processGladiatorClass(sc);
97         }
98         for(Iterator it = ac.iterator(); it.hasNext();) {
99             SootClass sc = (SootClass)it.next();
100             if (sc.isInterface()) continue;
101             processClass(sc);
102         }
103     }
104
105     // Initializers //////////////////////////////////////////////////////////////////////////////
106
107     public Type objectType;
108     public List arrayCopyTypes = new LinkedList();
109     public SootMethodRef arrayCopy;
110     public void init() {
111         objectType = Scene.v().getSootClass("java.lang.Object").getType();
112         arrayCopyTypes.add(objectType);
113         arrayCopyTypes.add(IntType.v());
114         arrayCopyTypes.add(objectType);
115         arrayCopyTypes.add(IntType.v());
116         arrayCopyTypes.add(IntType.v());
117         arrayCopy = 
118             Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.System"),
119                                     "arraycopy",
120                                     arrayCopyTypes,
121                                     VoidType.v(),
122                                     true);
123     }
124
125     // Helpers //////////////////////////////////////////////////////////////////////////////
126
127     private int tfr = 0;
128     public Local newLocal(Body b, Type t) {
129         Local l = Jimple.v().newLocal("tmpRef" + (tfr++), t);
130         b.getLocals().add(l);
131         return l;
132     }
133
134     public Local viaLocal(Value v, Body b, Unit u) {
135         Local l = newLocal(b, v.getType());
136         if (v instanceof IdentityRef) b.getUnits().insertBefore(Jimple.v().newIdentityStmt(l, v), u);
137         else b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
138         return l;
139     }
140
141     boolean          implementsGladiator(SootClass c)  { return c.implementsInterface("org.ibex.arenaj.Gladiator"); }
142     boolean          implementsGladiator(Type t) {return (t instanceof RefType)&&implementsGladiator(((RefType)t).getSootClass());}
143     String           getArenaName(SootClass sc) { return sc.getName().substring(0, sc.getName().lastIndexOf('$')); }
144     String           getGladiatorName(SootClass sc) { return sc.getName().substring(sc.getName().lastIndexOf('$')+1); }
145     SootClass        getArenaForGladiator(SootClass sc) { return Scene.v().getSootClass(getArenaName(sc)); }
146     InstanceFieldRef newIFR(Body b, SootFieldRef fr) { return Jimple.v().newInstanceFieldRef(b.getThisLocal(), fr); }
147     void             assign(Body b, Value left, Value right) { b.getUnits().add(Jimple.v().newAssignStmt(left, right)); }
148     void             assign(Body b, Value l, Value r, Stmt w) { b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, r), w); }
149     Type             getSliceElementType(Type t) { return implementsGladiator(t) ? IntType.v() : t; }
150     SootFieldRef     getSliceForField(SootField f)    { return getSliceForField(f.makeRef()); }
151     SootFieldRef     getSliceForField(SootFieldRef f) {
152         return Scene.v().makeFieldRef(getArenaForGladiator(f.declaringClass()),
153                                       getGladiatorName(f.declaringClass())+"$$"+f.name(),
154                                       getSliceElementType(f.type()).makeArrayType(),
155                                       false);
156     }
157
158
159     // Operations performed on the Gladiator class //////////////////////////////////////////////////////////////////////////
160
161     public void processGladiatorClass(SootClass sc) {
162         SootClass mc = getArenaForGladiator(sc);
163         String subname = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1);
164         String incFuncName = subname + "$$inc";
165         SootMethod method = new SootMethod(incFuncName, new LinkedList(), IntType.v(), Modifier.PRIVATE, new LinkedList());
166
167         mc.setApplicationClass();
168         SootMethod initMethod = mc.getMethod("<init>", new LinkedList());
169         if (!initMethod.hasActiveBody()) initMethod.setActiveBody((JimpleBody)Jimple.v().newBody(initMethod));
170         Body mcInitBody = initMethod.getActiveBody();
171
172         mc.addMethod(method);
173         Body body = Jimple.v().newBody(method);
174         method.setActiveBody(body);
175         ((JimpleBody)body).insertIdentityStmts();
176         Local l  =  newLocal(body, IntType.v());
177         Local l2 =  newLocal(body, IntType.v());
178         Local l3 =  newLocal(body, IntType.v());
179         Local l4 =  newLocal(body, IntType.v());
180
181         Stmt where = ((JimpleBody)mcInitBody).getFirstNonIdentityStmt();
182         SootField maxField = new SootField(subname + "$$max", IntType.v());
183         mc.addField(maxField);
184         assign(mcInitBody, newIFR(mcInitBody, maxField.makeRef()), IntConstant.v(initialSize), where);
185
186         SootField sfr = new SootField(subname + "$$size", IntType.v());
187         mc.addField(sfr);
188         assign(mcInitBody, newIFR(mcInitBody, sfr.makeRef()), IntConstant.v(0), where);
189         
190         assign(body, l,                                 newIFR(body, sfr.makeRef()));
191         assign(body, l2,                                Jimple.v().newAddExpr(l, IntConstant.v(1)));
192         assign(body, newIFR(body, sfr.makeRef()),       l2);
193         assign(body, l3,                                newIFR(body, maxField.makeRef()));
194
195         Stmt returnStmt = Jimple.v().newReturnStmt(l2);
196         body.getUnits().add(Jimple.v().newIfStmt(Jimple.v().newLtExpr(l2, l3), returnStmt));
197
198         assign(body,  l4,                               Jimple.v().newShlExpr(l3, IntConstant.v(1)));
199         assign(body,  newIFR(body, maxField.makeRef()), l4);
200
201         for(Iterator it = sc.getFields().iterator(); it.hasNext();) {
202             SootField f = (SootField)it.next();
203             Type t      = getSliceElementType(f.getType());
204             mc.addField(f = new SootField(getGladiatorName(sc) + "$$" + f.getName(), t.makeArrayType(), 0));
205
206             Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(initialSize));
207             Local newArrLocal = newLocal(mcInitBody, f.getType());
208             mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newIFR(mcInitBody, f.makeRef()), newArrLocal));
209             mcInitBody.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
210
211             Local ll0 = newLocal(body, f.getType());
212             Local ll = newLocal(body, f.getType());
213             assign(body, ll0, newIFR(body,  f.makeRef()));
214             assign(body, ll,  Jimple.v().newNewArrayExpr(t, l4));
215
216             List args = new LinkedList();
217             args.add(ll0);
218             args.add(IntConstant.v(0));
219             args.add(ll);
220             args.add(IntConstant.v(0));
221             args.add(l3);
222             body.getUnits().add(Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(arrayCopy, args)));
223             assign(body, newIFR(body,  f.makeRef()), ll);
224         }
225
226         for(Iterator it = sc.getMethods().iterator(); it.hasNext();) {
227             SootMethod m = (SootMethod)it.next();
228             if (!m.isConcrete()) continue;
229             boolean doremove = true;
230             Body mbody = m.getActiveBody();
231             if (implementsGladiator(m.getDeclaringClass()) && m.getName().equals("<init>")) {
232                 doremove = false;
233                 SootClass c = m.getDeclaringClass();
234                 String name = /*c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + */"$init";
235                 List li = m.getParameterTypes();
236                 c.removeMethod(m);
237                 SootMethod nm = new SootMethod(name, li, implementsGladiator(m.getReturnType()) ? IntType.v() : m.getReturnType());
238                 JimpleBody bod = Jimple.v().newBody(nm);
239                 bod.importBodyContentsFrom(m.getActiveBody());
240                 nm.setActiveBody(bod);
241                 m = nm;
242                 mbody = bod;
243                 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
244                 mbody.getUnits().remove(((JimpleBody)mbody).getFirstNonIdentityStmt());
245             } else {
246                 System.out.println("examining " + sc.getName() + "." + m.getSignature());
247                 m.retrieveActiveBody();
248             }
249             if (m.isStatic()) continue;
250
251             String name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + m.getName();
252             List list = new LinkedList();
253             list.addAll(m.getParameterTypes());
254             list.add(IntType.v());
255             for(Iterator i = list.iterator(); i.hasNext();) System.out.println(i.next());
256             SootMethod m2 = new SootMethod(name, list, m.getReturnType());
257             getArenaForGladiator(sc).addMethod(m2);
258
259             JimpleBody ab = (JimpleBody)Jimple.v().newBody(m2);
260             ab.importBodyContentsFrom(mbody);
261             m2.setActiveBody(ab);
262             Local loc = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
263             ab.getLocals().add(loc);
264             // FIXME: insert assignment to this
265
266             for(Iterator z = ab.getLocals().iterator(); z.hasNext();) {
267                 Local loc2 = (Local)z.next();
268                 if (implementsGladiator(loc2.getType())) {
269                     loc2.setType(IntType.v());
270                 }
271             }
272
273             Chain units = ab.getUnits();
274             boolean touched = false;
275             Local loc0 = Jimple.v().newLocal("tmpRef" + (tfr++), getArenaForGladiator(sc).getType());
276             ab.getLocals().add(loc0);
277             for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
278                 Stmt s = (Stmt) stmtIt.next();
279                 if (s instanceof IdentityStmt) {
280                     IdentityStmt is = (IdentityStmt)s;
281                     Local left = (Local)is.getLeftOp();
282                     if (is.getRightOp() instanceof ThisRef) {
283                         left.setType(IntType.v());
284                         is.getRightOpBox().setValue(Jimple.v().newParameterRef(IntType.v(), m.getParameterCount()));
285                         if (!touched) {
286                             units.addFirst(Jimple.v().newIdentityStmt(loc0, Jimple.v().newThisRef(getArenaForGladiator(sc).getType())));
287                             touched = true;
288                         }
289                     }
290                 }
291
292                 for(Iterator i = s.getUseAndDefBoxes().iterator(); i.hasNext();) {
293                     Object o = i.next();
294                     if (o instanceof ValueBox) {
295                         ValueBox vb = (ValueBox)o;
296                         o = vb.getValue();
297                         /*
298                         if (o instanceof Local && implementsGladiator(((Local)o).getType())) {
299                             System.out.println("thunking");
300                             vb.setValue(loc0);
301                         }
302                         */
303                         if (vb.getValue() instanceof ThisRef) {
304                             System.out.println(s);
305                             vb.setValue(loc);
306                         }
307                     }
308                 }
309
310             }
311             
312             if (doremove) sc.removeMethod(m);
313
314         }
315         body.getUnits().add(returnStmt);
316     }
317
318     // Operations performed on all classes ////////////////////////////////////////////////////////////////////////////
319
320     public void processClass(SootClass c) {
321         for(Iterator it = c.getFields().iterator(); it.hasNext();) {
322             SootField f = (SootField)it.next();
323             Type t = f.getType();
324             if (t instanceof RefType) {
325                 RefType rt = (RefType)t;
326                 SootClass sc = rt.getSootClass();
327                 if (implementsGladiator(sc)) f.setType(IntType.v());
328             } else if (t instanceof ArrayType) {
329                 ArrayType at = (ArrayType)t;
330                 t = at.getElementType();
331                 if (!(t instanceof RefType)) continue;                
332                 RefType rt = (RefType)t;
333                 SootClass sc = rt.getSootClass();
334                 if (implementsGladiator(sc)) f.setType(IntType.v().makeArrayType());
335             }
336         }
337
338         List list = new LinkedList(); list.addAll(c.getMethods());
339         for(Iterator it = list.iterator(); it.hasNext();) {
340             SootMethod m = (SootMethod)it.next();
341             Body b = null;
342             if (m.getName().endsWith("$$inc")) continue;
343             if (m.hasActiveBody()) b = processBody(m.getActiveBody(), c, m);
344             List l2 = new LinkedList();
345             List l = m.getParameterTypes();
346             for(Iterator it2 = l.iterator(); it2.hasNext();) {
347                 Type t = (Type)it2.next();
348                 l2.add(implementsGladiator(t) ? IntType.v() : t);
349             }
350             Type t = m.getReturnType();
351             if (implementsGladiator(t)) {
352                 t = IntType.v();
353                 if (m.hasActiveBody()) {
354                     Body bod = m.getActiveBody();
355                     for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
356                         Stmt s = (Stmt) stmtIt.next();
357                         if (s instanceof ReturnStmt) {
358                             if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
359                                 ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
360                             }
361                         }
362                     }
363                 }
364             }
365             String name = m.getName();
366             SootMethod meth = new SootMethod(name, l2, implementsGladiator(t) ? IntType.v() : t, m.getModifiers());
367             if (b != null) {
368                 JimpleBody b2 = Jimple.v().newBody(meth);
369                 b2.importBodyContentsFrom(b);
370                 meth.setActiveBody(b2);
371             }
372             c.removeMethod(m);
373             c.addMethod(meth);
374         }
375
376     }
377
378     protected Body processBody(Body body, SootClass ownerClass, SootMethod smeth) {
379         if (body instanceof JimpleBody) {
380             JimpleBody b2 = Jimple.v().newBody(smeth);
381             b2.importBodyContentsFrom(body);
382             body = b2;
383         }
384         Chain units = body.getUnits();
385         System.out.println("processBody("+body.getMethod()+")");
386         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
387             Local l = (Local)it.next();
388             if (implementsGladiator(l.getType())) l.setType(IntType.v());
389         }
390         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
391             Stmt s = (Stmt) stmtIt.next();
392             if (s instanceof DefinitionStmt) {
393                 DefinitionStmt ds = (DefinitionStmt)s;
394                 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
395                     ds.getRightOpBox().setValue(IntConstant.v(-1));
396                 }
397             }
398             if (implementsGladiator(smeth.getReturnType()) && s instanceof ReturnStmt)
399                 if (((ReturnStmt)s).getOp().getType() instanceof NullType)
400                     ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
401             List l = s.getUseAndDefBoxes();
402             for(Iterator it = l.iterator(); it.hasNext();) {
403                 Object o = it.next();
404                 if (o instanceof ValueBox) {
405                     ValueBox vb = (ValueBox)o;
406                     Value v = vb.getValue();
407                     
408                     if (v instanceof BinopExpr) {
409                         BinopExpr boe = (BinopExpr)v;
410                         Type t1 = boe.getOp1().getType();
411                         Type t2 = boe.getOp2().getType();
412                         if (t1 instanceof PrimType && t2 instanceof NullType) boe.setOp2(IntConstant.v(-1));
413                         if (t2 instanceof PrimType && t1 instanceof NullType) boe.setOp1(IntConstant.v(-1));
414                     }
415
416                     if (v instanceof NewExpr) {
417                         NewExpr ne = (NewExpr)v;
418                         if (implementsGladiator(ne.getBaseType())) {
419                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
420                             SootClass mc = getArenaForGladiator(sc);
421                             String incFuncName = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$inc";
422                             SootMethodRef smr = Scene.v().makeMethodRef(mc, incFuncName, new LinkedList(), IntType.v(), false);
423                             Expr invokeExpr = Jimple.v().newSpecialInvokeExpr(body.getThisLocal(), smr);
424                             Local ll = viaLocal(invokeExpr, body, s);
425                             vb.setValue(ll);
426                             v = ll;
427                             continue;
428                         } 
429                     } else
430
431                     if (v instanceof InvokeExpr) {
432                         InvokeExpr ie = (InvokeExpr)v;
433                         SootMethodRef mr = ie.getMethodRef();
434                         String name = mr.name();
435                         if (v instanceof InstanceInvokeExpr && implementsGladiator(mr.declaringClass())) {
436                             InstanceInvokeExpr iie = (InstanceInvokeExpr)v;
437                             List li = new LinkedList();
438                             li.addAll(iie.getArgs());
439                             LinkedList pl = new LinkedList();
440                             for(Iterator it2 = mr.parameterTypes().iterator(); it2.hasNext();) {
441                                 Type t = (Type)it2.next();
442                                 pl.add(implementsGladiator(t) ? IntType.v() : t);
443                             }
444                             if (mr.name().equals("<init>") && implementsGladiator(mr.declaringClass())) {
445                                 name = "$init";
446                                 //li.remove(0);
447                                 //pl.remove(0);
448                             }
449                             pl.add(IntType.v());
450                             li.add(viaLocal(iie.getBase(),body,s));
451                             SootClass sc = mr.declaringClass();
452                             name = sc.getShortName().substring(sc.getShortName().lastIndexOf('$')+1) + "$$" + name;
453                             mr = Scene.v().makeMethodRef(getArenaForGladiator(sc),
454                                                          name,
455                                                          pl,
456                                                          implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
457                                                          false);
458                             ie = Jimple.v().newVirtualInvokeExpr(body.getThisLocal(), mr, li);
459                             vb.setValue(v = ie);
460
461                         } else if (!(v instanceof StaticInvokeExpr)) {
462                             List l0 = mr.parameterTypes();
463                             List l2 = new LinkedList();
464                             for(Iterator it2 = l0.iterator(); it2.hasNext();) {
465                                 Type t = (Type)it2.next();
466                                 l2.add(implementsGladiator(t) ? IntType.v() : t);
467                             }
468                             mr = Scene.v().makeMethodRef(mr.declaringClass(),
469                                                          mr.name(),
470                                                          l2,
471                                                          implementsGladiator(mr.returnType()) ? IntType.v() : mr.returnType(),
472                                                          mr.isStatic());
473                             ie.setMethodRef(mr);
474                             vb.setValue(v = ie);                            
475                         }
476
477                         for(int i=0; i<ie.getArgCount(); i++) {
478                             ValueBox b = ie.getArgBox(i);
479                             Value val = b.getValue();
480                             if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
481                                 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
482                                 List typelist = new LinkedList();
483                                 typelist.add(IntType.v());
484                                 SootMethod intMethod = intClass.getMethod("<init>", typelist);
485                                 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
486                                 List list = new LinkedList();
487                                 list.add(val);
488                                 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
489                                                                                                             intMethod.makeRef(),
490                                                                                                             list)),
491                                                    s);
492                                 b.setValue(loc);
493                             }
494                             if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
495                                 b.setValue(IntConstant.v(-1));
496                             }
497                         }
498
499
500                     } else if (v instanceof CastExpr) {
501                         CastExpr ce = (CastExpr)v;
502                         if (implementsGladiator(ce.getCastType())) {
503                             SootClass mc = getArenaForGladiator(((RefType)ce.getCastType()).getSootClass());
504                             SootClass ic = Scene.v().getSootClass("java.lang.Integer");
505                             ce.setCastType(ic.getType());
506
507                             Local l1 = Jimple.v().newLocal("tmpRef" + (tfr++), ic.getType()); body.getLocals().add(l1);
508                             Local l2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v()); body.getLocals().add(l2);
509
510                             Stmt s2 = Jimple.v().newAssignStmt(l1, Jimple.v().newCastExpr(ce.getOp(), ic.getType()));
511                             body.getUnits().insertBefore(s2, s);
512
513                             Stmt isNull = Jimple.v().newAssignStmt(l2, IntConstant.v(-1));
514                             body.getUnits().insertAfter(isNull, s2);
515
516                             Stmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(l1, NullConstant.v()), s);
517                             body.getUnits().insertAfter(ifStmt, isNull);
518
519                             SootMethodRef mr = Scene.v().makeMethodRef(ic, "intValue", new LinkedList(), IntType.v(), false);
520                             Stmt isNotNull =
521                                 Jimple.v().newAssignStmt(l2, Jimple.v().newVirtualInvokeExpr(l1, mr, new LinkedList()));
522                             body.getUnits().insertAfter(isNotNull, ifStmt);
523
524                             vb.setValue(l2);
525                             qq = 0;  // ???
526                             break;
527                         }
528
529                     } else if (v instanceof InstanceFieldRef) {
530                         InstanceFieldRef ifr = (InstanceFieldRef)v;
531                         SootFieldRef fr = ifr.getFieldRef();
532                         Type t = fr.type();
533                         if (implementsGladiator(fr.declaringClass()) && fr.name().equals("this$0")) {
534                             System.out.println("******************");
535                             vb.setValue(body.getThisLocal());
536                             System.out.println("s is " + s);
537                         } else if (implementsGladiator(fr.declaringClass())) {
538                             SootClass mc = getArenaForGladiator(fr.declaringClass());
539                             InstanceFieldRef sfr = newIFR(body,  getSliceForField(fr));
540                             vb.setValue(Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase()));
541                         }
542                         if ((t instanceof RefType) && implementsGladiator(((RefType)t).getSootClass())) {
543                             SootClass tc = ((RefType)t).getSootClass();
544                             SootClass mc = getArenaForGladiator(tc);
545                             ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
546                         }
547                     }
548
549                 }
550             }
551         }
552         body.validate();
553         return body;
554     }
555 }