Skip to content

DannyDaoBoYang/Simple-Rust-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple-Rust-Compiler

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.

Prerequisites

  • Windows with MSYS2
  • Packages in the MSYS2 MinGW 64-bit shell:
    • LLVM, Clang, CMake

Install MSYS2:

winget install MSYS2.MSYS2

Open 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-cmake

Build

In 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)

Build with CMake (recommended)

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.rs

Usage

Prepare an input file like input.rs. Then run:

./minirust input.rs

This 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;)

Features

  • 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::IRBuilder in 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

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

Troubleshooting

  • Building in PowerShell fails: compile in the "MSYS2 MinGW 64-bit" shell. llvm-config and clang++ are available there.
  • llvm-config not found: install mingw-w64-x86_64-llvm and 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 include llvm-config and 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.

Notes

  • 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.

Next Steps

  • Parse let statements 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 { ... }

About

Very basic LLVM compiler for a very small subset of Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published