• Stars
    star
    2,009
  • Rank 23,046 (Top 0.5 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Alternative rust compiler (re-implementation)

Mutabah's Rust Compiler

In-progress alternative rust compiler. Capable of building a fully-working copy of rustc, but not suitable for everyday use (due to terrible error messages).

Build Status: windows C/C++ CI

Intro

This project is a "simple" rust compiler written in C++ that is able to bootstrap a "recent" rustc, but may eventually become a full separate re-implementation.

As mrustc's primary goal is bootstrapping rustc, and as such it tends to assume that the code it's compiling is valid (and any errors in the generated code are mrustc bugs). Code generation is done by emitting a high-level assembly (currently very ugly C, but LLVM/cretone/GIMPLE/... could work) and getting an external tool (i.e. gcc) to do the heavy-lifting of optimising and machine code generation.

Progress

  • Builds working copies of rustc and cargo from a release source tarball
    • Supports (and can bootstrap) rustc 1.19.0, 1.29.0, 1.39.0, and 1.54.0
  • Supported Targets:
    • x86-64 linux GNU (fully bootstrap tested using Debian 10.9)
    • x86-64 windows MSVC (runnable executables on Windows 10, but bootstrap hasn't been fully tested)
    • x86_64 and arm64 macOS
    • (incomplete) x86 windows MSVC
  • rustc bootstrap tested and validated (1.19.0 isn't fully repeatable, but later versions are)
    • See the script TestRustcBootstrap.sh for how this was done.

Getting Started

Dependencies

  • C++14-compatible compiler (tested with gcc 5.4 and gcc 6, and MSVC 2015)
  • C11 compatible C compiler (for output, see above)
  • make (for the mrustc makefiles)
  • patch (For doing minor edits to the rustc source)
  • libz-dev (used to reduce size of bytecode files, linux only - windows uses vcpkg to download it)
  • curl (for downloading the rust source, linux only)
  • cmake (at least 3.4.3, required for building llvm in rustc)

Linux GNU and macOS

  • make RUSTCSRC - Downloads the rustc source tarball (1.29.0 by default)
  • make -f minicargo.mk - Builds mrustc and minicargo, then builds libstd, libtest, finally rustc and cargo
  • make -C run_rustc - Build libstd and a "hello, world" using the above-built rustc

BSD

Similar to Linux, but you might need to

  • specify the rustc default target explicitly
  • specify the compiler
  • use gmake to run GNU make

e.g. gmake CC=cc RUSTC_TARGET=x86_64-unknown-freebsd -f minicargo.mk

Windows

(Tested with VS2015)

  • Download and extract rustc-1.29.0-src.tar.gz to the repository root (such that the rustc-1.29.0-src directory is present)
    • NOTE: I am open to suggestions for how to automate that step
  • Open vsproject/mrustc.sln and build minicargo
  • Run vsproject/run_hello.cmd to build libstd and "hello, world", and run it
    • There are other similar scripts for building cargo and rustc. Cargo works, but rustc hasn't fully been tested (building LLVM on windows has been a challenge)

Building non-rustc code

To build your own code with mrustc, first you need to build at least libcore (and probably the full standard library). This can be done on Linux or macOS by running make -f minicargo.mk LIBS, or on windows with build_std.cmd.

Next, run

  • minicargo -L <path_to_libstd> <crate_path> to build a cargo project.
  • or, mrustc -L <path_to_libstd> --out-dir <output_directory> <path_to_main.rs> to directly invoke mrustc.

For additional options, both programs have a --help option.

Diagnosing Issues and Reporting Bugs

Debugging

Both the makefiles and minicargo write the compiler's stdout to a file in the output directory, e.g. when building output/libcore.hir it'll save to output/libcore.hir_dbg.txt. To get full debug output for a compilation run, set the environment variable MRUSTC_DEBUG to a : separated list of the passes you want to debug (pass names are printed in every log line). E.g. MRUSTC_DEBUG=Expand:Parse make -f minicargo.mk

Bug Reports

Please try to include the following when submitting a bug report:

  • What you're trying to build
  • Your host system version (e.g. Ubuntu 17.10)
  • C/C++ compiler version
  • Revision of the mrustc repo that you're running

Support and Discussion

For problems that don't warrant opening an issue (e.g. help in running the compiler), join the IRC channel - irc.libera.chat#mrustc

Current Features

  • Full compilation chain including HIR and MIR stages (outputting to C)
  • MIR optimisations (to take some load off the C compiler)
  • Optional exhaustive MIR validation (set the MRUSTC_FULL_VALIDATE environment variable)
  • Functional cargo clone (minicargo)
    • Includes build script support
  • Procedural macros (custom derive)
  • Custom target specifications
    • See docs/target.md

Plans

Short-term

  • Fix currently-failing tests (mostly in type inference)
  • Fix all known TODOs in MIR generation (still some possible leaks)

Medium-term

  • Propagate lifetime annotations so that MIR can include a borrow checker
  • Emit C code that is (more) human readable (uses names from the original source, reduced/no gotos)
  • Add alternate backends (e.g. LLVM IR, cretonne, ...)

More Repositories

1

rust_os

An OS kernel written in rust. Non POSIX
Rust
666
star
2

rust-barebones-kernel

A barebones framework for a rust kernel.
Assembly
258
star
3

acess2

Acess2 Hobby Operating System
C
80
star
4

rust-cc

A C Compiler/Parser written in Rust
Rust
49
star
5

stack_dst-rs

Stack-allocated DSTs for rust (fixed capacity)
Rust
44
star
6

va_list-rs

A rust implementation of the va_list type from `stdarg.h`
Rust
19
star
7

tag_safe

A plugin to rustc that emits warnings on tagged functions
Rust
10
star
8

utf16_literal-rs

Rust procedural to provide UTF-16 string literals
Rust
9
star
9

equation-editor

Equation editor built in rust, intended to handle very large expressions
Rust
8
star
10

cmdline_words_parser

A rust string parser that extracts 'words' using the same rules as posix shells
Rust
6
star
11

rme2

Real-Mode Emulator - Emulates 16-bit x86
C
5
star
12

array_iter-rs

By-value iterators for fixed-size arrays
Rust
4
star
13

logiccircuit

Simplistic logic gate simulator
C
4
star
14

fury3clone

Clone of the game "Fury3" (engine only, loads official datafiles)
Rust
4
star
15

rust-logiccircuit

A rust port of my LogicCircuit simulator
Rust
2
star
16

cits3200

CITS3200 Professional Computing 2011 Sem 2
C#
2
star
17

binaryview2

Executing disassembler and type tracer
Rust
1
star
18

rust-utf8reader

An inline UTF-8 decoder that reads a stream of code-points from a "Reader"
Rust
1
star
19

fyp-boat

2017 Final-year project, Self-guided boat
C++
1
star
20

inverse_fft

Hackjob image->audio converter
Rust
1
star
21

legacy-acess

LEGACY: A repository containing a snapshot of my original OS project
C
1
star
22

anvil_find_block

Locates a specific block IDs in the Minecraft 'Anvil' file format.
C
1
star
23

tunic_text

A single-file HTML tool to type and translate the text from the adventure game "Tunic"
HTML
1
star