logo
Kurashizu Blogwhere ideas flow
  • Home
  • News
  • Blog
  • About
  • Home
  • News
  • Blog
  • About

© 2026 Kurashizu. All rights reserved.

Admin·New Post·Service Status
Powered by Cloudflare·02.11.26
← Back to news

crustc: entirety of `rustc`, translated to C

Original article
github.com|360 points|81 comments|by Philpax|Jul 2, 2026

🦀 crustc: The Rust Compiler, Reimagined in C

Project Banner

crustc is a monumental technical demonstration: the complete source of rustc (version 1.98.0-nightly, commit c712ea946 from 2026-06-16) translated into approximately 46 million lines of C code.

This is not just a transpilation exercise; it is a fully functional Rust compiler that can be assembled using GCC and make.


🛠️ Building and Running

To get this beast running, you need to link it against the appropriate LLVM libraries. The author opted to embed random binaries not to include pre-built LLVM binaries to keep the repository clean.

Compilation

You must provide the path to libLLVM.so.22.1-rust-1.98.0-nightly.

make -j20 LLVM_LIB_DIR= ~/.rustup/toolchains/nightly-2026-06-16-aarch64-unknown-linux-gnu/lib

Execution

Because rustc relies on LLVM, you must set your LD_LIBRARY_PATH so the driver can locate the shared objects:

LD_LIBRARY_PATH= ~/.rustup/toolchains/nightly-2026-06-16-aarch64-unknown-linux-gnu/lib:./rustc_driver ./rustc/rustc --version

Expected Output: rustc 1.98.0-nightly (c712ea946 2026-06-16)

This compiler is fully capable of building the standard library, including core, alloc, and std.


⚙️ The Engine: The cilly Toolchain

crustc serves as a "flashy" showcase for a broader project called cilly. While crustc shows the compiler compiling itself, cilly is a general-purpose toolchain designed to compile any Rust code into C for any target.

How it Works

cilly consists of a Rust library for C code generation and a specialized compiler backend (plugin).

The core innovation is its adaptive nature. Instead of assuming a target's capabilities, cilly generates "witness" programs to probe the host C compiler's features.

Example Witness Logic:

/* This compiles if and only if our C compiler supports _Thread_local. */
_Thread_local int KEYWORD_TLS_SUPPORTED;

The toolchain queries the environment for:

  • Type Layouts: sizeof(float)=?sizeof(double)\text{sizeof}(\text{float}) \stackrel{?}{=} \text{sizeof}(\text{double})sizeof(float)=?sizeof(double)
  • Alignments & Encodings: Ensuring ASCII compatibility.
  • Integer Formats: Verifying two's complement.

cilly adheres strictly to ANSI C, implementing workarounds for modern standards (like strict aliasing) to ensure maximum compatibility.

The Trade-off

Because it adapts to the specific compiler used, the output is compiler-specific.

  • C code generated for ARM64 ≠\neq= C code for RISC-V 32.
  • The crustc build provided is specifically targeted for ARM64 Linux.

🌐 Architecture & Use Cases

The primary motivation is to bring Rust to ancient or obscure hardware that lacks LLVM or GCC support but does have a C compiler.

Solving the Bootstrap Paradox

cilly is network-transparent. If you have a target (e.g., "Blorbo OS") without a cross-compiler, you can:

  1. Run a small C server on the target hardware.
  2. Run rustc on a standard Linux machine.
  3. Let cilly communicate over TCP (or even UART) to compile the code.

Real-world Proof: The author successfully compiled Rust programs for x86 Plan9 VMs from an ARM64 Linux host.

Plan9 Terminal Output:

term% echo `{cat /dev/sysname} osversion `{cat /dev/osversion} cputype $cputype
gnot osversion 2000 cputype 386
term% /tmp/hello_plan9
Hello, world!

term% nm /tmp/hello_plan9 | grep rust_begin_unwind
1020 T _RNvCshfEkAwg4zv6_7___rustc17rust_begin_unwind

Advanced Build Features

  • IR Caching: Saves Intermediate Representation to a cache.
  • Marker Embedding: Places markers in object files to split functions/globals.
  • Makefile Generation: Automatically creates Makefiles for building Rust projects via C.
  • ABI: The resulting code is largely ABI-compatible with standard rustc output.

📋 Reproduction Details

Environment Specifications

ComponentSpecification
OSUbuntu 24.04 (Kernel 6.17.0-1021-nvidia)
Architectureaarch64 (ARM64)
C CompilerGCC 13.3.0
LinkerUbuntu LLD 18.1.3

Configuration Example

To define a target (e.g., for sdcc on Z180), cilly uses a JSON-like configuration:

{
  "triple": ["sdcc_z180-unknown-none"],
  "tool_def": {
    "kind": "local",
    "compile": {
      "base": {
        "executable": "/usr/bin/sdcc",
        "base_args": ["-mz180", "--std-c89", "-c"],
        "input_arg_template": ["{input}"]
      }
    }
  }
}

Final Checklist for Setup

  • Install make and gcc.
  • Obtain rustc nightly toolchain for aarch64-unknown-linux-gnu.
  • Locate libLLVM.so.22.1-rust-1.98.0-nightly.
  • Set LD_LIBRARY_PATH before execution.