Skip to content

Latest commit

 

History

117 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decaf Compiler

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)!

Pipeline

The compiler runs through six stages to convert .dcf source files to x86-64 Assembly:

  1. Scanning - scans the source file and converts it into tokens
  2. Parsing - parses the tokens into an AST (abstract syntax tree)
  3. Semantic checking - checks that the syntax tree conforms to the Decaf language's rules
  4. CFG construction - constructs a control flow graph representing the source code's path(s) of execution
  5. Dataflow optimizations - perform several optimizations to make the resulting code smaller and faster
  6. 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

Usage

./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), or assembly
  • -O / --opt — comma-separated optimizations to apply (currently dce, or all)
  • -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

About

Compiler for Decaf (a simplified C-like language) written in Rust.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages