Rust JIT / dynamic code generation

I'm writing an interpreter for a small language where the only data types are i32, f32, vec of i32, and vec of f32.

The operations are basic math operations + if + while.

However, performance is really important.

Is there a way to do dynamic code generation in Rust ? I'm looking for something like this:

  1. crate provides an 'instruction set'
  2. My Rust code, at run time, generates a Vec<Instruction>
  3. crate converts this Vec<Instruction> to some code I can call (messiness here to pass arguments of i32, f32, vec of i32, vec of f32)
    4.My Rust code can then invoke this newly generated code.

An interesting approach may be to generate WASM files, and take advantage of an existing WASM interpreter.

2 Likes

Depending on your use case, perhaps you can integrate Lua into your Rust code instead, which can be run dymanically.

https://crates.io/crates/mlua

I switched to this after trying to implement a language for a DSL myself. Lua works, and all the hard work is done for you already.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.