crustc translates the entire rustc compiler to C

crustc translates the entire rustc compiler to C

crustc translates the entire rustc compiler to C

crustc is a demo that converts the complete source of rustc 1.98.0‑nightly (c712ea946 2026‑06‑16) into roughly 46 million lines of C. The generated C code compiles with a standard GCC toolchain and produces a working Rust compiler capable of building the standard library.


What crustc actually does

crustc is a transpiler built on the author’s private cilly toolchain. cilly is a Rust library that emits portable ANSI‑C code and a compiler backend plugin that drives the translation. The key ideas are:

  • Compiler‑agnostic outputcilly probes the target C compiler for supported language features (e.g., _Thread_local, type sizes) and tailors the generated C accordingly.
  • Network transparency – the tool can talk to a remote C compiler over TCP (or even UART), allowing a powerful host to drive compilation for devices that lack a native C toolchain.
  • Makefile generation – optional markers in the generated objects let cilly reconstruct a directory of makefiles, enabling traditional make‑based builds of Rust code via the C compiler.

The current repository only showcases the compiler compiling itself, which the author calls the “flashiest showcase”.


Why translate rustc to C?

The primary motivation is hardware support. Many legacy or obscure platforms lack LLVM or recent GCC versions but still have a C compiler. By converting Rust to C, those systems can run Rust programs without needing a full LLVM stack.

"The primary goal of this is support for old/obscure hardware with no LLVM/GCC support. There are still some systems out there that don't support Rust but support C." – crustc README

This also sidesteps the classic Rust bootstrapping paradox: a Rust compiler can be built on a machine that only provides a C compiler, then used to compile Rust for the target platform.


How the build works

  1. Prerequisites – a modern GCC (the author used GCC 13.3.0) and the matching LLVM libraries that the upstream rustc expects (libLLVM.so.22.1‑rust‑1.98.0‑nightly).
  2. Compilation – run make -j20 LLVM_LIB_DIR=~/.rustup/toolchains/nightly-2026-06-16-aarch64-unknown-linux-gnu/lib. The build produces a rustc binary written in C.
  3. Running – set LD_LIBRARY_PATH to point at the LLVM libs and execute ./rustc/rustc --version to see the original version string.

The author notes that optimizations should be disabled; with CFLAGS=-g the full build finishes in about 1 minute 18 seconds on a 20‑core machine. Enabling -O2 often leads to crashes on large source files.


Current limitations and bugs

  • The generated C is target‑specific; code produced for ARM64 will not run on RISC‑V without re‑generation.
  • Some ABI mismatches remain, especially on ARM64 where the compiler cannot represent the sret calling convention used by Rust for small structs.
  • Crustc crashes when executed from the repository root due to a path‑canonicalisation issue (the author admits they are also confused by this bug).
  • The cilly toolchain is not yet public; the author cites personal constraints (new job, thesis, and a literal blender incident) as reasons for the delay.

Community reaction

  • Dedication – Commenter @lioeters praised the 14‑year effort behind cilly and highlighted the bootstrapping relevance.
  • Comparison to existing projects@ahartmetz reminded readers of LLVM’s historical C backend and linked to recent discussions about its revival.
  • Practicality concerns@nxtfari expressed skepticism about trusting a beta transpiler for security‑critical or legacy systems, but still found the concept exciting.
  • Performance curiosity@Tiberium asked how the C‑generated code performs compared to native LLVM‑generated binaries, hinting at potential non‑porting use cases.
  • Bootstrappable OS interest@Imustaskforhelp suggested crustc could simplify bootstrappable Rust builds, reducing reliance on OCaml or other heavy toolchains.

Overall, the community sees crustc as a novel proof of concept with intriguing implications for portability, bootstrapping, and cross‑compilation, while recognizing that the tooling is still experimental.


Outlook

crustc demonstrates that a full Rust compiler can be expressed in ANSI‑C and built with conventional C toolchains. If the author eventually releases the full cilly suite, developers could target any C‑compatible platform, from Plan 9 VMs to obscure embedded CPUs, without writing a custom LLVM backend. Until then, crustc remains a compelling showcase of what is technically possible when Rust is treated as a source‑to‑source transformation problem.

Sources