L4Compiler is a simple L4 language compiler written in C++17, implementing the full compilation pipeline. Developed through four lab stages, it progressively adds support for advanced language features, optimization passes, and runtime checks.
- Flex based L4 lexer (
Lexer.l). - Bison-generated LALR(1) parser (
Parser.y) producing an Abstract Syntax Tree (AST). - Static type checking, scope management, symbol tables, and memory-safety validation.
- Custom IR with Control‑Flow Graph (CFG), supporting optimization passes such as dead code elimination, and variable coalescing.
- Register Allocation: Graph‑coloring allocator with spill handling and calling‑convention support.
- IR-to-assembly translation via X86Translator.
- ASMGenerator emits AT&T‑syntax x86-32 / x86-64 code with Position‑Independent Code (PIC) support.
- Perform simple optimizations during code generation
- Generate AT&T-syntax x86-32 and x86-64 assembly output
- Provide a command-line interface for compiling source files
- Include unit tests powered by GoogleTest
- Support JSON-based configuration and resource loading for individual unit test cases
L4Compiler/
├── .github/ # CI workflows (GitHub Actions)
├── build.sh # Convenience script: configure & build
├── run.sh # Wrapper: compile source → binary
├── test.sh # Wrapper: run unit tests
├── CMakeLists.txt # Root CMake configuration
├── include/ # Vendored headers (FlexLexer, cxxopts, nlohmann/json, spdlog, Boost)
├── src/ # Core compiler implementation
│ ├── core/ # Parser, AST, CFG, IR, semantic analyzer, ASMGenerator
│ ├── model/ # AST & IR data structures, CFG basic blocks
│ ├── handlers/ # IR generation and translation handlers
│ ├── driver/ # CLI entrypoint & orchestration (API.cpp)
│ ├── L4Compiler.cpp # Main wrapper
│ └── CMakeLists.txt # Add subdirectories & define `compiler` target
├── test/ # GoogleTest suite (FetchContent for gtest)
├── resources/ # JSON configs and test inputs
└── COPYING # GPL-3.0 License
- A C++17-compatible compiler (e.g., g++, clang++)
- CMake 3.17 or higher
- Bash (for the provided build and run scripts)
-
Clone the repository
-
Build with CMake:
mkdir build cd build cmake -DBUILD_TEST_CASES=ON [-DBUILD_UNITY=ON] .. make
-
Or use the helper scripts:
./build.sh # Configure and build
Compile a source file:
# Direct invocation:
build/bin/compiler -i path/to/source.txt -o path/to/output_binary [--r64] [-d]
# Using provided wrapper scripts:
./run.sh path/to/source.txt path/to/output_binary
./test.shRun the full test suite:
./test.shJSON Tests: Located under resources/tests/, where each file defines inputs and
expected outputs.
- cxxopts – Lightweight C++ command line parser
- nlohmann/json – JSON library for modern C++
- spdlog – Fast C++ logging library
- fmtlib – Formatting library (bundled with spdlog)
- GNU Flex – Lexical analyzer generator
- GNU Bison – Parser generator
- GoogleTest – Unit testing framework
- Boost – A collection of peer-reviewed, portable C++ libraries covering common tasks such as smart pointers, data structures, algorithms, threading, serialization, and more.
All test cases and their associated configurations are located in the
resources/ folder.
Contributions, issues, and feature requests are welcome! Please open an issue or submit a pull request on GitHub.