Very basic LLVM-based toy compiler for a tiny Rust-like subset. It tokenizes input, performs a simple ownership analysis, generates LLVM IR, and prints the result.
- Windows with MSYS2
- Packages in the MSYS2 MinGW 64-bit shell:
- LLVM, Clang, CMake
Install MSYS2:
winget install MSYS2.MSYS2Open the "MSYS2 MinGW 64-bit" shell (Start Menu → search “MSYS2 MinGW 64-bit”), then update and install:
pacman -Syu
pacman -S mingw-w64-x86_64-llvm mingw-w64-x86_64-clang mingw-w64-x86_64-cmakeIn the "MSYS2 MinGW 64-bit" shell:
cd /c/Users/daobo/OneDrive/Documents/GitHub/Simple-Rust-Compiler
clang++ -std=c++17 -g main.cpp $(llvm-config --cxxflags) -o minirust $(llvm-config --ldflags --system-libs --libs all)cd /c/Users/daobo/OneDrive/Documents/GitHub/Simple-Rust-Compiler
cmake -S . -B build -G "MinGW Makefiles"
# If LLVM is not found automatically, add: -DLLVM_DIR=/mingw64/lib/cmake/llvm
cmake --build build -j
./build/minirust input.rsPrepare an input file like input.rs. Then run:
./minirust input.rsThis will:
- Read and print your source
- Tokenize and print all tokens
- Run a basic ownership analysis and print variable states and any errors
- Generate and print LLVM IR for a simple program (currently:
let x = 42; return x;)
- Lexer for a Rust-like subset:
fn,let,return,i64,+,-,*,/,=,->, braces and parens - Minimal AST nodes including variable access and borrow (
&x) - LLVM IR generation via
llvm::IRBuilderin main.cpp - Basic Ownership System:
- Tracks states: Owned, Moved, BorrowedImmut, BorrowedMut
- Detects use-after-move
- Prevents multiple mutable borrows
- Prevents mixing mutable and immutable borrows
Example source in input.rs:
fn main() -> i64 {
let x = 42;
let y = 10;
let sum = x + y;
return sum;
}Run and view output:
./minirust input.rs- Building in PowerShell fails: compile in the "MSYS2 MinGW 64-bit" shell.
llvm-configandclang++are available there. llvm-confignot found: installmingw-w64-x86_64-llvmand ensure you’re in the MinGW 64-bit shell, not the plain MSYS shell.- Headers not found (e.g.,
llvm/IR/IRBuilder.h): use MSYS2 packages; the bare Windows LLVM installer may not includellvm-configand headers in expected locations. - No output when running from PowerShell: run the program from the MSYS2 shell, or ensure the executable exists and runs with
./minirust input.rs.
- Ownership checking is illustrative; it simulates a few core rules but is not a full Rust borrow checker.
- Current IR generation is minimal and hard-coded; extending the parser/IR emission to cover more syntax is the next step.
- Parse
letstatements and expressions into an AST and emit corresponding IR - Expand ownership analysis across real AST nodes and statement boundaries
- Add a simple parser for
fn main() -> i64 { ... }