rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.57k stars 97 forks source link

Embedding rustc + cg_clif as a scripting environment #1139

Open lachlansneff opened 3 years ago

lachlansneff commented 3 years ago

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?

bjorn3 commented 3 years ago

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.

lachlansneff commented 3 years ago

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?

bjorn3 commented 3 years ago

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.

Keithcat1 commented 3 years ago

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

bjorn3 commented 3 years ago

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.