8 ms·
Eurydice: a Rust to C compiler
- mustache_kimono 11mo agoCool. One should perhaps mention some prior art: rustc_codegen_clr is an experimental Rust compiler backend(plugin), which allows you to transpile Rust into .NET assemblies, or C source files.[0] [0]: https://github.com/FractalFir/rustc_codegen_clr https://github.com/FractalFir/rustc_codegen_clr
- oconnor663 11mo ago> We recommend using Nix to easily ensure you are running the right versions of the tools and libraries. Ooof I remember when everything used to be like this. Cargo has really spoiled me.
- ValtteriL 11mo agoCan Cargo install deployment tools like Ansible? I find myself using nix for those kind of tools despite how good $lang package manager is.
- kamov 11mo agoYou can use both Nix and Cargo at the same time, and they accomplish different things. Nix's purpose is more like rustup, except it works for each program on your computer
- skavi 11mo agoCargo does not attempt to solve the same problems as Nix (if you depend on any software not written in Rust). A checked in Nix shell is genuinely great documentation on the expected system environment.
- apitman 11mo agoI use Rust and C at work. I quite enjoy Rust, but I currently have no reason to believe C won't outlive it, by a lot.
- po1nt 11mo agoWell, Fortran is still used somewhere too
- rahen 11mo agoYou mean everywhere. It's just hidden behind abstraction layers or Fortran libraries like BLAS/LAPACK, which are used by NumPy, R, Julia, MATLAB, Excel, TensorFlow, PyTorch (for some backends), and basically anything that involves linear algebra.
- hu3 11mo agoAnd I think Zig will gradually eat a large piece of the C pie that would otherwise be eaten by Rust.
- pjmlp 11mo agoI doubt it, unless it sorts out its use after free issues, embraces binary distribution used in commercial world, and gets adoption by an OS vendor.
- Zambyte 11mo agoYou can create and link to shared objects and create statically or dynamically linked executables. What more could you want on "embracing binary distribution"?
- opem 11mo agoJai is also on the que
- Y_Y 11mo ago
- BobbyTables2 11mo agoNot saying it isn’t neat, but WHY? Seems like the kind of thing that happens before a language is natively supported by a compiler.
- viraptor 11mo agoIf only the article started with an explanation...
- thrance 11mo agoThere's literally a section named "Why?" in the article.
- procaryote 11mo agoThey want to use rust but can't, because they need C compatibility, e.g. because they're providing a library or wants to support platforms that rust don't. It's at least a less bad idea than I expected originally – I've mainly seen people try to use transpilers to sneakily start using the language they prefer as part of a larger code base, with all the obvious problems. It's still not great as you now have an additional translation step to deal with. You will at some point need to look for bugs in the generated C layer, which will really suck. Users of the C library can't read your source to understand what it does internally without an extra step to figure out what rust code the thing they use corresponds to, etc. If you want to provide a C library, write C. If rust isn't an option because it doesn't work for your customers who use C, don't use rust.
- zozbot234 11mo agoYou can write a C-compatible binary library in Rust (see the cdylib target) and cbindgen can then generate proper C header files for any C-ABI interfaces exposed in Rust code. A full Rust-to-C compiler should only be needed for targets that have some C compiler available but are just not supported by the current Rust toolchain.
- flohofwoe 11mo agoIf you have an existing build system for your C project, you sure as hell don't want to bring another compiler toolchain (like Rust) into the mix.
- nutjob2 11mo agoAnother Rust compiler with a C backend: https://github.com/thepowersgang/mrustc https://github.com/thepowersgang/mrustc
- ValtteriL 11mo agoGithub: https://github.com/AeneasVerif/eurydice https://github.com/AeneasVerif/eurydice
- opem 11mo agoCool, loved this! These tools would be needed in the near future to manage the tech debt safe rust is compounding.
- bloppe 11mo agoRust 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.
- aw1621107 11mo agoIn 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 https://releases.llvm.org/3.1/docs/ReleaseNotes.html [1]: https://releases.llvm.org/3.1/docs/ReleaseNotes.html https://releases.llvm.org/3.1/docs/ReleaseNotes.html
- rcxdude 11mo agoThis would cover many different weird embedded targets, and a lot of those targets can be an utter pain to target with a compiler.
- Wuzado 11mo agoSometimes 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.
- hexo 11mo ago[flagged]
- pankajdoharey 11mo agoIn a world where Crabs are trying to rewrite everything in their favourite Crab Speak its nice to see the Reverse. I wonder if it coulkd be used to translate Rust compiler itself to C :-D
- Y_Y 11mo agohttps://github.com/Rust-GCC/gccrs/tree/master/gcc/rust https://github.com/Rust-GCC/gccrs/tree/master/gcc/rust There's a Rust compiler in C++ in case that's any good to you
- Aissen 11mo ago> for instance, Rust panics on integer overflow By default, this is only in debug mode. I recently forgot to add it to release mode on a project, and was surprised when I broke the CI (tests run in debug, I only tested in release mode).
- IshKebab 11mo agoThe perfect headline to bring the anti-Rust luddites out of the woodwork...
- exDM69 11mo agoThe project itself is cool and useful but the motivating example of crypto (primitives?) isn't great. Cryptography is already difficult to write in high level languages without introducing side channels via timing, branch predictor, caches etc. Cryptography while going through two high level compilers, especially when the code was not designed and written to do so is an exercise fraught with peril. Tbf, this is just nitpicking about the article, not the project itself
- the8472 11mo agoIt's not possible to correctly implement any cryptographic algorithms in any high-level language with an optimizing backend where timing is not considered an observable/perserved property. Currently this includes anything backed by LLVM or GCC, though there's a proposal to introduce such guarantees through a new builtin in LLVM https://github.com/llvm/llvm-project/pull/166702 https://github.com/llvm/llvm-project/pull/166702 though those could still be broken by post-build optimizers, like wasm.
- d-lisp 11mo agoWhen you think about it, Perseus did use Medusa's head to transform things into stone. In fact he turned king Polydectes and all of his followers into stone when he went back to Sephiros. Perseus defeated Medusa by not looking at her in the eyes. Rust in a sense, allow you to solve problems without having to look directly at memory unsafe behavior. I would have called this project "Medusa".
- protz 11mo agoauthor here; thanks for the suggestion, that'll be a cool name for my next project :-)
- jll29 11mo agoThe original author of the post also - has designed the Mezzo programming language in his Ph.D. thesis and - has worked on generating user-friendly C automatically. Most tools that generate C output pretty unreadable/unmaintainable code that you won't like to maintain, so this is a much under-researched topic.
- Surac 11mo agoI was always told rust uses llvm tokens not produceable by c code to do its magic. Was I informed wrong?
- mmastrac 11mo agoThere are multiple stages of IR in the compiler, basically
- SAI_Peregrinus 11mo agoYou probably misunderstood. C can represent any program's semantics, since it's Turing-complete (modulo finite memory). C can't encode the lifetimes Rust uses, but those get erased during compilation to MIR. This takes MIR from rustc (where borrow checking has been completed and lifetime annotations erased) and outputs C with the same semantics. LLVM doesn't use tokens not produceable by C, but rustc does.
- Grikbdl 11mo agoI think it's a reference to certain optimizations possible due to aliasing rules in Rust that are not possible (or maybe only "not straight forward", I'm not sure) in C. So a transpiled program while keeping its semantics might not still compile to equally optimized assembly.
- SAI_Peregrinus 11mo agoIIRC C can do the same things with correct usage of `restrict`, but that's extremely difficult by hand. So difficult that LLVM's `restrict` support was very buggy when Rust first started using the capabilities. Those bugs got fixed, but it's still impractical to use in handwritten C.
- 1718627440 11mo agoC still has lifetime rules. It just has no syntax for them. What people are bitten with is that they violate the lifetime rules, but their compiler doesn't tell them.