Cranelift, a low-level retargetable code generator written in Rust
github.com
github.com
[1] https://cranelift.readthedocs.io/en/latest/ir.html#boolean-t...
x = some_bool ? a : b;
can be compiled as a branchless: x = (some_bool & a) | (~some_bool & b);
This is why Forth also represents true as -1.C arguably picked the wrong semantics here. Hardware picks up the slack, however, with the setcc instructions on x86, csinc on AArch64, etc.
const True = -1
const False = 0
const ternary = (condition, left, right) => \
(condition & left) | (~condition & right)
ternary(True, 0, 1) // 0
ternary(False, 0, 1) // 1I assume someone once upon a time thought it was a nice thing for ~x to be equal to !x.
"Cranelift is designed to be a code generator for WebAssembly, but it is general enough to be useful elsewhere too. The initial planned uses that affected its design are:
WebAssembly compiler for the SpiderMonkey engine in Firefox. Backend for the IonMonkey JavaScript JIT compiler in Firefox. Debug build backend for the Rust compiler."
If it is to generate code with the same optimization quality and hardware support as LLVM, not really.
There are still cases where LLVM is playing catch up with GCC.
I also would like to see a pure Rust compiler toolchain, but sometimes pragmatism wins.