Any simpler version of cranelift?

i am making a compiler, and i already have complete IR, i just need to generate machine code using it. i originally thought about using cranelift, but seeing how it has it's IR and requires creating everything from scratch (i don't mind having to write data types, but the fact that there's not even strings or bytes builtin really seems to me like too much). so i was wondering if there's any crates that are similar to cranelift but are either simpler or don't require IR

You can do codegen with dynasmrt, which is just a fancy assembler with focus on JIT compilation. If you are looking for an AOT codegen framework, this might not be the right choice. dynasm isn't going to output position-independent code or ELF object files on its own.

You'll also notice that it's very opinionated on platform ISAs, and that's a consequence of not requiring an IR.

Another option might be customasm, but it's targeted as a DSL for writing assemblers. I've used it for defining a few (real-world) ISAs and at least one fantasy ISA. The trouble with it is that defining the ISA with their DSL can be challenging, and the result is still quite low-level. It can produce a macro-assembler and might be able to transform your IR into machine code. But it only parses string input and it's going to have the same problem WRT position-independent code and ELF objects.

what about with IR? if there are good enough options, then i'd rather use them before the project gets too big

Apart from cranelift and LLVM, I found qbe. I don't know anything about it.

Also: the GCC backend with its GIMPLE IR and libgccjit, Firm, The Amsterdam Compiler Kit, and others. Not sure if any of these have Rust bindings...

What are you expecting as a builtin here? Strings are hard. It's pretty normal for IRs not to have primitive strings, but to have primitive pointers that you can use to walk strings.


yeah, but i can't allow myself to just take 3 years and make a perfect compiler. just like you said, strings are difficult, so i was expecting something things utf-8 strings to be builtin. that's why i said simpler

One option is to compile to another high level language and then piggyback off it. I have done that in the past for toy projects, in that case compiling to C. But you could compile to Rust or something else that is suitable.

1 Like

I sympathise. When I decided I had to write a compiler, a simple one just to show to myself I could get from source text to executable code, I ended up emitting x86 assembly. And used an assembler to get to the final executable code. Later I added output in assembler for the Parallax Propeller micro controller. You just have to know the instruction set of your target and generate the instructions, in assembly language, to do what you want. CPU data books are a must.

1 Like

You could limit compiler support to string literals and leave the rest to libraries. C and C++ take this approach.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.