feat: less gc (WIP)

This commit is contained in:
2025-06-02 14:18:59 +08:00
parent d3442e87e7
commit 51f8df9cca
11 changed files with 125 additions and 179 deletions

View File

@@ -361,7 +361,7 @@ extern "C" fn helper_call<'gc>(
ValueTag::Function => {
let func = Value::from(func).unwrap_func();
let env = unsafe { &mut *env };
env.alloc_gc(func.call_compile(arg, vm, mc).unwrap(), mc).into()
func.call_compile(arg, vm, mc).unwrap().clone().into()
}
_ => todo!(),
}
@@ -369,19 +369,19 @@ extern "C" fn helper_call<'gc>(
extern "C" fn helper_arg(idx: usize, env: *const VmEnv) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val: JITValue = env.lookup_arg(idx).into();
let val: JITValue = env.lookup_arg(idx).clone().into();
val
}
extern "C" fn helper_lookup_let(level: usize, idx: usize, env: *const VmEnv) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val: JITValue = env.lookup_let(level, idx).into();
let val: JITValue = env.lookup_let(level, idx).clone().into();
val
}
extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val: JITValue = env.lookup_with(&sym).unwrap().into();
let val: JITValue = env.lookup_with(&sym).unwrap().clone().into();
val
}
@@ -399,7 +399,7 @@ extern "C" fn helper_force<'gc>(
let mc = unsafe { mc.as_ref() }.unwrap();
let thunk = Value::from(thunk).unwrap_thunk();
if let Some(val) = thunk.get_value() {
return val.into();
return val.clone().into();
}
let (opcodes, env) = thunk.suspend(mc).unwrap();
let func = unsafe { jit.as_ref() }
@@ -414,9 +414,9 @@ extern "C" fn helper_force<'gc>(
extern "C" fn helper_new_thunk<'gc>(opcodes: *const OpCodes, env: *mut VmEnv<'gc>, mc: *const Mutation<'gc>) -> JITValue {
let mc = unsafe { &*mc };
let env = unsafe { &mut *env };
env.alloc_gc(Value::Thunk(Thunk::new(
Value::Thunk(Thunk::new(
unsafe { opcodes.as_ref() }.unwrap(),
mc
)), mc)
))
.into()
}

View File

@@ -1,4 +1,5 @@
use std::marker::PhantomData;
use std::rc::Rc;
use std::ops::Deref;
use gc_arena::{Collect, Gc, Mutation};
@@ -63,7 +64,7 @@ impl<'gc> From<JITValue> for Value<'gc> {
match value.tag {
Int => Value::Int(unsafe { value.data.int }),
Null => Value::Null,
Function => Value::Func(unsafe { Gc::from_ptr(value.data.ptr as *const _) }),
Function => Value::Func(unsafe { Rc::from_raw(value.data.ptr as *const _) }),
Thunk => Value::Thunk(self::Thunk {
thunk: unsafe { Gc::from_ptr(value.data.ptr as *const _) },
}),
@@ -72,19 +73,21 @@ impl<'gc> From<JITValue> for Value<'gc> {
}
}
impl From<&Value<'_>> for JITValue {
fn from(value: &Value) -> Self {
impl From<Value<'_>> for JITValue {
fn from(value: Value) -> Self {
match value {
&Value::Int(int) => JITValue {
Value::Int(int) => JITValue {
tag: ValueTag::Int,
data: JITValueData { int },
},
&Value::Func(func) => JITValue {
tag: ValueTag::Function,
data: JITValueData {
ptr: Gc::as_ptr(func) as *const _,
},
},
Value::Func(func) => {
JITValue {
tag: ValueTag::Function,
data: JITValueData {
ptr: Rc::into_raw(func) as *const _,
},
}
}
Value::Thunk(thunk) => JITValue {
tag: ValueTag::Thunk,
data: JITValueData {