How is this issue big? One can implement wrapping signed integer arithmetic in plain C, so we would just need a support C library that provides wrapping_add and thatâs what we would lower the Rust code to.
@comex
And it doesnât address other cfg keys, such as target_os .
A C support library can do this as well. Rust doesnât really do anything magical here.
Also, we could initially require transpiled C code to be compiled with gcc... -DRUST_TARGET_OS=darwin and call it a day.
But if you have portable Rust code that uses, say, libc , you canât necessarily convert it to portable C code that uses libc. To do so, youâd need a sophisticated mechanism to allow for âconstant expressionsâ that arenât actually known at compile time, such as sizeof(some_libc_struct)
I donât understand this point. libc could be converted to C code (its just extern functions), and if the values of the cfg() macros are know it should generate a C wrapper that is 100% ABI-identical with that of the platform; everything else is a bug in libc which is why this is actually tested by the ctest crate on every libc commit.
@josh
Rather than fixing this by transpiling to C, why not handle it by porting LLVM to those platforms?
Because porting LLVM to all platforms that every C compiler was ever able to target is a huge amount of work.
@matthieum
Translating Rust or MIR to C is going to probably run into very similar problems than translating LLVM-IR to C did.
IIRC the main problem is that MIR, LLVM-IR, etc. are all target dependent, which means that if the Rust toolchain generating the MIR or LLVM-IR does not support a particular target (or generates them for a slightly different target), the generated C code wonât probably compile with the C compiler that the end user wants to use.
I think this means that we cannot generate C from MIR because macros are expanded well before that. We would have to generate C directly from Rust, expanding Rust macros to C macros and maybe even expanding every âportableâ operation to calls into a call into a C support library.
We donât even have to provide this C support library, we can just transpile to C and expect the user to provide it for the platform they want to use. Such support libraries will probably be huge.