HN
Today

crustc: entirety of `rustc`, translated to C

A developer has translated the entire rustc compiler into 46 million lines of C, creating a functional Rust compiler (crustc) that can be built with GCC and Make. This monumental technical undertaking is a proof-of-concept for their cilly toolchain, which aims to bring Rust to obscure hardware and platforms by adapting to any C compiler. It represents a significant step towards broadening Rust's reach to environments traditionally considered inaccessible due to lack of modern toolchain support.

24
Score
1
Comments
#2
Highest Rank
19h
on Front Page
First Seen
Jul 2, 11:00 PM
Last Seen
Jul 3, 5:00 PM
Rank Over Time
233324665447881114142022

The Lowdown

This project, crustc, presents a remarkable achievement: a fully functional Rust compiler, specifically rustc 1.98.0-nightly, entirely translated into C code. Weighing in at 46 million lines, this C-based compiler can be built using standard tools like GCC and Make, showcasing the capabilities of its underlying cilly toolchain. The developer's primary motivation is to expand Rust's compatibility to older or more obscure hardware and operating systems that currently lack support for modern Rust toolchains like LLVM or GCC.

  • crustc is a direct C translation of a recent rustc nightly build, providing a functional Rust compiler in C. It supports compiling Rust's core libraries such as core, alloc, and std.
  • The project serves as a demonstration for cilly, a new Rust library and compiler backend designed to translate Rust code into C for arbitrary targets. This is the 14th attempt by the author to achieve this goal over three years.
  • A key innovation of cilly is its adaptability to various C compilers. It generates "witness" programs to probe a given compiler and platform for specific features, type layouts, sizes, alignments, and integer formats, ensuring compatibility with even highly specialized C environments.
  • cilly makes minimal assumptions, striving to adhere to ANSI C standards and providing workarounds for modern C features like strict aliasing. However, the generated C code is compiler-specific and not directly portable between different architectures (e.g., Arm64 C code won't run on RISC-V).
  • The tool supports "network transparency," allowing rustc to run on a powerful host machine while cilly communicates with a C compiler on a target machine (e.g., Plan 9 VM via TCP) to compile Rust code remotely.
  • cilly can generate Makefiles from its intermediate representation, facilitating the build process for Rust projects using traditional C build systems.
  • While cilly aims for ABI compatibility with standard rustc output, there are nuances, particularly on platforms like Arm64, where rustc uses ABIs not directly representable in C.
  • The cilly toolchain is not yet publicly available, with the author citing ongoing development, employment, and academic commitments, along with some optimization-related bugs, as reasons.

This project significantly pushes the boundaries of Rust's portability, offering a path for the language to thrive in environments previously considered out of reach, potentially removing a long-standing barrier to Rust adoption in specialized embedded or legacy systems.