Hints
Getting Started
-
It's worth reading the options and phase options pages in the Soot tutorial.
They'll tell you the structure of Soot's phases and options so you can
figure out where to start.
-
An easy way to apply your intraprocedural analysis framework might be to
add your IR building, analysis, transformation, and IR-to-Jimple phases
to the Jimple Transformation Pack, or jtp. They will automatically
be run on each source file in an application, with Soot running in --app
mode.
-
Adding interprocedural analyses can be done similarly by using the wjtp
pack.
-
You might model your compiler after the structure in examples 5.4.3 and
5.4.5 from the master's thesis on Soot--it shows how to add transformation
passes to the jtp and wjtp packs.
-
Make sure you have your classpath set correctly (see the project
page or the Soot
documentation). If you haven't included the /cse/courses/cse501/01wi/rt
directory, you might get errors like "Warning: java.lang.Object is a phantom
class!"
Writing a transformation to your representation
-
If you use Soot's analysis framework to construct your IR, be aware that
the "merge" function may be called with the first and last arguments aliased
to the same object. This is not explicitly documented.
-
If you add a new subclass of Stmt or Expr to your new representation, be
sure to implement the appropriate functions in the interface, so that your
code will print out. A first implementation can throw an exception,
so you know that you need to implement it when it is called.
-
When you create a new Local (using Jimple.v().newLocal()), be sure to add
it to the body with body.getLocals().add()
-
You don't have to worry about arrays or pointers pointing to local variables.
That's because local variables in Java can only hold scalar values and
references to objects. Pointers become a problem in languages like
C, where you can have a pointer to a local variable, or in Java when you
try to reason about the fields of different objects. For example,
it requires some pointer analysis to re-order loads and stores in Java
bytecode. An array access is just like a field load (with an index
instead of an static offset) for this purpose.
-
For debugging, it's nice to have the Jimple code look like your original
Java code. You can turn off some of the optimizations that Soot does
in the transformation from bytecode to Jimple--see the Phase
Options page. For example, if you want to preserve the original
variable names in your test file TestFile.java, first compile with the
-g option:
then run soot with the -p jb use-original-names option:
java soot.Main TestFile -p jb use-original-names