feat(jit): fib!

This commit is contained in:
2025-05-19 19:29:25 +08:00
parent 6d26716412
commit 9e172bf013
10 changed files with 363 additions and 166 deletions

View File

@@ -4,24 +4,24 @@ use derive_more::Constructor;
use itertools::Itertools;
use crate::error::Result;
use crate::vm::{Env, VM};
use crate::vm::{LetEnv, VM};
use super::super::public as p;
use super::Value;
#[repr(C)]
#[derive(Debug, Constructor, Clone, PartialEq)]
#[derive(Debug, Default, Constructor, Clone, PartialEq)]
pub struct AttrSet<'jit: 'vm, 'vm> {
data: HashMap<usize, Value<'jit, 'vm>>,
}
impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> {
pub fn empty() -> Self {
AttrSet {
data: HashMap::new(),
}
impl<'jit, 'vm> From<HashMap<usize, Value<'jit, 'vm>>> for AttrSet<'jit, 'vm> {
fn from(data: HashMap<usize, Value<'jit, 'vm>>) -> Self {
Self { data }
}
}
impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> {
pub fn with_capacity(cap: usize) -> Self {
AttrSet {
data: HashMap::with_capacity(cap),
@@ -47,7 +47,7 @@ impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> {
self.data.get(&sym).is_some()
}
pub fn capture(&mut self, env: &Env<'jit, 'vm>) {
pub fn capture(&mut self, env: &LetEnv<'jit, 'vm>) {
self.data.iter().for_each(|(_, v)| match v.clone() {
Value::Thunk(ref thunk) => {
thunk.capture(env.clone());

View File

@@ -9,7 +9,7 @@ use crate::error::Result;
use crate::ir;
use crate::jit::JITFunc;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{Env, VM};
use crate::vm::{LetEnv, VM};
#[derive(Debug, Clone)]
pub enum Param {
@@ -44,7 +44,7 @@ impl From<ir::Param> for Param {
#[derive(Debug, Clone, Constructor)]
pub struct Func<'jit: 'vm, 'vm> {
pub func: &'vm BFunc,
pub env: Env<'jit, 'vm>,
pub env: LetEnv<'jit, 'vm>,
pub compiled: OnceCell<JitFunction<'jit, JITFunc<'jit, 'vm>>>,
pub count: Cell<usize>,
}
@@ -54,7 +54,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
use Param::*;
let env = match self.func.param.clone() {
Ident(ident) => self.env.clone().enter([(ident.into(), arg)].into_iter()),
Ident(ident) => self.env.clone().enter_let([(ident.into(), arg)].into_iter()),
Formals {
formals,
ellipsis,
@@ -85,7 +85,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
if let Some(alias) = alias {
new.push((alias.clone().into(), Value::AttrSet(arg)));
}
self.env.clone().enter(new.into_iter())
self.env.clone().enter_let(new.into_iter())
}
};
@@ -93,7 +93,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
self.count.replace(count + 1);
if count >= 1 {
let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func));
let ret = unsafe { compiled.call(vm as *const VM, &env as *const Env) };
let ret = unsafe { compiled.call(vm as *const VM, &env as *const LetEnv) };
return Ok(ret.into());
}
vm.eval(self.func.opcodes.iter().copied(), env)

View File

@@ -11,7 +11,7 @@ use super::public as p;
use crate::bytecode::OpCodes;
use crate::error::*;
use crate::vm::{Env, VM};
use crate::vm::{LetEnv, VM};
mod attrset;
mod func;
@@ -157,7 +157,11 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
pub fn typename(&self) -> &'static str {
use Value::*;
match self {
Const(_) => unreachable!(),
Const(self::Const::Int(_)) => "int",
Const(self::Const::Float(_)) => "float",
Const(self::Const::Bool(_)) => "bool",
Const(self::Const::String(_)) => "string",
Const(self::Const::Null) => "null",
Thunk(_) => "thunk",
ThunkRef(_) => "thunk",
AttrSet(_) => "set",
@@ -481,7 +485,7 @@ pub struct Thunk<'jit, 'vm> {
#[derive(Debug, IsVariant, Unwrap, Clone)]
pub enum _Thunk<'jit, 'vm> {
Code(&'vm OpCodes, OnceCell<Env<'jit, 'vm>>),
Code(&'vm OpCodes, OnceCell<LetEnv<'jit, 'vm>>),
SuspendedFrom(*const Thunk<'jit, 'vm>),
Value(Value<'jit, 'vm>),
}
@@ -493,7 +497,7 @@ impl<'jit, 'vm> Thunk<'jit, 'vm> {
}
}
pub fn capture(&self, env: Env<'jit, 'vm>) {
pub fn capture(&self, env: LetEnv<'jit, 'vm>) {
if let _Thunk::Code(_, envcell) = &*self.thunk.borrow() {
envcell.get_or_init(|| env);
}