
Cranelift, a low-level retargetable code generator written in Rust - exrook
https://github.com/CraneStation/cranelift
======
smaddox
It's interesting that booleans are stored as 0 or -1 [1]. I wonder what drove
that decision. I just checked, and Rust's LLVM stores booleans as 0 or 1.

[1]
[https://cranelift.readthedocs.io/en/latest/ir.html#boolean-t...](https://cranelift.readthedocs.io/en/latest/ir.html#boolean-
types)

~~~
KMag
Well, for one, if true is -1 (using two's complement), then a bunch of
conditionals and bit-twiddling become more simple. For instance:

    
    
        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.

~~~
eridius
That would only be true if `a` is guaranteed to be a non-zero value though,
right? e.g. `x = some_bool ? 0 : 1` would not be able to be represented that
way.

~~~
repsilat
It works for me. Note that `|` is a bitwise-or (not that it really matters, it
just removes a branch)

    
    
        const True = -1
        const False = 0
        const ternary = (condition, left, right) => \
          (condition & left) | (~condition & right)
    
        ternary(True, 0, 1) // 0
        ternary(False, 0, 1) // 1

~~~
eridius
Oh right, because the right-hand side evaluates to 0 as well in this case.
Makes sense.

------
seeekr
The README says:

"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."

------
xvilka
Hopefully will replace LLVM one day.

~~~
pjmlp
It will be an herculean effort though, given the amount of companies that
contribute to LLVM.

~~~
sunfish
Fortunately, this isn't required for the project to be successful :-).

~~~
pjmlp
Depends on how successful is going to be measured.

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.

~~~
sunfish
Indeed, pragmatism is why we don't define success in those terms.

------
steveklabnik
Previously called “cretone”, by the way.

~~~
cpeterso
I heard that the project was renamed because no one could pronounced
"Cretonne". :)

~~~
stmw
Plus it has the danger of sounding too much like "cretin"

------
kodablah
Does it, or are there plans to, translate the IR to WASM (i.e. a WASM backend
instead of just frontend)? This seems to have a more expressive set of
instructions than WASM and could be a good target for compilers.

~~~
sunfish
It is something we're thinking about. You might have to limit yourself to a
subset of the full Cranelift IR for this, but I think we could make that
subset big enough to be interesting.

------
ndh2
Any example or tutorial?

~~~
dmytrish
I can't say if it's complete or work in progress, but here is a demo:
[https://github.com/sunfishcode/simplejit-
demo](https://github.com/sunfishcode/simplejit-demo)

~~~
smaddox
Nice! This should be amazing for writing toy compiled languages! This looks
way easier to get up and running than an LLVM backend.

------
jeandejean
I understand the enthusiasm for Rust, but I'm growing tired of these project
advertised as "written in Rust" like it proves a point in itself.

~~~
sunfish
Fwiw, "written in Rust" was added by whoever submitted the HN link; the
project's own web page and documentation don't describe it like that.

