this post was submitted on 23 Jun 2024
26 points (100.0% liked)
Rust
5960 readers
2 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Sure you could JIT Rust, the question is if you can write a JIT compiler in rust since it needs to do some quite scary stuff to swap in compiled routines when evaluating code. I’m not even sure if unsafe is enough for that, you may need goto or arbitrary function pointers (which is kind of the same thing)
Considering most JIT compilers for JavaScript are written in C++, I can't conceive of a reason you couldn't implement one in Rust.
Is part of your requirement that
unsafe
doesn't get used anywhere in the dependency tree? If so you'd have to take away most of the Ruststd
library since many implementations in there have small strategic uses ofunsafe
under the hood.In my entire software engineering career, which spans embedded systems to CAD applications, I've never encountered a case where
GOTO
is actually needed (but maybe some places where it can be used as a dirty shortcut to save you some lines of code).As for arbitrary function pointers, if those function pointers are written in Rust then they'll come with all the safety assurances afforded to Rust code. I suppose if you're worried about the danger of running ussr-code with
unsafe
in it, you could probably have your JIT refuse to compile theunsafe
keyword specifically.The only part of a JIT compiler I don’t understand how it works is the part that swaps in compiled routines during interpretation. That’s the point I’m unsure of how to write in Rust because it seems like it would require very custom control flow. It might be that you can handle this by storing your compiled instructions somewhere using the C calling convention and then having Rust call your compiled function like a C function.
In essence, what you have is a Rust program that has to produce machine code (easy!), store it somewhere in RAM (also easy, I think), and then somehow call it (how???). The final part seems like the difficult one since passing execution into arbitrary memory they just wrote is just the sort of thing programs aren’t normally supposed to do.
I originally meant solution described here:
https://blog.cloudflare.com/using-go-as-a-scripting-language-in-linux/