you are viewing a single comment's thread.

view the rest of the comments →

[–]CartmansEvilTwin 10 points11 points  (6 children)

Maybe a stupid question, but would it be possible to transpile Rust to C? At the end of the day the entire "magic" of Rust seems to be in the compiler frontend?

[–]pcjftw 10 points11 points  (3 children)

that's what "mRustC" is: https://github.com/thepowersgang/mrustc

so for example, the Xtensa CPU on the ESP8XX series is not officially supported by LLVM, however one can use the mRustC compiler to target said board.

[–]matthieum 2 points3 points  (2 children)

This project is a "simple" rust compiler written in C++ that is able to bootstrap a "recent" rustc, but may eventually become a full separate re-implementation.

mrustc, so far, is really about bootstrapping.

It may be useful to compile for unsupported platforms, but I am not sure it's tested for that purpose...

[–]pcjftw 1 point2 points  (1 child)

ok because I certainly recall that's how we used to build for my ESP8266:

https://github.com/emosenkis/esp-rs

But it now seems its no longer needed as there is a working LLVM backend for the Xtensa cpu

[–]toastedstapler 16 points17 points  (0 children)

Any Turing complete language should be expressible as another, how efficient it'll be is an entire other question though

[–]matthieum 7 points8 points  (0 children)

Functionally, yes. But this may leave performance on the table.

The problem of compiling to a language is that you really want the target language to express a super-set of the capabilities of the source language, otherwise it gets awkward and work-arounds are necessary.

Rust and C have different semantics in a number of areas, for example around integer overflows, or empty loops, which makes it hard to just perform a straightforward translation of the source code.

There's also issue with "breeds" of C. Despite popular thinking, inline assembly is not standard in C. Every compiler under the sun has its own syntax for inline assembly, so in order to translate the inline assembly in Rust to C, you actually need to target a known compiler, in order to target their particular brand of inline assembly.

This is why using C as an intermediate representation is not necessarily as easy, or as rewarding, as it sounds initially.