NOTE: This compiler is under development now
mini-rustc a toy Rust compiler written in Rust from scratch which outputs LLVM IR. This compiler implements typecheck but not other static analyses like lifetime, mutability, or unsafety. If you find a bug, feel free to open an issue to report it!
mini-rustc has been much inspired by GCC Rust and Rui Ueyama's compiler book. Big thanks to these wonderful materials/software.
- Cargo
Also, llc is required to compile LLVM IR to executables.
To build mini-rustc, run the following command:
$ git clone <this repo>
$ cd mini-rustc
$ cargo build
To compile Rust code, run the following command:
$ cargo run <file>
or
$ cargo run <source>
Generated LLVM IR is output to stdout.
Run the following command:
$ ./test.sh
examples/hello.rs
contains:
extern "C" {
fn puts(s: &str) -> i32;
}
fn main() -> () {
unsafe {
puts("Hello mini-rustc!");
};
}
Run the follwoing commands:
$ cargo run examples/hello.rs > tmp.ll
$ llc tmp.ll -o tmp.s -opaque-pointers # this option is required!
$ gcc tmp.s -o a.out
$ ./a.out
Hello mini-rustc!
- Type system
- Primitives
i32
,bool
, unit(()
), never(!
),str
,*const T
- References
-
&'static str
- But not represented as a fat pointer.
-
- Arrays
- ADTs
- (Nested) Structs
- Enums
- Typechecking
- Type inference
- Generics
- Type cast
-
&T
to*const T
-
*const U
to*const V
-
-
impl
s - Trait & Trait
impl
s
- Primitives
- items
- Structs
- Functions
- Return type cannot be omitted
- Struct params and returning structs are not supported
-
extern
blocks (e.g.extern "C" { ... }
)- Only
"C"
is available
- Only
- Modules
mod
- Visibility (
pub
) is not suported
- Visibility (
- Global variables
- statements
-
let
statement- Keyword
mut
is not supported
- Keyword
- Expression statements
- Expression with
;
-
- expressions
- Arithmetic operators
+
,-
,*
- Comparison operators
==
,<
,>
- Literals: integer, boolean, string
-
if-else
expressions - Block expressions
{ ... }
- Return expressions
return expr
- Omitting expression is not supported (i.e. Use
return ()
instead ofreturn
)
- Omitting expression is not supported (i.e. Use
- Call expressions
func(params...)
- Parameter passing: ZSTs and ADTs are supported
- Return value: ADTs and arrays are not supported
- Array expressions
[expr, expr, ...]
- Struct expressions
SomeName { field1: expr, .. }
- Field expressions
strct.field
- Index expressions
array[index]
- Paths in expressions
a
,crate::foo
- Arithmetic operators
- Others
- Paths
- Patterns (Pattern matching)
- Comments
//
unsafe
- block
-
fn
- Internal
- Name Resolution
- Shadowing
- Type Resolution
mini-rustc's ABI is similar to system V ABI, but not fully compatible. When functions are called, arrays and ADTs are passed via memory, ZST parameters are ignored (not passed).
I have developed the parser refering to Rust Reference, but mini-rustc cannot parse several grammars correctly. I will investigate rustc or other compilers to fix it.
examples:
// How do we decide condition is ident or struct expr?
fn main() -> i32 { if some_ident { 3 } else { 4 } }
// How do we decide this expr is a function call or two expr stmts?
fn main() -> i32 { () () }
- gccrs: https://github.com/Rust-GCC/gccrs
- Rui Ueyama's compiler book: https://www.sigbus.info/compilerbook
- chibicc: https://github.com/rui314/chibicc/
- Rustc Dev Guide: https://rustc-dev-guide.rust-lang.org/
- The Rust Reference: https://doc.rust-lang.org/reference/
- Rust Compiler: https://github.com/rust-lang/rust