CSE 401, Winter 2014

Robert R. Henry

Assignment 9

Statement Counting

Due via dropbox Friday March 7 by the end of the day (midnight). This assignment is not as hard as the others, especially since you can make convenient assumptions about things, like the maximum number of lines of code in a MJ file.


By the end of this assignment you will be able generate statement count profiles of executing Mini Java programs.  You will need to trade off work done at compile time, work done at run time in the code you generate, and work done in the Mini Java runtime library (boot.c).


  1. Implement a zero-filled array of int64’s (Java longs, Mini Java ints) big enough to hold counters for all the source code lines in your program, or assume a maximum program size.
  2. Emit code at the beginning of each statement that increments the counter that corresponds to the line that statement was on.  Don’t worry about cases where there is more than one statement on the line.
  3. Dump the counters in a way that correlates the source line number with the statement count itself.
  4. Merge the counters and a copy of the original source code, and print the results, nicely formatted.  Follow the example output from gcov that I passed out in class a few weeks ago. Feel free to write a companion program, in any language of your choice, to do the merge.  You may be able to use the unix “pr” (“print”) program for this.


  1. Generate an annotated source file for the Mini Java source file we supplied you Pi.java (in SamplePrograms/SampleMiniJavaPrograms/Pi.java unless you’ve moved things around); the annotations should clearly show which lines were executable, and for those lines, how many times each statement was executed.
  2. We weren’t able to figure out how to get a per-line statement counting to come out of the Java Run Time to use as a reference implementation.
  3. If you want, you can spend 5 minutes hand translating the java program Pi.java program to a C++ program Pi.cc, and then run the gcc/g++ statement counting pipeline to get a reference execution, as shown here:
  1. g++ -fprofile-arcs -ftest-coverage -Wall -O0 -g3 Pi.cc -o Pi.x
  2. ./Pi.x
  3. gcov Pi.cc
  4. cat Pi.cc.gcov

What to turn in

  1. Tarball of your source
  2. Instructions on how to run your statement profiler
  3. The annotated source file for Pi.java showing statement counts.