feat: gc (does compile, but WIP)

This commit is contained in:
2025-05-27 21:08:59 +08:00
parent 319c12c1f4
commit c3ace28af1
20 changed files with 696 additions and 575 deletions

View File

@@ -1,11 +1,9 @@
use std::cell::Cell;
use derive_more::Constructor;
use gc_arena::lock::GcRefLock;
use gc_arena::lock::{GcRefLock, RefLock};
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable};
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 +11,7 @@ use crate::error::Result;
use crate::ir;
use crate::jit::JITFunc;
use crate::ty::internal::{Thunk, Value};
use crate::vm::VM;
use crate::vm::{GcRoot, VM, eval};
#[derive(Debug, Clone, Collect)]
#[collect(no_drop)]
@@ -46,7 +44,7 @@ impl From<ir::Param> for Param {
}
}
#[derive(Clone, Constructor)]
#[derive(Clone)]
pub struct Func<'gc> {
pub func: &'gc BFunc,
pub env: Gc<'gc, VmEnv<'gc>>,
@@ -54,17 +52,33 @@ pub struct Func<'gc> {
pub count: Cell<usize>,
}
unsafe impl<'gc> Collect for Func<'gc> {
fn trace(&self, cc: &gc_arena::Collection) {
unsafe impl<'gc> Collect<'gc> for Func<'gc> {
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
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::*;
impl<'gc> Func<'gc> {
pub fn new(func: &'gc BFunc, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) -> Self {
Self {
func,
env,
compiled: Gc::new(mc, RefLock::new(None)),
count: Cell::new(0),
}
}
pub fn call(
&self,
arg: Value<'gc>,
vm: &'gc VM<'gc>,
mc: &Mutation<'gc>,
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
) -> Result<Value<'gc>> {
todo!()
/* use Param::*;
let mut env = self.env;
env = match self.func.param.clone() {
@@ -75,8 +89,7 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
alias,
} => {
let arg = arg.unwrap_attr_set();
let mut new =
HashMap::with_capacity(formals.len() + alias.iter().len());
let mut new = HashMap::with_capacity(formals.len() + alias.iter().len());
if !ellipsis
&& arg
.as_inner()
@@ -91,7 +104,8 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
let arg = arg
.select(formal)
.or_else(|| {
default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
default
.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
})
.unwrap();
new.insert(formal, arg);
@@ -106,15 +120,18 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
let count = self.count.get();
self.count.replace(count + 1);
if count >= 1 {
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) };
let compiled = &mut *self.compiled.borrow_mut(mc);
let compiled = compiled.get_or_insert_with(|| vm.compile_func(self.func));
let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
return Ok(ret.into());
}
vm.eval(self.func.opcodes.iter().copied(), env)
eval(self.func.opcodes.iter().copied(), arena, |val, _| {
Ok(val)
}) */
}
}
impl PartialEq for Func<'_, '_, '_> {
impl PartialEq for Func<'_> {
fn eq(&self, _: &Self) -> bool {
false
}