A compiler for Decaf, a simplified C-like language, written in Rust. Includes several dataflow optimizations and a register allocator. Built with Andrew and Selena for Computer Language Engineering (spring 2025)!
The compiler runs through six stages to convert .dcf source files to x86-64 Assembly:
- Scanning - scans the source file and converts it into tokens
- Parsing - parses the tokens into an AST (abstract syntax tree)
- Semantic checking - checks that the syntax tree conforms to the Decaf language's rules
- CFG construction - constructs a control flow graph representing the source code's path(s) of execution
- Dataflow optimizations - perform several optimizations to make the resulting code smaller and faster
- Register allocation & x86-64 code generation - allocates variables to CPU registers in a way that maximizes program execution speed, then generates the final x86-64 code
./build.sh # cargo build --release
./run.sh -t <stage> [-O <opt,..>] [-o <outfile>] [-d] <input.dcf>-t / --target— stage to run:scan,parse,inter(semantic check), orassembly-O / --opt— comma-separated optimizations to apply (currentlydce, orall)-o / --output— output file (defaults to stdout)-d / --debug— print intermediate representations (AST, CFG, etc.) to stderr
Compile and run a Decaf program end-to-end:
./run.sh -t assembly tests/codegen/input/01-import.dcf -o out.S -O dce
gcc -O0 -no-pie out.S -o out
./out