A simple compiler written in Rust that translates C language into x86-64 assembly code. This project demonstrates the fundamental phases of compilation: lexical analysis, parsing, and code generation.
- Function definitions with return types
- Return statements with optional expressions
- Function parameters (basic parameter parsing implemented)
- Declarations and Assignments
- Block statements with curly braces
{}
- Integers (
int
) - Void (
void
) - Char (
char
) - Long (
long
) - Double (
double
) - Float (
float
)
- Arithmetic operations:
+
,-
,*
,/
- Unary operations:
- Unary minus (
-
) - Logical NOT (
!
) - Bitwise NOT (
~
)
- Unary minus (
- Binary expressions with proper operator precedence
- Parenthesized expressions
- Variable references (identifier lookup)
- Numeric literals
- Ternary expressions
- Single-line comments (
//
) - Block comments (
/* */
)
- Variable declarations and assignments
- Local variable scoping
- Function calls with argument passing
- Multiple parameter support (currently limited to 6 due to calling convention)
- Conditional statements (
if
/else
) - Loops (
while
,for
) - Comparison operators (
==
,!=
,<
,>
,<=
,>=
) - Logical operators (
&&
,||
)
- Arrays and indexing
- Nested Blocks
- Pointers and references
- Structures/records
- String handling
- Multiple source files
- Basic optimizations (constant folding, dead code elimination)
- Better error messages with line numbers and suggestions
- Debugging information generation
- Standard library functions (
printf
, etc.)
- Type system improvements
- Generic/template support
- Module system
- Memory management features
The compiler follows a traditional three-phase design:
- Scanner tokenizes the source code
- Handles keywords, operators, identifiers, numbers, and strings
- Supports both single-line and block comments
- Implements "maximal munch" principle for token recognition
- Recursive descent parser builds an Abstract Syntax Tree (AST)
- Implements operator precedence for binary expressions
- Handles unary expressions and function definitions
- Error reporting for syntax errors
- AssemblyGenerator translates AST to x86-64 assembly
- Follows System V ABI calling conventions
- Manages stack frame allocation and variable storage
- Generates complete executable assembly with proper prologue/epilogue
- Rust compiler
- GNU Assembler (
as
) - GNU Linker (
ld
)
- Compile the compiler:
cargo build --release
- Compile a source file:
./target/release/your_compiler_name source_file.c
- Run the generated executable:
./source_file
# Create a simple program
echo 'int main() { return 42; }' > test.c
# Compile it
cargo run test.c
# Run the generated executable
./test
# Check the exit code
echo $? # Should output: 42
This is a learning project, but contributions are welcome! Areas that need attention:
- Parser improvements - Better error recovery and reporting
- Code generation - More expression types and optimizations
- Testing - Comprehensive test suite for all components
- Documentation - Code comments and usage examples
This compiler is a work in progress and serves as an educational project for understanding compiler construction principles.