feat: JIT (WIP)

This commit is contained in:
2025-05-11 14:57:05 +08:00
parent cfed44ebf4
commit d664d433dc
10 changed files with 38 additions and 22 deletions

View File

@@ -11,6 +11,7 @@ use crate::ty::public as p;
use stack::{STACK_SIZE, Stack};
pub use env::{CapturedEnv, Env};
pub use jit::JITContext;
mod env;
mod jit;
@@ -19,7 +20,7 @@ mod stack;
#[cfg(test)]
mod test;
pub fn run(prog: Program) -> Result<p::Value> {
pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
let vm = VM::new(
prog.thunks,
prog.funcs
@@ -28,8 +29,10 @@ pub fn run(prog: Program) -> Result<p::Value> {
env: OnceCell::new(),
param: f.param,
opcodes: f.opcodes,
compiled: None,
})
.collect(),
jit
);
let env = env();
let temp = vm.eval(prog.top_level, env)?;
@@ -37,18 +40,19 @@ pub fn run(prog: Program) -> Result<p::Value> {
Ok(temp)
}
pub struct VM<'vm> {
pub struct VM<'vm, 'jit> {
thunks: Box<[Thunk<'vm>]>,
funcs: Box<[Func<'vm>]>,
jit: JITContext<'jit>,
}
impl<'vm> VM<'vm> {
fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func<'vm>]>) -> Self {
impl<'vm, 'jit> VM<'vm, 'jit> {
fn new(thunks: Box<[OpCodes]>, funcs: Box<[Func<'vm>]>, jit: JITContext<'jit>) -> Self {
let thunks = thunks
.into_iter()
.map(|opcodes| Thunk::new(opcodes))
.collect();
VM { thunks, funcs }
VM { thunks, funcs, jit }
}
pub fn get_thunk(&self, idx: usize) -> &'vm Thunk<'vm> {