Skip to content

tamaroning/mini-rustc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini-rustc

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.

Requirement

  • Cargo

Also, llc is required to compile LLVM IR to executables.

Build & Run

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.

Test

Run the following command:

$ ./test.sh

Compile Hello world!

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!

Status

  • 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
    • impls
    • Trait & Trait impls
  • 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
    • Modules mod
      • Visibility (pub) is not suported
    • Global variables
  • statements
    • let statement
      • Keyword mut is not supported
    • 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 of return)
    • 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
  • Others
    • Paths
    • Patterns (Pattern matching)
    • Comments //
    • unsafe
      • block
      • fn
  • Internal
    • Name Resolution
    • Shadowing
    • Type Resolution

ABI

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

Problem of ambiguous grammars

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 { () () }

References

About

A toy Rust compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published