Assignment 04 - miniIR to microIR Translator
| Release Date | 21/09/2026 |
| Deadline | 12/10/2026, 23:59 hours |
This assignment is graded out of 15: 9 for this take-home part and 6 for an extension you implement in the lab session on 13/10/2026. See the assignments page for how the lab component works.
This assignment is the fourth part of a multi part project to write an optimizing compiler for MiniJava. In the previous assignment you produced miniIR, in which an operand may itself be an arbitrarily nested expression. In this assignment you will translate miniIR into microIR, a flattened form in which every operand of every instruction is either a temporary or a literal.
Flattening is what makes the later phases possible: once each instruction performs exactly one operation on simple operands, the program is a linear sequence of three-address instructions, and liveness analysis and register allocation (assignment 5) can be defined over it directly.
Concretely, a nested miniIR statement such as
MOVE TEMP 3 HALLOCATE 4
must be rewritten so that the inner expression is computed into a fresh temporary first:
MOVE TEMP 2 4
MOVE TEMP 3 HALLOCATE TEMP 2
You will need to introduce fresh temporaries for intermediate results, and
linearise nested statement-expressions (BEGIN ... RETURN ... END) into
straight-line code. Take care that the temporaries you invent do not collide
with those already present in the input.
Use JTB and JavaCC and write in Java one or more visitors which translate miniIR programs to microIR form. Your main file should be called P3.java, if A.miniIR contains a program to be flattened then
$ javac P3.java
$ java P3 < A.miniIR > A.microIR
should create A.microIR in microIR form and is semantically equivalent to A.miniIR. Note, your program must take input from standard input and write to standard output (so that we can use redirection).
To check that a program is in microIR form, you could tie the microIR.jj to build a parser. To ensure that your microIR program is semantically equivalent to the miniIR program, you can use the interpreters interp-miniIR.jar and interp-microIR.jar and compare their outputs:
$ java -jar interp-miniIR.jar < A.miniIR > expected.txt
$ java -jar interp-microIR.jar < A.microIR > actual.txt
$ diff expected.txt actual.txt
Alternatively, make compiles the assignment, make run FILE=<input> runs it,
and make clean removes the .class files you must delete before submitting.
Note: Please do not alter the directory structure of RollNo_P3.
Only edit GJDepthFirst.java and P3.java
Resources
The source files are on Github.
Get the source by cloning the entire repo:
$ git clone https://github.com/fplaunchpad/cs3300_m26
If you have already cloned the repo, you can get the latest updates by:
$ cd cs3300_m26 # go to the cloned repo
$ git pull
The miniIR grammar specification is BNF-miniIR.
The microIR grammar specification is BNF-microIR.
Sample miniIR inputs are in sample_miniIR, and sample microIR programs (to
show you the shape of the expected output) are in sample_microIR.
Submission
Rename the folder RollNo_P3 with your roll number. For example, if your roll
number is cs99b999, then the folder should be named cs99b999_P3. Remove all
the .class files before submission. For example, you can do:
$ cd cs99b999_P3
$ find . -name "*.class" | xargs rm
Then produce the compressed gzip archive as follows:
$ tar cvzf cs99b999_P3.tar.gz cs99b999_P3
Submit the .tar.gz file.
Note: You should thoroughly test your code. Some sample miniIR programs are provided for testing. Create positive and negative tests per feature and test your code before submission. Please ensure that there are no compilation errors. If there are compilation errors, that code will yield zero marks.
Checking your work before you submit
Run the same program that marks you over the public testcases:
$ python3 ../selftest/eval.py <YOUR-ROLLNO>_P3.tar.gz
It builds your archive the way the grader does, so a packaging mistake shows
up here instead of costing you marks. Note that assignment 4 submits as
_P3, one behind the assignment number.
There are three sets of testcases: the public set you can run here, a private set behind the online checker that reports only how many passed, and a hidden set used for marking that you never see. You are marked on all three together, every test counting equally, so passing the first two is necessary and not sufficient. A submission that does not build scores zero.
See selftest/README.md.