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