feat: gc-arena (WIP, does not compile)

This commit is contained in:
2025-05-25 17:18:54 +08:00
parent b41fd38bcc
commit cc06369c5e
17 changed files with 882 additions and 585 deletions

View File

@@ -1,9 +1,11 @@
use std::cell::{Cell, OnceCell};
use std::cell::Cell;
use derive_more::Constructor;
use gc_arena::lock::GcRefLock;
use hashbrown::HashMap;
use inkwell::execution_engine::JitFunction;
use itertools::Itertools;
use gc_arena::{Collect, Gc, Mutation};
use crate::bytecode::Func as BFunc;
use crate::env::VmEnv;
@@ -13,7 +15,8 @@ use crate::jit::JITFunc;
use crate::ty::internal::{Thunk, Value};
use crate::vm::VM;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Collect)]
#[collect(no_drop)]
pub enum Param {
Ident(usize),
Formals {
@@ -43,21 +46,29 @@ impl From<ir::Param> for Param {
}
}
#[derive(Debug, Clone, Constructor)]
pub struct Func<'jit: 'vm, 'vm> {
pub func: &'vm BFunc,
pub env: &'vm VmEnv<'jit, 'vm>,
pub compiled: OnceCell<JitFunction<'jit, JITFunc<'jit, 'vm>>>,
#[derive(Clone, Constructor)]
pub struct Func<'gc> {
pub func: &'gc BFunc,
pub env: Gc<'gc, VmEnv<'gc>>,
pub compiled: GcRefLock<'gc, Option<JITFunc<'gc>>>,
pub count: Cell<usize>,
}
impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
pub fn call(&self, vm: &'vm VM<'jit>, arg: Value<'jit, 'vm>) -> Result<Value<'jit, 'vm>> {
unsafe impl<'gc> Collect for Func<'gc> {
fn trace(&self, cc: &gc_arena::Collection) {
self.env.trace(cc);
self.compiled.trace(cc);
self.count.trace(cc);
}
}
impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
use Param::*;
let mut env = self.env;
env = match self.func.param.clone() {
Ident(ident) => env.enter_arg(ident, arg, &vm.bump),
Ident(ident) => env.enter_arg(ident, arg, mc),
Formals {
formals,
ellipsis,
@@ -65,7 +76,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
} => {
let arg = arg.unwrap_attr_set();
let mut new =
HashMap::with_capacity_in(formals.len() + alias.iter().len(), &vm.bump);
HashMap::with_capacity(formals.len() + alias.iter().len());
if !ellipsis
&& arg
.as_inner()
@@ -80,7 +91,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
let arg = arg
.select(formal)
.or_else(|| {
default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)).into()))
default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
})
.unwrap();
new.insert(formal, arg);
@@ -88,23 +99,22 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
if let Some(alias) = alias {
new.insert(alias, Value::AttrSet(arg));
}
env.enter_let(vm.bump.alloc(new), &vm.bump)
env.enter_let(Gc::new(mc, new.into()), mc)
}
};
let count = self.count.get();
self.count.replace(count + 1);
if count >= 1 {
let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func));
let env = env as *const _;
let ret = unsafe { compiled.call(vm as *const VM, env) };
let compiled = self.compiled.borrow_mut(mc).get_or_insert_with(|| vm.compile_func(self.func));
let ret = unsafe { compiled.call(vm as *const VM, env.as_ref() as *const VmEnv) };
return Ok(ret.into());
}
vm.eval(self.func.opcodes.iter().copied(), vm.bump.alloc(env))
vm.eval(self.func.opcodes.iter().copied(), env)
}
}
impl PartialEq for Func<'_, '_> {
impl PartialEq for Func<'_, '_, '_> {
fn eq(&self, _: &Self) -> bool {
false
}