crustc: Entirety of rustc, translated to C
89 points by ajdecon
89 points by ajdecon
It can generate "witness" programs, which check what a given compiler and platform support:
I find it super weird that this is how the classical C configure build "chain" works in general. And it makes a lot of sense that this compiler (transpiler?!) adapts that pattern!
This is, by my count, the 14th attempt: cilly.
Well that's some dedication! I wish I had such persistence.
For comparison, I happen to have entirety of Zig compiler translated to C handy because it is part of how we routinely build from source.
It's 4.6 million lines, so almost exactly one order of magnitude smaller than this project. The generated C code is target specific (because comptime code can observe the target), but it is not compiler-specific.
Maybe dumb question from someone who's not (at least not yet) a systems programmer – would a project like this hypothetically impact the choice to use Rust vs Zig? Since Zig's selling points include its support for a wide range of cross-compilation targets, and its self-hosted toolchain (with LLVM an optional dependency), this project's promised ability to compile Rust to obscure-platform-specific C seems like a bit of a shot across the bow.
Not by using crustc, because that translated compiler still supports the same compilation targets as the original one. But you could translate another Rust project to C to run and compile that on a C-only target.
Don't overestimate the impact of this though: It's a slightly cumbersome solution to a very niche problem. Rustc already has a wide compilation target support, with more coming via its gcc backend. Zig cross-compilation tooling is cool (especially for the C parts, which are more troublesome than Zig or Rust), but it's more about making things more convenient than about supporting more targets.
In the end, target support is already very similar between Zig and Rust. It's unlikely to be the deciding factor, there are many more significant differences between the two languages.
This is cool, but isn't it still going to be limited by the capabilities of LLVM?
Why would it be? crustc is just an example of what cilly (the Rust to C toolchain) can do:
The full
cillytoolchain compiles your own Rust to C for arbitrary targets. This repo just shows the compiler compiling itself, as I believe this is the flashiest showcase I could do.
well, crustc is still just rustc, unless cilly changes something internally it should still depend on LLVM, right?
AIUI cilly is a rustc backend that replaces the llvm backend, much like rustc_codegen_cranelift.
See also mrustc (https://github.com/thepowersgang/mrustc) - a written-in-C++ Rust compiler that works by transpiling to C (and then handing over to GCC) and so which can also transpile rustc to C.
Definitely a stupid question: but why is it hard to write a codegen backend that outputs C89 or whatever C code? Surely we're not targeting non-compliant compilers too?
Niche platforms that LLVM hasn't bothered with supporting are generally exactly the kind of platforms that only have non-compliant C compilers.
Generating C99 (or newer) code likely allows for more performant or succinct code. When you only target the minimum common denominator, you end up with a mediocre result.