Lineiform, a meta-JIT library for Rust interpreters
redvice.org
redvice.org
The last paragraph is unfortunate though:
> And if it works, and if it’s fast, and if it’s useful? Who knows. I’d like if Apple or Facebook paid me a lot of money for this library in order to make things fast (think being able to JIT compile build scripts or DSLs), so I’m not sure if I’ll opensource or source-available it or not; if you want access to the repo to play around, or even just to ask how things are going or talk about various compiler things, feel free to reach out.
Isn't a magic step missing here? In order to "lift, optimize, and then lower x86 instructions" you need x86 instructions to start with. How does the leap from having a Rust closure object to a snippet of baseline machine code work?
It's kinda complex, because Rust closure internals aren't documented all that well, but it works fine and isn't UB (as far as I know). The bigger issue is actually that Rust closures use the Rust ABI, which is not stable, and so Lineiform shouldn't really assume which arguments are in which registers since it could (technically) change at any point: we end up having the disassembly entry point be a C ABI trampoline that calls the rust-call ABI so that rustc emits the cross-ABI register shuffling for us as a prelude, and emit a C ABI function to call as an output.
The relevant code is https://github.com/chc4/lineiform/blob/4a104c994561282318179.... It is very messy, sorry.
This is how qemu and DynamoRIO and things work too, where they have an IR which allows them to more abstract transform the code than having to keep it normalize to x86 or some other assembly the entire time.
As part of switching to my own IR I'm going to have to write my own optimization passes for it as well instead of being able to piggyback off Cranelift.
The iflags design in general is kinda awkward too, and was being rethought a few months ago when I was first getting this working; I think they're planning on redesigning the add carryout interface and things to be slightly more streamlined. I suspect that any redesigned interface will have similar problems with mismatch between what I want from Cranelift and what 90% of other uses of Cranelfit want, though, and so I decided to just make my own IR instead.