rustc_codegen_cranelift icon indicating copy to clipboard operation
rustc_codegen_cranelift copied to clipboard

Embedding rustc + cg_clif as a scripting environment

Open lachlansneff opened this issue 4 years ago • 5 comments

I'm interested in using rustc + cranelift as a library to embed rust as a jitted scripting language within another application. Do you expect this to be possible reasonably soon?

lachlansneff avatar Feb 18 '21 17:02 lachlansneff

If you don't want to preserve any state in between runs this is already almost possible. https://github.com/bjorn3/rustc_codegen_cranelift/blob/1337f96c48022396bbdd5af93ba27a1d22fa0603/src/bin/cg_clif.rs is an example how to use cg_clif using a custom rustc driver. On line 61 you can pass a custom config instead of None to make it always JIT the code. You may want to use the sysroot logic at https://github.com/bjorn3/rustc_codegen_cranelift/blob/1337f96c48022396bbdd5af93ba27a1d22fa0603/src/bin/cg_clif_build_sysroot.rs#L24-L34 though. The only thing that would work against your goal is currently https://github.com/bjorn3/rustc_codegen_cranelift/blob/1337f96c48022396bbdd5af93ba27a1d22fa0603/src/driver/jit.rs#L152, which exits the process once the jitted code is done.

bjorn3 avatar Feb 18 '21 17:02 bjorn3

Ah, dope, I'll fork the repo to patch the exit.

It does look a little complicated to actually get the compiler to process source code though. And the sysroot stuff does make me think that there needs to be libstd or something similar installed on the computer for the compiler to work. Is the only way around that to create a fake file system and replace the FileLoader?

lachlansneff avatar Feb 18 '21 17:02 lachlansneff

For the source you did write a FileLoader implementation if you don't want to write to the disk. For the sysroot you can use the sysroot shipped with rustc as found by https://github.com/bjorn3/rustc_codegen_cranelift/blob/1337f96c48022396bbdd5af93ba27a1d22fa0603/src/bin/cg_clif_build_sysroot.rs#L24-L34. Or you can use the sysroot built when building cg_clif using ./build.sh.

bjorn3 avatar Feb 18 '21 17:02 bjorn3

Would a Rust REPL be possible? That would be incredibly cool.

Keithcat1 avatar Aug 03 '21 18:08 Keithcat1

https://github.com/google/evcxr is a rust repl. It doesn't use cg_clif, but it should in principle be possible to use cg_clif to compile the code.

bjorn3 avatar Aug 03 '21 18:08 bjorn3