A SysY compiler implementation targeting Koopa IR and RISC-V Assembly. Developed for the PKU Compile Principle Laboratory.
- Frontend: Lexical and Syntactic analysis using Flex & Bison. Generates an Abstract Syntax Tree (AST).
- Middle-end: Generates Koopa IR (Intermediate Representation) from AST.
- Backend: Generates RISC-V assembly from Koopa IR.
- Modern C++: Built with C++20 Modules and C++23 standards for better modularity and compilation speed.
- Dependencies: Uses
fmtlibrary for formatting (self-contained, fetched via CMake).
This project is designed to run in a Docker environment since LLVM and other dependencies can be tricky to configure locally. You have two ways to build the project.
-
Ensure you have Docker and VS Code installed with the Dev Containers extension.
-
Open this project folder in VS Code.
-
When prompted (or via the Command Palette
F1), select Dev Containers: Reopen in Container. -
Once inside the container, the environment is pre-configured. You can build via the terminal:
make
If you are not using VS Code Dev Containers, you can use the provided Makefile to enter the Docker environment.
-
Enter the Docker container:
make shell
-
Note: The environment requires
Ninjafor the build system tailored inMakefile. Sincesudois not available, you should manually download the Ninja binary: -
Build the project using
make:make
This command uses
cmakewith theNinjagenerator.
The compiler requires specific mandatory arguments to function correctly. The basic command structure is:
./compiler -[mode] <input_file> -o <output_file>One of the following modes must be specified to generate output:
-koopa: Compile SysY source to Koopa IR.-riscv: Compile SysY source to RISC-V assembly.-perf: Compile with performance optimizations enabled.
Compile to Koopa IR:
./cmake-build/compiler -koopa test.c -o test.koopaCompile to RISC-V Assembly:
./cmake-build/compiler -riscv test.c -o test.S| Option | Required | Description |
|---|---|---|
-koopa |
Yes* | Output Koopa IR (Mutual exclusion with -riscv) |
-riscv |
Yes* | Output RISC-V assembly (Mutual exclusion with -koopa) |
-perf |
No | Enable optimizations(Not yet completed) |
-o <file> |
Yes | Specify the output file path |
<input_file> |
Yes | The source code file to compile |
-h, --help |
No | Show help message |
Note: You must select a compilation target mode.
src/frontend: Lexer (sysy.lx) and Parser (sysy.y)src/ir: AST and IR generation logicsrc/backend: RISC-V code generationsrc/main.cpp: Entry point with C++20 module importsinclude: Header files, including Koopa wrapper and logging
- Complete implementation notes and documentation
- Optimizations for IR and RISC-V
- Floating point support (maybe)