4 ms·
Oh, and regarding the request for a Rust AST to C compiler, I hope that doesn't happen for a number of reasons. First, it undoes the work we're doing on MIR, wh
by pcwalton 11y ago
Oh, and regarding the request for a Rust AST to C compiler, I hope that doesn't happen for a number of reasons. First, it undoes the work we're doing on MIR, which is very important (you couldn't run MIR-level optimizations, the borrow check and codegen would be at high risk of divergence, we'd have to take a big backwards step to serializing ASTs for generics, etc.) Second, it'd be very hard to dodge C's undefined behavior: think of what we'd have to do to make signed integer overflow crash cleanly instead of leading to UB, just to name one particularly egregious problem...
- comex 11y agoI don't see how signed overflow is hard: whenever you see a signed arithmetic operation you just emit, e.g., `add_i32(a, b)` instead of `a + b`, and then include definitions of those functions for each signed integer type (all 4 of them) which are 1-4 lines long. Strict aliasing and pointer rules, on the other hand, definitely are hard if you want to produce truly standard C code: you have to completely bypass C's type system, using `char *` or `uintptr_t` for everything, and while this can be done, the resulting code is likely to look pretty ugly. However, a reasonable alternative is to depend on nonstandard annotations to disable TBAA: `__attribute__((may_alias))` for every popular compiler other than MSVC, which I believe doesn't do TBAA at all. (If MSVC ever adds it, the generated C code would need updating, but that's not the end of the world.) As for MIR - while I said AST, I suspect it would be fine to do it on MIR, at the cost of having to reconstruct some control flow. The biggest problem with doing it on LLVM IR is the simplified type info, while the full info is still there for MIR, right? Not an expert. Anyway, it's just an idea.