crustc: entirety of `rustc`, translated to C
121 points
2 hours ago
| 15 comments
| github.com
| HN
lioeters
1 hour ago
[-]
> For the past 3 years, I have been working on compiling Rust to C. .. This is, by my count, the 14th attempt: cilly

Gotta respect the dedication to a niche interest.

> The primary goal of this is support for old/obscure hardware with no LLVM/GCC support.

I remember reading about the bootstrapping question, how it typically requires a Rust compiler to build the Rust compiler from source. https://bootstrapping.miraheze.org/wiki/Bootstrapping_Specif...

Oh, but I see there's a C++ implementation of the Rust compiler. https://github.com/thepowersgang/mrustc

Anyway, this part sounds useful too, that crustc can compile across network and devices.

> You build a small C server on your Blorbo OS, run rustc on some normal platform like Linux, and let cilly talk over the wire.

reply
lispwitch
36 minutes ago
[-]
Guix uses mrustc for bootstrapping Rust, as required for compiler packages; it's a really impressive project and has worked well in that role for some time. This new project is interesting for other reasons though, because mrustc is targeted at the de fact "subset" of Rust in use by rustc at any given time. This looks like it could have broader applications, like compiling Rust programs for platforms not supported by LLVM. If it really targets ANSI C (C89 or so), that's potentially many more platforms than are possible with ordinary rustc

More on the Rust bootstrapping process (2018): https://guix.gnu.org/blog/2018/bootstrapping-rust/

reply
zadikian
1 hour ago
[-]
So the author made a Rust to C transpiler and immediately used it to transpile... the Rust compiler. I love it.
reply
taris2
1 hour ago
[-]
Have you tried Diverse Double-Compiling (DDC) to test if the official rust compiler has a backdoor?

Use crustc to compile the rust source code, producing a new compiler. Then use this new compiler and the official rustc binary, both with deterministic flags, to compile the rust source code again. The two outputs should match bit for bit.

reply
steveklabnik
3 minutes ago
[-]
This was done with mrustc, which produced byte identical output.
reply
swalberg
9 minutes ago
[-]
reply
amir734jj
39 minutes ago
[-]
Awesome idea!
reply
levkk
1 hour ago
[-]
Very cool. At first, I thought it was yet another LLM-generated demo, but no: original work of art. Super cool. Transpiling into C does seem easier than LLVM IR, and letting GCC optimize seems like this might actually work.

Excited to see the compiler implementation when it's out -- a lot to learn from.

reply
ronsor
1 hour ago
[-]
> I put my left hand in a blender. The blender won. (Still have all my fingers, just some stitches). I will not elaborate further.

What a shame. I would've read an article about this.

reply
cozzyd
30 minutes ago
[-]
I mean I get confused every time I use blender too but not to the point of losing fingers...
reply
npalli
31 minutes ago
[-]
Rewrite in C is the new Rewrite in Rust.
reply
ahartmetz
1 hour ago
[-]
Wait, I thought LLVM had a C backend which could be used for the purpose of transpiling Rust to C? Turns out not for a long time, but now maybe again: https://discourse.llvm.org/t/re-ann-llvm-c-backend-still-abl...
reply
linzhangrun
3 minutes ago
[-]
Quite an interesting project, lol. I like the name `crustc` :)
reply
nxtfari
52 minutes ago
[-]
this is really cool but it seems very unlikely that someone targeting an exotic system not supported by rust (mostly embedded and ancient mainframe targets) would be willing to trust a beta transpiler to not inject any bugs or leaks in the process of turning rust to c. nevertheless, very cool.
reply
SpecialistK
1 hour ago
[-]
I wonder if this could be used in PPC Mac OS X, where LLVM isn't supported and most graphical applications need to use GCC 4 with Apple's SDK.
reply
Cadwhisker
1 hour ago
[-]
> 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.

The landing page mentions Plan 9 as one of the systems.

reply
layer8
1 hour ago
[-]
Finally we can rewrite all the Rust in C. ;)
reply
groos
58 minutes ago
[-]
As an ex C++ compiler developer, I heartily approve of this project. Kudos.
reply
Tiberium
2 hours ago
[-]
I wonder how the performance looks like, because this can be interesting even for non-porting reasons ;)
reply
adastra22
1 hour ago
[-]
It is very unlikely that it would be faster.
reply
gerdesj
1 hour ago
[-]
Faster than what? Please finish your sentence.
reply
lpribis
1 hour ago
[-]
Faster than rustc (the main rust compiler written in rust). Obvious from the context.
reply
keepupnow
1 hour ago
[-]
Sonic
reply
dangoodmanUT
1 hour ago
[-]
i believe the author is confused

this is the wrong direction

(jk i read the readme)

reply
Imustaskforhelp
1 hour ago
[-]
This could be used within https://bootstrappable.org/projects.html to make bootstrappability of rust incredibly much easier other than the previous route of OCaml and other things.

I know some folks within the bootstrappable OS projects community are on Hackernews and I hope that they could take a look at this. I feel as if this project could drastically shrink down the efforts needed to get a working rust compiler in a bootstrappable manner.

reply
lispwitch
31 minutes ago
[-]
mrustc (a handwritten Rust compiler in C++) is already used for that in Guix and likely other distros: https://guix.gnu.org/blog/2018/bootstrapping-rust/ This would have other benefits though, as it's both a second bootstrapping path and could potentially compile Rust programs for platforms not supported by LLVM
reply
hkalbasi
1 hour ago
[-]
Not really. This C code is more like a binary and compiler artifact than a source code. So it won't match the standards of bootstrap.
reply