For compiling Latte programs to x64.
The compiler construction course at MIMUW.
A simple Java-like object-oriented language. Some of its features:
- 32-bit signed integeres, booleans, on-heap strings, arrays and user-defined classes.
- Virtual methods.
- Very simple IO through builtin
readInt
,readString
,printInt
,printString
methods. For some examples of correct Latte programs, seetest_inputs/good
directory.
This compiler is written in Rust programming language. It uses LALRPOP for generating a parser. The file src/grammar.lalrpop
contains the grammar used by LALRPOP
to generate Rust code during compilation (see build.rs
).
Implemented code optimizations:
- Const propagation
- Dead code removal
- GCSE
- Linear register allocation
Compilation:
- The input program is parsed into an AST using the parser generated with
LALRPOP
- The AST is statically checked and simplified (e.g.
for
loops are transformed intowhile
loops with anonymous variables) -src/frontend
module - Each function definition is transformed into a high-level intermediate SSA representation modeled after LLVM -
src/middlend
module - Intermediate representation is transformed into x64 assembly in nasm format -
src/backend
module - x64 assembly is processed using
nasm
executable and linked with Latte runtime withgcc
Command make
produces compilation artifacts in both target
and runtime
directories. To compile the project, make
uses gcc
and cargo
- the standard package manager for Rust.
Apart from building the compiler, make
also builds the Latte runtime. The source code for that is located in lib/runtime.c
.
Running the compiler requires compiled Latte runtime, gcc
and nasm
. For more info, see cargo run -- --help
.