Most active commenters
  • aw1621107(3)

←back to thread

Eurydice: a Rust to C compiler

(jonathan.protzenko.fr)
185 points todsacerdoti | 19 comments | | HN request time: 0.629s | source | bottom
1. bloppe ◴[] No.46180140[source]
Rust compiles to LLVM IR. I'm pretty surprised that building this transpiler was considered a better use of time than writing an LLVM backend for whatever "weird embedded target" might need this.
replies(9): >>46180523 #>>46180571 #>>46180649 #>>46180773 #>>46180810 #>>46181523 #>>46184598 #>>46185313 #>>46189858 #
2. aw1621107 ◴[] No.46180523[source]
In fact there used to be a C backend for LLVM, but it was removed in LLVM 3.1 [0]. JuliaHub has resurrected it as a third-party backend [1], though I have no idea if there is any interest in upstreaming the work from either end.

[0]: https://releases.llvm.org/3.1/docs/ReleaseNotes.html

[1]: https://releases.llvm.org/3.1/docs/ReleaseNotes.html

replies(1): >>46181163 #
3. rcxdude ◴[] No.46180571[source]
This would cover many different weird embedded targets, and a lot of those targets can be an utter pain to target with a compiler.
4. Wuzado ◴[] No.46180649[source]
Sometimes you may need to deal with odd proprietary processors with limited and flawed knowledge about them.

For example, in the 37c3 talk "Breaking "DRM" in Polish trains" by the folks from Dragon Sector (I highly recommend watching it), they needed to reverse-engineer Tricore binaries, however they found the Ghidra implementation had bugs.

As for the PLCs, the IEC 61131-3 functional block diagrams transpile to C code which then compiles to Tricore binaries using an obscure GCC fork. Not saying that anyone would want to write Rust code for PLCs, but this is not uncommon in the world of embedded.

replies(1): >>46184185 #
5. SkiFire13 ◴[] No.46180773[source]
Rustc supports backends other than LLVM btw
replies(1): >>46181073 #
6. flohofwoe ◴[] No.46180810[source]
If you write a library in Rust and want to make that library available to other language ecosystems, not requiring a Rust compiler toolchain for using the library is a pretty big plus - instead create a C source distribution of the library, basically using C as the cross-platform intermediate format.
7. forgotpwd16 ◴[] No.46181073[source]
And someone has made (https://github.com/FractalFir/rustc_codegen_clr) a backend targeting C (alongside .NET/CIL).
8. fithisux ◴[] No.46181163[source]
The refs are duplicated.
replies(1): >>46182019 #
9. torginus ◴[] No.46181523[source]
There are many kinds of IRs in compilers - I'm not familiar with how Rust works, but for example GCC has an intermediate representation called GENERIC, which is a C-like language that preserves the scoping and loops and branches of the inputted C code.

Lower level representations also exist - since C has goto, you can pretty much turn any SSA IR to C, but the end result won't be readable.

10. aw1621107 ◴[] No.46182019{3}[source]
Ack, my bad. Can't edit the comment any more, unfortunately. Second ref is supposed to be to https://github.com/JuliaHubOSS/llvm-cbe
replies(1): >>46186009 #
11. mistrial9 ◴[] No.46184185[source]
.. there is some humor in the string

"Breaking "DRM" in Polish trains"

replies(1): >>46184645 #
12. NooneAtAll3 ◴[] No.46184598[source]
I have never seen any guides or blogs about writing LLVM backend

and considering how long compiling LLVM takes... it's reasonable to go for other options

13. iberator ◴[] No.46184645{3}[source]
Why?
14. Bratmon ◴[] No.46185313[source]
I think you may not be familiar with how embedded development works.

Most teams who write code for embedded devices (especially the weird devices at issue here) don't have the hardware knowledge, time, or contractual ability to write their own compiler backend. They're almost always stuck with the compiler the manufacturer decided to give them.

replies(2): >>46185418 #>>46185597 #
15. aw1621107 ◴[] No.46185418[source]
I think the approach would not be to alter the manufacturer's compiler directly, but to run your Rust code through a separate Rust-to-C compiler then feed that output into the compiler the manufacturer gave you.
16. bloppe ◴[] No.46185597[source]
You're right. But I'm also surprised any device manufacturer would think it's a better use of their time to ship a bespoke C compiler rather than an LLVM backend that would allow a lot more languages to be built against their ISA, making it more valuable.

But ya, I believe this project exists for a meaningful purpose, I'm just surprised.

replies(1): >>46189121 #
17. fithisux ◴[] No.46186009{4}[source]
Thanks
18. lelanthran ◴[] No.46189121{3}[source]
> But I'm also surprised any device manufacturer would think it's a better use of their time to ship a bespoke C compiler rather than an LLVM backend that would allow a lot more languages to be built against their ISA, making it more valuable.

They aren't usually building a compiler from scratch; they modify their existing compiler[1] for their new hardware, which is fractions of effort required compared to building a new compiler or modifying the LLVM backend.

Unless it's a completely new ISA, they'll spend an hour or less just adding in the new emit code for only the changes in the ISA.

----------------------------------

[1] Usually just the previous gcc that they used, which is why in 2022 I was working on brand new devices that came with a customised gcc 4.something

19. zem ◴[] No.46189858[source]
this effectively writes a backend for all the weird targets at once, since pretty much everything out there has c compiler support