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