RCC is a small Rust-based C compiler project for a minimal subset of C. It lexes, parses, builds an AST, lowers that AST to LLVM IR with inkwell, and then invokes clang or gcc to produce an executable.
Current support is intentionally small:
int main() { return <expr>; }- integer literals
- binary arithmetic:
+-*/
- parentheses for grouping
Examples:
int main() { return 5; }
int main() { return 2 + 3; }
int main() { return 4 * (2 + 1); }src/
ast/
codegen/
lexer/
parser/
main.rs
tests/
samples/
- Rust and Cargo
clangorgcc- LLVM 17 for
inkwell
This repo is currently configured for Homebrew LLVM 17 on macOS in .cargo/config.toml:
[env]
LLVM_SYS_170_PREFIX = "/opt/homebrew/opt/llvm@17"If you use a different LLVM install location, update that path.
cargo buildThe compiler binary is named compiler.
Compile a C file:
cargo run --bin compiler -- samples/return_5.cThis produces:
output.ll- generated LLVM IRoutput- compiled executable
Run the produced executable and inspect its return code:
./output
echo $?Sample inputs live in the samples directory:
Run all tests:
cargo testThe test suite includes:
- unit tests for the lexer, parser, AST, and codegen
- end-to-end tests that compile small C programs, run them, and check the exit code
The compiler pipeline is:
- Lex source code into tokens
- Parse tokens into an AST
- Lower the AST into LLVM IR
- Write LLVM IR to
output.ll - Invoke the system compiler to build
output
For example, this AST:
int main() { return 4 * (2 + 1); }can lower to LLVM IR equivalent to:
define i32 @main() {
entry:
ret i32 12
}LLVM may fold constant expressions during IR construction, so simple arithmetic can appear already simplified in the generated IR.