Code Generator for Mini

Aarne Ranta
Assigment 1, Compiler Construction Hands-On, Sun Yat-Sen University, 2016

Summary

The objective of this assignment is to write an code generator from a fragment of the Mini programming language to JVM, Java Virtual Machine. The code generator should produce Java class files, which can be run in the Java bytecode interpreter so that they correctly perform all their input and output actions.

Before the work can be submitted, the program has to pass some tests, which are given on the book web page via links later in this document.

The recommended implementation is via a BNF grammar processed by the BNF Converter (BNFC) tool. The grammar built in Assignment 1 can be used.

A type checker is moreover recommended to annotate the abstract syntax trees with types, needed in instruction selection. But it can be replaced by a simplified "type guesser" that infers types but doesn't check that e.g. all operands conform to these types.

The code generator is partially characterized by compilation schemes in Chapter 6 of the book (and its slides). More instructions are given in Appendix B, which is reproduced here. These explanations follow Jasmin assembler; the code generator may emit Jasmin assembly code into text files, which are then processed further by Jasmin to create class files.

To test

An easy way to test your compiler is to run the test file test3.sh. In a Unix shell, you can use the command source test3.sh. This assumes that

Running the test produces the file log3.txt, which you must submit together with the file CodeGenerator.hs.

An optional extension: functions

This is based on the extension of the grammar in Assignment 1 with functions and function calls, and uses the grammar that you wrote there.

The recommended procedure is two passes:

  1. build a symbol table that for every function gives its type in the JVM format;
  2. compile the program by generating a class file containing every source code function as a method.

The main function in your code can have int as return type and an empty parameter list, like in C. But the compiler must treat it following the Java conventions (void return, array of strings as parameter).

All function calls are compiled to invokestatic calls of the methods found in the function lookup table created as the first pass of the compiler.

Your compiler must compile all additional example programs.