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