6c527533df040175cb5972227cfe51e3aafd1494
[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     public static Local viaLocal(Value v, Body b, Chain c, Unit u) {
26         Local l = Jimple.v().newLocal("tmpRef", v.getType());
27         b.getLocals().add(l);
28         c.insertBefore(Jimple.v().newAssignStmt(l, v), u);
29         return l;
30     }
31
32     protected void internalTransform(Body body, String phaseName, Map options) {
33         SootClass sClass = body.getMethod().getDeclaringClass();
34         SootField gotoCounter = null;
35         boolean addedLocals = false;
36         Local tmpRef = null, tmpLong = null;
37         Chain units = body.getUnits();
38         
39         boolean isMainMethod = body.getMethod().getSubSignature().equals("void main(java.lang.String[])");
40         Iterator stmtIt = units.snapshotIterator();
41         while(stmtIt.hasNext()) {
42             Stmt s = (Stmt) stmtIt.next();
43             //if (isMainMethod) {
44             List l = s.getUseAndDefBoxes();
45             for(Iterator it = l.iterator(); it.hasNext();) {
46                 Object o = it.next();
47                 if (o instanceof ValueBox) {
48                     ValueBox vb = (ValueBox)o;
49                     Value v = vb.getValue();
50                     if (v instanceof CastExpr) {
51                         CastExpr ce = (CastExpr)v;
52                         //System.out.println(">>> cast " + ce.getOp().getType() + " -> " + ce.getCastType());
53                     } else if (v instanceof InstanceFieldRef) {
54                         InstanceFieldRef ifr = (InstanceFieldRef)v;
55                         SootField f = ifr.getField();
56                         System.out.println(">>> getField " + f);
57                         if (f.getName().equals("foo")) {
58                             SootClass mc = Scene.v().getMainClass();
59                             Type arrType = f.getType().makeArrayType();
60                             SootField sf = mc.getField("arr_" + f.getName(), arrType);
61                             StaticFieldRef sfr = Jimple.v().newStaticFieldRef(sf.makeRef());
62                             ArrayRef ar = Jimple.v().newArrayRef(viaLocal(sfr, body, units, s), IntConstant.v(3));
63                             System.out.println(">>>>> " + ar.getType());
64                             System.out.println(">>>>> " + v.getType());
65                             vb.setValue(ar);
66                         }
67                     }
68                 }
69             }
70         }
71     }
72 }