This project implements a full-featured compiler for a custom programming language, demonstrating modern compiler design principles and optimization techniques. The compiler follows the traditional multi-pass compilation process while incorporating advanced features like optimization frameworks and sophisticated register allocation.
The compiler is structured into several major components:
-
Lexical Analysis:
- Implemented using Flex for efficient token generation
- Handles source code tokenization with support for complex language constructs
- Located in lexer.l and src/lexer.cpp
-
Parsing:
- Built with Bison for robust grammar processing
- Generates Abstract Syntax Tree (AST) representation
- Implements error recovery mechanisms
- Core files: parser.y and src/parser.cpp
-
Semantic Analysis:
- Comprehensive type checking system (src/type_system.cpp)
- Symbol table management for variable scoping (src/symbol_table.cpp)
- Semantic error detection and reporting
-
Optimization Framework:
- Multiple optimization passes:
- Common subexpression elimination
- Dead code elimination
- Constant propagation
- Loop optimization
- Configurable optimization levels
- Implementation in src/optimization.cpp
- Multiple optimization passes:
-
Code Generation:
- Target-specific code emission
- Efficient instruction selection
- Located in src/codegen.cpp
-
Register Allocation:
- Graph coloring-based register allocation
- Register spilling handling
- Implemented in src/register_allocator.cpp
- CMake (3.10 or higher)
- C++ compiler supporting C++17
- Flex and Bison
- Make
mkdir build && cd build
cmake ..
make
make
.
├── include/ # Header files
├── src/ # Source implementations
├── test/ # Test suite
├── lexer.l # Flex lexer definition
├── parser.y # Bison parser grammar
├── CMakeLists.txt # CMake configuration
└── Makefile # Traditional build support
- Complete frontend pipeline (lexing, parsing)
- Type system with static type checking
- Basic optimization framework
- Register allocation using graph coloring
- Runtime support system
-
Short-term Goals
- Enhance optimization passes
- Improve error reporting
- Extend type system
-
Medium-term Goals
- Implement advanced optimizations
- Add cross-platform support
- Enhance debugging information
-
Long-term Vision
- JIT compilation support
- Parallel compilation
- Language server protocol integration
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch
- Implement your changes
- Add tests for new functionality
- Submit a pull request
- Follow the existing code style
- Add documentation for new features
- Ensure all tests pass
- Update relevant documentation
This project is licensed under the MIT License - see the LICENSE file for details.
- The LLVM Project for inspiration
- The GCC community for optimization insights
- Contributors and maintainers