MiniJava to LLVM IR compiler
This is a MiniJava compiler that targets LLVM, written in Java with the help of JFlex, JavaCUP and JTB.
I would like to thank Stefanos Baziotis for contributing his MiniJava testsuite!
# Compile the project
make
# Run the compiler
java Main file1 [file2 [file3 ...]]
# Cleanup
make clean
MiniJava is a simple language when it comes to scoping: all declarations precede statements. This property prevents declarations from showing up in nested compound statements such as if-else, while and block statements. Thus, the symbol table need only keep track of class declarations, class-scoped declarations (i.e. fields and methods) and method-scoped declarations (i.e. formal parameters and local variables).
We start by producing an AST for the source program with the help of JFlex, JavaCUP and JTB. Then, we check its semantics & finally generate the LLVM IR code with the help of the following visitor classes:
STVisitor
populates the symbol table
SCVisitor
type checks the program
VTVisitor
populates the virtual method table
CGVisitor
generates the resulting LLVM IR code
The MainClass
class and its main
method are not handled as special cases. The method's
argument is stored in the symbol table with type String[]
, so, if it appears in any
expression in the program, a type error will be triggered by SCVisitor
inevitably.
Scopes are implemented by constructing strings of the form ClassID [":" [MethodID]]
and passing
them as arguments to children nodes as the visitors are walking the AST.
LLVM-IR code virtual registers are assigned names of the form %_number
, where number
is an
auto-incrementing integer.