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.
lispwitch 1 days 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
Gccrs is valuable for various reasons (bootstrapping, refining the spec, etc), but for the main purpose of "compiling to targets supported by gcc but not llvm", it's definitely not as promising as https://github.com/rust-lang/rustc_codegen_gcc. It's a rustc backend, at the same level as llvm or cranelift, it's much further along than gccrs (already available as a rustup component and can compile any rust code) and will not suffer from lagging behind rustc.
But OP has wider ambitions: the real niche targets are supported by neither gcc nor llvm. A rust-to-c transpiler that can adapt to weird C compilers should have a very wide reach.
Narishma 10 hours ago [-]
But what about platforms that even GCC doesn't support well? I'm thinking mostly retro stuff like 16-bit x86.
lioeters 10 hours ago [-]
I might have seen that Bootstrapping Rust article, how interesting. I've been curious about Guix for a while, one of these days I need to dive in and explore deeper. I really enjoyed their article on full-source bootstrap, it's relevant now more than ever.
> The primary goal of this is support for old/obscure hardware with no LLVM/GCC support.
If you're going to go to all this effort for an old target though, wouldn't the effort be better spent on making it an LLVM target? Then you'd get Rust and a bunch of other languages for free.
But maybe there are required parts of the LLVM IR that make this undesirable for certain targets, maybe requiring specific hardware features, I don't know. I guess also WASM-as-IR is a possible way to go. (Is that a thing?)
Edit: sorry I see that this point was already raised in this thread by ivanjermakov. Ignore.
lioeters 10 hours ago [-]
> WASM-as-IR .. (Is that a thing?)
Yes, actually I've heard of some projects compiling a language to Wasm, then using wasm2c to translate to C.
I get the feeling this is an underappreciated technique with more potential.
zadikian 1 days ago [-]
So the author made a Rust to C transpiler and immediately used it to transpile... the Rust compiler. I love it.
Animats 21 hours ago [-]
It makes sense as a porting tool, if you need to port the Rust compiler to some target that has a C compiler.
But it doesn't mean rustc generates code for that target, only that you can run it there. You'd still have to teach LLVM about the target. Although that might already have been done.
It's not that useful for retro computers because the Rust compiler needs too much memory for most machines of the 32-bit era.
sylware 12 hours ago [-]
As far as I know, ISO c++ is no better than microsoft rust.
It is said microsoft rust syntax is on the same brain damage complexity level than ISO c++. That, and I don't even talk about the technical cost of its runtime (not far from the toxicity of a jvm?)
slashdev 11 hours ago [-]
What is Microsoft rust? I’ve never heard of such a thing. Or did I miss the joke?
hmry 11 hours ago [-]
I don't think there's a joke, this commenter seems to believe Rust is controlled by Microsoft
sylware 8 hours ago [-]
Don't think I am alone to know that...
taris2 1 days 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.
steveklabnik 1 days ago [-]
This was done with mrustc, which produced byte identical output.
rcxdude 1 days ago [-]
Better than that, you can get a bootstrapped rust from the Guix project, which has bootstrapped its entire system from source code from only a tiny verifiable binary.
wmanley 19 hours ago [-]
It's not diverse in that case - it's the same compiler source compiled to binaries twice - it's just that with one compiler you've gone via a C intermediate representation. For the purposes of diversity it's the same as compiling rustc with the cranelift/gcc backend.
moltonel 13 hours ago [-]
From a "trusting trust" point of view, compiling rustc-translated-to-C with a C compiler (and comparing the result to normally-compiled rustc) is a valid demonstration, because we're again starting from code and can eliminate the possibility of a binary-resident compromise.
The actual issue here is that the translation was done using a rustc backend, and therefore an existing rustc binary which could be compromised and inject a "if (user=="wmanley") {...}" that isn't present in the original Rust code. If cilly was completely standalone (like mrustc), or if you had a rustc+cilly build you trusted, there would be no issue.
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.
ronsor 1 days 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.
RustyRussell 18 hours ago [-]
Nobody who would code this up would be entirely sane. But I can't help thinking "these are my people" when I read this...
gpvos 11 hours ago [-]
He wrote 14 Rust to C compilers. Sanity has gone out of the window already.
cozzyd 1 days ago [-]
I mean I get confused every time I use blender too but not to the point of losing fingers...
This approach is harder than you might imagine. LLVM can do a lot of things that don't map to C language constructs. You cannot generally roundtrip arbitrary LLVM IR through some C representation. You can emulate most things, but you won't necessarily get the same LLVM IR in the other end.
afdbcreid 19 hours ago [-]
FWIW, Rust can also do a lot of things that do not easily map to C (at least standard C) constructs. For example:
- You can compare any two pointers, while in C they must point to the same allocation. This is possible to solve by converting to integers first.
- Signed integer overflow is UB in C, defined to wrap/panic in Rust.
- Type-based alias analysis is a big one, does not exist in Rust.
tialaramex 13 hours ago [-]
> You can compare any two pointers, while in C they must point to the same allocation. This is possible to solve by converting to integers first.
Indeed specifically Rust defines that pointer comparisons are done by address, whereas C doesn't specify what the rules are exactly. This gets sticky if the pointer was invalidated (e.g. you free'd the memory it was pointing at). Rust says - as you might expect - that you can still compare this invalid pointer to another pointer (which may or may not still be valid and indeed might be a valid pointer to the same address!) because we're only comparing the address - but C++ says you mustn't do that and I believe C has the same rule.
> Signed integer overflow is UB in C, defined to wrap/panic in Rust.
This doesn't feel like an interesting difference. C made a weird choice, which was maybe convenient for the usual Worse Is Better reasons 50+ years ago. LLVM doesn't care about that choice, LLVM can cheerfully simulate this behaviour in C if you want that.
> Type-based alias analysis is a big one, does not exist in Rust.
This also doesn't feel relevant but I guess I'm interested in why you think it would make "a big" difference?
Edited: I confirmed C11 has "pointer zap" which is the behaviour I described above, and I believe C23 also still has pointer zap although paulmck and co. are trying to get rid of it or do something on this topic in C2y.
Cadwhisker 1 days 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.
layer8 1 days ago [-]
Finally we can rewrite all the Rust in C. ;)
avadodin 18 hours ago [-]
As long as we rewrite all the C++ in Rust first.
ivanjermakov 19 hours ago [-]
> The primary goal of this is support for old/obscure hardware with no LLVM/GCC support
Wouldn't it be easier to add old hardware support to LLVM/GCC instead? I adore the project scale and determination, but for this goal extending existing projects seems more logical than building a language translator.
Perz1val 18 hours ago [-]
> Wouldn't it be easier to add old hardware support to LLVM/GCC instead?
No, in fact it's much, much harded. You have no idea of the scope. I have no idea of the scope. Nobody does. There are obscure machines we've never heard about and there are C compilers for them. Targeting and supporting them from modern toolchains is a fool's errand.
jitl 14 hours ago [-]
rust to c supports infinite platforms that already have a c compiler by implementing a single rust to c program
on the other hand, porting llvm to an infinite number of platforms requires an infinite amount of work
so, it is less work this way
claudex 19 hours ago [-]
Some architecture lacks documentation, if you have a working C compiler, it's easier to use it than working on a compiler to target it.
Liquid_Fire 15 hours ago [-]
Presumably you still need a C++ compiler for LLVM itself though. Or... a C++ to C translator.
Edit: On second thought, that's only needed if you want to run rustc itself on the old hardware, which is probably not super useful given the main reason you would need to do this is if LLVM can't target that hardware.
For building code written in Rust for such old hardware, this would be sufficient.
npalli 1 days ago [-]
Rewrite in C is the new Rewrite in Rust.
keyle 19 hours ago [-]
GTA VI out in Rust before C++?
roflcopter69 18 hours ago [-]
Wake me up when consoles officially support Rust. Until then there's no real way around C/C++ if you want to publish a real game to real consoles. But there's https://akaganite.com so it might become possible soon™
maximilianburke 12 hours ago [-]
They don’t need to “officially support” Rust in order to ship code written with Rust. Just target the applicable architecture and ABI. It’s a lot easier now that the ABI’s are mostly standard now too.
Source: I wrote a compiler and runtime that ran .NET code (AoT compiled with LLVM) on PS3/Xbox360/Wii and shipped a few games with it.
groos 1 days ago [-]
As an ex C++ compiler developer, I heartily approve of this project. Kudos.
SpecialistK 1 days 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.
adithyassekhar 20 hours ago [-]
I'll wait for rustcrustc
Archit3ch 10 hours ago [-]
If I understand this correctly, you could run this compiler on iPadOS (since it's plain C). But you cannot mark it's output executable, since that would require JIT entitlement.
ronsor 10 hours ago [-]
Target WASM then
Archit3ch 9 hours ago [-]
Sure, that works. Caveats:
1. Your frontend needs to support emitting WASM. Ok for Rust, not for every language out there.
2. 2x-4x performance overhead.
db48x 16 hours ago [-]
We should port it to Rust.
kube-system 12 hours ago [-]
What sort of hardware would this be useful for?
Imustaskforhelp 1 days 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.
lispwitch 1 days 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
hkalbasi 1 days 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.
afdbcreid 19 hours ago [-]
Depending on what your goal is. If it is eliminating trusting trust attacks, yes this is no enough. But more commonly you only want to compile rustc for a platform it was never compiled on, and for that this project is definitely enough.
Tiberium 1 days ago [-]
I wonder how the performance looks like, because this can be interesting even for non-porting reasons ;)
adastra22 1 days ago [-]
It is very unlikely that it would be faster.
gerdesj 1 days ago [-]
Faster than what? Please finish your sentence.
lpribis 1 days ago [-]
Faster than rustc (the main rust compiler written in rust). Obvious from the context.
gerdesj 1 days ago [-]
[flagged]
keepupnow 1 days ago [-]
Sonic
nxtfari 1 days 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.
micheleeet 18 hours ago [-]
Rust is finally memory safe enough to become C
Sharlin 18 hours ago [-]
"You have become the very thing you swore to destroy"
sylware 12 hours ago [-]
The real salvation is assembly written software with a wordwide non-IP-locked ISA (for instance RISC-V). Then very high language interpreters would be written directly in such assembly.
westurner 13 hours ago [-]
There are discussions of round-trip between Rust and C;
The most interesting part of this to me is not “Rust to C” by itself, but the fact that it widens the pool of people who can help debug portability problems.
There are relatively few people who understand Rust’s compiler internals, LLVM backends, and obscure target support deeply. But there are many engineers who understand C compilers, ABIs, linkers, makefiles, cross-compilation, old operating systems, and weird platform-specific compiler behavior.
If Rust can be lowered into target-specific C, then some problems stop being exclusively “Rust compiler problems” and also become C toolchain problems. That means more people can inspect the generated C, build failures, linker errors, ABI mismatches, and compiler-specific behavior.
C is obviously not a magic portability layer. ABI details, integer widths, alignment, TLS, aliasing, and undefined behavior still matter. But as an ecosystem boundary, C gives many more engineers a way to participate in debugging and porting work.
I think that social/maintenance aspect may be more important than the language translation itself.
jolmg 21 hours ago [-]
Guideline:
> Don't post generated text or AI-edited text. HN is for conversation between humans.
jdw64 20 hours ago [-]
I saw the reply, and it's not GenAI text. It's just that in the process of translation, people usually use machine translation or LLM translation. The problem is the vocabulary we East Asians use. I experience this issue too.
Probably because in East Asia, we tend to emphasize things with things like "xx" a lot.
jolmg 19 hours ago [-]
I thought that may be the case. I shared the guideline so they'd know the likely reason all their comments die immediately.
However, their comments are consistently long, so it may be GenAI after all. Their last comment in particular...
You may be right. It's a really difficult problem.
jdw64 20 hours ago [-]
I think you're probably using DeepL or some other AI translation. When you use DeepL, most sentences become flat and end up being judged as GenAI. I also used DeepL to communicate on Hacker News in the early days.I had a similar problem
dangoodmanUT 1 days ago [-]
i believe the author is confused
this is the wrong direction
(jk i read the readme)
sylware 12 hours ago [-]
Nope, and c++ should get the same thing.
But we now all know rust is microsoft and c++ ISO. I don't think we can trust much more ISO than microsoft to produce less feature creep over the currently known planned obsolescence cycle of 5-10 years.
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.
More on the Rust bootstrapping process (2018): https://guix.gnu.org/blog/2018/bootstrapping-rust/
For this, gcc-rs[1][2] is the most promising candidate.
[1] https://rust-gcc.github.io/
[2] https://github.com/Rust-GCC/gccrs
But OP has wider ambitions: the real niche targets are supported by neither gcc nor llvm. A rust-to-c transpiler that can adapt to weird C compilers should have a very wide reach.
Building from Source All the Way Down - https://guix.gnu.org/en/blog/2023/the-full-source-bootstrap-...
If you're going to go to all this effort for an old target though, wouldn't the effort be better spent on making it an LLVM target? Then you'd get Rust and a bunch of other languages for free.
But maybe there are required parts of the LLVM IR that make this undesirable for certain targets, maybe requiring specific hardware features, I don't know. I guess also WASM-as-IR is a possible way to go. (Is that a thing?)
Edit: sorry I see that this point was already raised in this thread by ivanjermakov. Ignore.
Yes, actually I've heard of some projects compiling a language to Wasm, then using wasm2c to translate to C.
https://github.com/WebAssembly/wabt/blob/main/wasm2c/README....
The Zig project does something similar for their bootstrapping process with a custom wasm2c implementation with just the features they need.
https://github.com/ziglang/zig/blob/master/stage1/wasm2c.c
I get the feeling this is an underappreciated technique with more potential.
But it doesn't mean rustc generates code for that target, only that you can run it there. You'd still have to teach LLVM about the target. Although that might already have been done.
It's not that useful for retro computers because the Rust compiler needs too much memory for most machines of the 32-bit era.
It is said microsoft rust syntax is on the same brain damage complexity level than ISO c++. That, and I don't even talk about the technical cost of its runtime (not far from the toxicity of a jvm?)
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.
The actual issue here is that the translation was done using a rustc backend, and therefore an existing rustc binary which could be compromised and inject a "if (user=="wmanley") {...}" that isn't present in the original Rust code. If cilly was completely standalone (like mrustc), or if you had a rustc+cilly build you trusted, there would be no issue.
Excited to see the compiler implementation when it's out -- a lot to learn from.
What a shame. I would've read an article about this.
- You can compare any two pointers, while in C they must point to the same allocation. This is possible to solve by converting to integers first.
- Signed integer overflow is UB in C, defined to wrap/panic in Rust.
- Type-based alias analysis is a big one, does not exist in Rust.
Indeed specifically Rust defines that pointer comparisons are done by address, whereas C doesn't specify what the rules are exactly. This gets sticky if the pointer was invalidated (e.g. you free'd the memory it was pointing at). Rust says - as you might expect - that you can still compare this invalid pointer to another pointer (which may or may not still be valid and indeed might be a valid pointer to the same address!) because we're only comparing the address - but C++ says you mustn't do that and I believe C has the same rule.
> Signed integer overflow is UB in C, defined to wrap/panic in Rust.
This doesn't feel like an interesting difference. C made a weird choice, which was maybe convenient for the usual Worse Is Better reasons 50+ years ago. LLVM doesn't care about that choice, LLVM can cheerfully simulate this behaviour in C if you want that.
> Type-based alias analysis is a big one, does not exist in Rust.
This also doesn't feel relevant but I guess I'm interested in why you think it would make "a big" difference?
Edited: I confirmed C11 has "pointer zap" which is the behaviour I described above, and I believe C23 also still has pointer zap although paulmck and co. are trying to get rid of it or do something on this topic in C2y.
The landing page mentions Plan 9 as one of the systems.
Wouldn't it be easier to add old hardware support to LLVM/GCC instead? I adore the project scale and determination, but for this goal extending existing projects seems more logical than building a language translator.
No, in fact it's much, much harded. You have no idea of the scope. I have no idea of the scope. Nobody does. There are obscure machines we've never heard about and there are C compilers for them. Targeting and supporting them from modern toolchains is a fool's errand.
on the other hand, porting llvm to an infinite number of platforms requires an infinite amount of work
so, it is less work this way
Edit: On second thought, that's only needed if you want to run rustc itself on the old hardware, which is probably not super useful given the main reason you would need to do this is if LLVM can't target that hardware.
For building code written in Rust for such old hardware, this would be sufficient.
Source: I wrote a compiler and runtime that ran .NET code (AoT compiled with LLVM) on PS3/Xbox360/Wii and shipped a few games with it.
1. Your frontend needs to support emitting WASM. Ok for Rust, not for every language out there.
2. 2x-4x performance overhead.
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.
Would this be useful for this too?
From https://news.ycombinator.com/item?id=46265855 :
> To better port C to Rust: 3C (Checked C), c2rust, Crown ownership analysis, RustMap, c2saferrust (LLM), Laertes
C -> Checked C -> Rust
Because Checked C will annotate the raw and other C pointers first.
[1] https://github.com/tsoding/crust [2] https://github.com/bext-lang/b [3] https://www.nokia.com/bell-labs/about/dennis-m-ritchie/kbman...
There are relatively few people who understand Rust’s compiler internals, LLVM backends, and obscure target support deeply. But there are many engineers who understand C compilers, ABIs, linkers, makefiles, cross-compilation, old operating systems, and weird platform-specific compiler behavior.
If Rust can be lowered into target-specific C, then some problems stop being exclusively “Rust compiler problems” and also become C toolchain problems. That means more people can inspect the generated C, build failures, linker errors, ABI mismatches, and compiler-specific behavior.
C is obviously not a magic portability layer. ABI details, integer widths, alignment, TLS, aliasing, and undefined behavior still matter. But as an ecosystem boundary, C gives many more engineers a way to participate in debugging and porting work.
I think that social/maintenance aspect may be more important than the language translation itself.
> Don't post generated text or AI-edited text. HN is for conversation between humans.
Probably because in East Asia, we tend to emphasize things with things like "xx" a lot.
However, their comments are consistently long, so it may be GenAI after all. Their last comment in particular...
https://news.ycombinator.com/item?id=48771339
this is the wrong direction
(jk i read the readme)
But we now all know rust is microsoft and c++ ISO. I don't think we can trust much more ISO than microsoft to produce less feature creep over the currently known planned obsolescence cycle of 5-10 years.