Skip to content

Latest commit

 

History

History

java-parse

Using

Printing JBir HTML

Just use $ ./main.sh print-jbir:

Print out the JBir representation of java bytecode.

  main.byte print-jbir

=== flags ===

  -c class     file to print.
  [-m method]  name in class file to target.
  [-o output]  folder to write html to.
  [-help]      print this help text and exit
               (alias: -?)

Here's an example:

javac -g examples/climb-stairs/ClimbStairs.java
./main.sh print -c examples/climb-stairs/ClimbStairs.class

Printing a Program Graph

Print out dot file of many classes and methods.

  main.byte print-graph 

=== flags ===

  [-c class]   file to analyze (put before method name).
  [-m method]  name in classfile to target.
  [-o output]  folder to write dot data to.
  [-help]      print this help text and exit

Here's an example:

javac -g examples/climb-stairs/ClimbStairs.java
./main.sh print-graph -c ./examples/climb-stairs/ClimbStairs.class -m 'climbStairs1(I)I' \
                      -c ./examples/climb-stairs/ClimbStairs.class -m 'climbStairs0(I)I'
dot -Tsvg -O dot-out/*.dot

And here's a sample output:

graph of first climbstairs function

Printing a JSON Implication Graph

Print out implication graph (in JSON) of many classes and methods.

  main.byte print-implication

=== flags ===

  [-c class]   file to analyze (put before method name).
  [-m method]  name in classfile to target.
  [-o output]  to dot instead of json and specify out folder.
  [-help]      print this help text and exit
               (alias: -?)

Here's an example:

javac -g examples/simple/EvenLoop.java
./main.sh print-graph -c ./examples/simple/EvenLoop.class -m 'loop(I)I' > imp_graph.json

And here's a sample output:

{
  "edges": [
    {
      "start": "EvenLoop/loop/2",
      "end": "EvenLoop/loop/1",
      "formula": "(ExprCons(ExprCons(Eql Int)(Var(Free i' Int)))(ExprCons(ExprCons(Add Int)(Var(Free i Int)))(LInt 2)))",
      "rename": [
        {
          "i": "i'"
        }
      ]
    },
    {
      "start": "EvenLoop/loop/1",
      "end": "EvenLoop/loop/4",
      "formula": "(ExprCons(ExprCons(Ge Int)(Var(Free i Int)))(Var(Free n Int)))",
      "rename": []
    },
    {
      "start": "EvenLoop/loop/1",
      "end": "EvenLoop/loop/2",
      "formula": "(ExprCons Not(ExprCons(ExprCons(Ge Int)(Var(Free i Int)))(Var(Free n Int))))",
      "rename": []
    },
    {
      "start": "EvenLoop/loop/0",
      "end": "EvenLoop/loop/1",
      "formula": "(ExprCons(ExprCons(Eql Int)(Var(Free i Int)))(LInt 0))",
      "rename": []
    }
  ],
  "vertices": {
    "EvenLoop/loop/4": [
      "i"
    ],
    "EvenLoop/loop/2": [
      "i",
      "n"
    ],
    "EvenLoop/loop/1": [
      "i",
      "n"
    ],
    "EvenLoop/loop/0": [
      "n"
    ]
  }
}

A Note about Methods '-m'

You can specify a method to target in the command line, the format is method_name(args_list)ret_val where the types are specified using the JVM codes for types:

Character Type
B Byte
C Char
D Double
F Float
I Int
J Long
S Short
V Void
Z Boolean
[ Array of the thing following the bracket (Array of ints is [I).
L<class name>; full path of class name.

So the main method signature void main(String[] args) will be represented as main([Ljava.lang.String;)V.

Contributing

First install OPAM: https://opam.ocaml.org/doc/Install.html

# init opam
opam init -y

# configure env vars
eval $(opam config env)
echo 'eval $(opam config env)' >> ~/.bashrc

# switch to compiler 4.03.0
opam switch 4.03.0

# This can get complicated, follow all the prompts and install the deps it says.
opam install -y oasis core sawja ocamlgraph ppx_deriving

git clone 'https://bitbucket.org/wrharris/sim-itps.git'
cd sim-itps
git checkout sawja

make

Then install Java OpenJDK 7. Check the printing-jbir example to make sure everything's OK!

Tips

Documentation Generation

opam install odoc odig ocaml-manual
odig odoc
odig doc

Autocomplete

Make sure to install merlin along with your editor's autocomplete plugin.

opam install -y merlin