checkpoint
[org.ibex.arenaj.git] / src / edu / berkeley / cs / megacz / Transformer.java
1 package edu.berkeley.cs.megacz;
2 import soot.*;
3 import soot.jimple.*;
4 import soot.util.*;
5 import java.io.*;
6 import java.util.*;
7
8 public class Transformer extends BodyTransformer {    
9
10     private static Transformer instance = new Transformer();
11     private Transformer() {}
12     public static Transformer v() { return instance; }
13
14     public static void main(String[] args)  {
15         if(args.length == 0) {
16             System.out.println("Syntax: java " + v().getClass().getName() + " [soot options]");
17             System.exit(0);
18         }
19         PackManager.v().getPack("jtp").add(new Transform("jtp.instrumenter", Transformer.v()));
20         // Just in case, resolve the PrintStream SootClass.
21         Scene.v().addBasicClass("java.io.PrintStream",SootClass.SIGNATURES);
22         soot.Main.main(args);
23     }
24
25     static int tfr = 0;
26     public static Local viaLocal(Value v, Body b, Unit u) {
27         Local l = Jimple.v().newLocal("tmpRef" + (tfr++), v.getType());
28         b.getLocals().add(l);
29         b.getUnits().insertBefore(Jimple.v().newAssignStmt(l, v), u);
30         return l;
31     }
32
33     HashMap map = new HashMap();
34     HashSet size_added = new HashSet();
35
36     public boolean isGladiator(SootClass c) { return c.implementsInterface("edu.berkeley.cs.megacz.Gladiator"); }
37     public boolean isGladiatorField(SootField f)
38         { return isGladiator(f.getDeclaringClass()) && !f.getName().equals("this") && f.getName().indexOf('$')==-1; }
39     public boolean isGladiatorFieldRef(SootFieldRef f) {
40         return isGladiator(f.declaringClass()) && !f.name().equals("this") && f.name().indexOf('$')==-1;
41     }
42     public SootField getGladiatorField(SootField f) { return getGladiatorField(f.makeRef()); }
43     public SootField getGladiatorField(SootFieldRef f) {
44         SootClass c  = f.declaringClass();
45         SootClass oc = Scene.v().getSootClass(c.getName().substring(0, c.getName().lastIndexOf('$')));
46         String sig = f.declaringClass().getName()+"."+f.name();
47         if (map.get(sig) != null) return (SootField)map.get(sig);
48         Type t = f.type();
49         if (t instanceof RefType && isGladiator(((RefType)t).getSootClass())) t = IntType.v();
50         SootField nf = new SootField(c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "$" + f.name(),
51                                      t.makeArrayType(),
52                                      0);
53         oc.addField(nf);
54         Body body = oc.getMethod("<init>", new LinkedList()).getActiveBody();
55         Expr newArr = Jimple.v().newNewArrayExpr(t, IntConstant.v(100000));
56         Local newArrLocal = Jimple.v().newLocal("tmpRef" + (tfr++), f.type().makeArrayType());
57         body.getLocals().add(newArrLocal);
58         InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), nf.makeRef());
59         body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, newArrLocal));
60         body.getUnits().addFirst(Jimple.v().newAssignStmt(newArrLocal, newArr));
61         map.put(sig, nf);
62         return nf;
63     }
64
65     private Body body;
66     public SootFieldRef getGladiatorFieldSizeRef(SootClass c) {
67         SootClass mc = Scene.v().getMainClass();
68         String name = c.getShortName().substring(c.getShortName().lastIndexOf('$')+1) + "_size";
69         if (map.get(c) == null) {
70             SootField f = new SootField(name, IntType.v());
71             mc.addField(f);
72
73             Body body = mc.getMethod("<init>", new LinkedList()).getActiveBody();
74             InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), f.makeRef());
75             body.getUnits().addFirst(Jimple.v().newAssignStmt(ifr, IntConstant.v(0)));
76
77             map.put(c, f);
78         }
79         return Scene.v().makeFieldRef(mc, name, IntType.v(), false);
80     }
81
82     public boolean isGladiatorType(Type t) {
83         return (t instanceof RefType) && isGladiator(((RefType)t).getSootClass());
84     }
85
86     public SootMethodRef convert(SootMethodRef mr) {
87         List l = mr.parameterTypes();
88         List l2 = new LinkedList();
89         for(Iterator it2 = l.iterator(); it2.hasNext();) {
90             Type t = (Type)it2.next();
91             l2.add(isGladiatorType(t) ? IntType.v() : t);
92         }
93         return Scene.v().makeMethodRef(mr.declaringClass(),
94                                        mr.name(),
95                                        l2,
96                                        isGladiatorType(mr.returnType()) ? IntType.v() : mr.returnType(),
97                                        mr.isStatic());
98     }
99
100     boolean done = false;
101     public void internalTransform(Body bz, String phaseName, Map options) {
102         System.out.println("============= " + phaseName);
103         if (done) return;
104         done = true;
105         //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test$Inner"));
106         //nuke(Scene.v().getSootClass("edu.berkeley.cs.megacz.Test"));
107         nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh"));
108         nuke(Scene.v().getSootClass("org.ibex.graphics.Mesh$Triangle"));
109     }
110     public void nuke(SootClass c) {
111         for(Iterator it = c.getFields().iterator(); it.hasNext();) {
112             SootField f = (SootField)it.next();
113             Type t = f.getType();
114             if (t instanceof RefType) {
115                 RefType rt = (RefType)t;
116                 SootClass sc = rt.getSootClass();
117                 if (isGladiator(sc)) f.setType(IntType.v());
118             } else if (t instanceof ArrayType) {
119                 ArrayType at = (ArrayType)t;
120                 t = at.getElementType();
121                 if (!(t instanceof RefType)) continue;                
122                 RefType rt = (RefType)t;
123                 SootClass sc = rt.getSootClass();
124                 if (isGladiator(sc)) f.setType(IntType.v().makeArrayType());
125             }
126         }
127
128         List list = new LinkedList(); list.addAll(c.getMethods());
129         for(Iterator it = list.iterator(); it.hasNext();) {
130             SootMethod m = (SootMethod)it.next();
131             if (m.hasActiveBody()) m.setActiveBody(fixBody(m.getActiveBody()));
132             if (m.getName().equals("<init>")) continue;
133             if (m.getName().equals("<clinit>")) continue;
134             //if (map.get(m) != null) continue;
135             //map.put(m, Boolean.TRUE);
136             System.out.println(m.getName() + " -- " + m.getActiveBody());
137             List l2 = new LinkedList();
138             List l = m.getParameterTypes();
139             for(Iterator it2 = l.iterator(); it2.hasNext();) {
140                 Type t = (Type)it2.next();
141                 //l2.add(t);
142                 l2.add(isGladiatorType(t) ? IntType.v() : t);
143                 //if (isGladiatorType(t)) System.out.println("ought to swap " + t);
144             }
145             Type t = m.getReturnType();
146             c.removeMethod(m);
147             if (isGladiatorType(t)) {
148                 t = IntType.v();
149                 Body bod = m.getActiveBody();
150                 for(Iterator stmtIt = bod.getUnits().snapshotIterator(); stmtIt.hasNext();) {
151                     Stmt s = (Stmt) stmtIt.next();
152                     if (s instanceof ReturnStmt) {
153                         if (((ReturnStmt)s).getOp().getType() instanceof NullType) {
154                             ((ReturnStmt)s).getOpBox().setValue(IntConstant.v(-1));
155                         }
156                     }
157                 }
158             }
159             SootMethod meth = new SootMethod(m.getName(), l2, t, m.getModifiers());
160             meth.setActiveBody(m.getActiveBody());
161             c.addMethod(meth);
162         }
163
164     }
165
166     protected Body fixBody(Body body) {
167         this.body = body;
168         Chain units = body.getUnits();
169         for(Iterator it = body.getLocals().snapshotIterator(); it.hasNext();) {
170             Local l = (Local)it.next();
171             if (isGladiatorType(l.getType())) l.setType(IntType.v());
172         }
173         for(int qq=0; qq<2; qq++) for(Iterator stmtIt = units.snapshotIterator(); stmtIt.hasNext();) {
174             Stmt s = (Stmt) stmtIt.next();
175             List l = s.getUseAndDefBoxes();
176             if (s instanceof DefinitionStmt) {
177                 DefinitionStmt ds = (DefinitionStmt)s;
178                 if (ds.getLeftOp().getType() instanceof PrimType && ds.getRightOp().getType() instanceof NullType) {
179                     System.out.println("*************************");
180                     ds.getRightOpBox().setValue(IntConstant.v(-1));
181                 }
182             }
183             for(Iterator it = l.iterator(); it.hasNext();) {
184                 Object o = it.next();
185                 if (o instanceof ValueBox) {
186                     ValueBox vb = (ValueBox)o;
187                     Value v = vb.getValue();
188                     
189                     if (v instanceof BinopExpr) {
190                         BinopExpr boe = (BinopExpr)v;
191                         if (boe.getOp1().getType() instanceof PrimType && boe.getOp2().getType() instanceof NullType) {
192                             System.out.println("*************************");
193                             boe.setOp2(IntConstant.v(-1));
194                         }
195                         if (boe.getOp2().getType() instanceof PrimType && boe.getOp1().getType() instanceof NullType) {
196                             System.out.println("*************************");
197                             boe.setOp1(IntConstant.v(-1));
198                         }
199                     }
200
201                     if (v instanceof InvokeExpr) {
202                         InvokeExpr ie = (InvokeExpr)v;
203                         SootMethodRef mr = ie.getMethodRef();
204                         if (isGladiator(mr.declaringClass())) {
205                             body.getUnits().remove(s);
206                             break;
207                         }
208                         mr = convert(mr);
209                         ie.setMethodRef(mr);
210                         for(int i=0; i<ie.getArgCount(); i++) {
211                             ValueBox b = ie.getArgBox(i);
212                             Value val = b.getValue();
213                             if (mr.parameterType(i) instanceof RefType && val.getType() instanceof PrimType) {
214                                 SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
215                                 List typelist = new LinkedList();
216                                 typelist.add(IntType.v());
217                                 SootMethod intMethod = intClass.getMethod("<init>", typelist);
218                                 Local loc = viaLocal(Jimple.v().newNewExpr(RefType.v(intClass)), body, s);
219                                 List list = new LinkedList();
220                                 list.add(val);
221                                 units.insertBefore(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(loc,
222                                                                                                             intMethod.makeRef(),
223                                                                                                             list)),
224                                                    s);
225                                 b.setValue(loc);
226                             }
227                             if (val != null && val.getType() instanceof NullType && mr.parameterType(i) instanceof IntType) {
228                                 b.setValue(IntConstant.v(-1));
229                             }
230                         }
231
232                     } else if (v instanceof NewExpr) {
233                         NewExpr ne = (NewExpr)v;
234                         if (isGladiatorType(ne.getBaseType())) {
235                             System.out.println("******");
236                             SootClass mc = Scene.v().getMainClass();
237                             SootClass sc = ((RefType)ne.getBaseType()).getSootClass();
238                             System.out.println(sc);
239                             InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(),
240                                                                                   getGladiatorFieldSizeRef(sc));
241                             Local ll = viaLocal(sfr, body, s);
242                             Local ll2 = Jimple.v().newLocal("tmpRef" + (tfr++), IntType.v());
243                             body.getLocals().add(ll2);
244                             Stmt stmt = Jimple.v().newAssignStmt(ll2, Jimple.v().newAddExpr(ll, IntConstant.v(1)));
245                             units.insertBefore(stmt, s);
246                             units.insertAfter(Jimple.v().newAssignStmt(sfr, ll2), stmt);
247                             vb.setValue(ll);
248                         }
249
250                     } else if (v instanceof CastExpr) {
251                         CastExpr ce = (CastExpr)v;
252                         if (isGladiatorType(ce.getCastType())) {
253                             //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
254                             //SootClass intClass = Scene.v().getSootClass("java.lang.Integer");
255                             SootClass mc = Scene.v().getMainClass();
256                             LinkedList list = new LinkedList();
257                             list.add(Scene.v().getSootClass("java.lang.Object").getType());
258                             SootMethodRef mr = Scene.v().makeMethodRef(mc, "zap", list, IntType.v(), true);
259                             Local loc = viaLocal(ce.getOp(), body, s);
260                             List list2 = new LinkedList();
261                             list2.add(loc);
262                             vb.setValue(viaLocal(Jimple.v().newStaticInvokeExpr(mr, list2), body, s));
263                         }
264
265                     } else if (v instanceof InstanceFieldRef) {
266                         InstanceFieldRef ifr = (InstanceFieldRef)v;
267                         SootFieldRef fr = ifr.getFieldRef();
268                         Type t = fr.type();
269                         if (isGladiatorFieldRef(fr)) {
270                             SootClass mc = Scene.v().getMainClass();
271                             SootFieldRef sf = getGladiatorField(fr).makeRef();
272                             InstanceFieldRef sfr = Jimple.v().newInstanceFieldRef(body.getThisLocal(), sf);
273                             ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, s), ifr.getBase());
274                             vb.setValue(ar);
275                         }
276                         if ((t instanceof RefType) && isGladiator(((RefType)t).getSootClass())) {
277                             SootClass tc = ((RefType)t).getSootClass();
278                             SootClass mc = Scene.v().getMainClass();
279                             ifr.setFieldRef(Scene.v().makeFieldRef(mc, fr.name(), IntType.v(), false));
280                         }
281                     }
282                 }
283             }
284         }
285         return body;
286     }
287 }