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

@@ -6,6 +6,7 @@ use inkwell::module::Module;
use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
use inkwell::values::{BasicValueEnum, FunctionValue};
use crate::bytecode::OpCodes;
use crate::env::VmEnv;
use crate::jit::JITValueData;
use crate::ty::internal::{Thunk, Value};
@@ -22,6 +23,8 @@ pub struct Helpers<'ctx> {
pub value_type: StructType<'ctx>,
pub func_type: FunctionType<'ctx>,
pub new_thunk: FunctionValue<'ctx>,
pub debug: FunctionValue<'ctx>,
pub capture_env: FunctionValue<'ctx>,
pub neg: FunctionValue<'ctx>,
@@ -48,6 +51,12 @@ impl<'ctx> Helpers<'ctx> {
let ptr_type = context.ptr_type(AddressSpace::default());
let value_type = context.struct_type(&[int_type.into(), int_type.into()], false);
let func_type = value_type.fn_type(&[ptr_type.into(), ptr_type.into()], false);
let new_thunk = module.add_function(
"new_thunk",
value_type.fn_type(&[ptr_type.into(), ptr_type.into()], false),
None,
);
let debug = module.add_function(
"debug",
context.void_type().fn_type(&[value_type.into()], false),
@@ -55,9 +64,10 @@ impl<'ctx> Helpers<'ctx> {
);
let capture_env = module.add_function(
"capture_env",
context
.void_type()
.fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into()], false),
context.void_type().fn_type(
&[value_type.into(), ptr_type.into(), ptr_type.into()],
false,
),
None,
);
let neg = module.add_function(
@@ -109,6 +119,7 @@ impl<'ctx> Helpers<'ctx> {
None,
);
execution_engine.add_global_mapping(&new_thunk, helper_new_thunk as _);
execution_engine.add_global_mapping(&debug, helper_debug as _);
execution_engine.add_global_mapping(&capture_env, helper_capture_env as _);
execution_engine.add_global_mapping(&neg, helper_neg as _);
@@ -130,6 +141,8 @@ impl<'ctx> Helpers<'ctx> {
value_type,
func_type,
new_thunk,
debug,
capture_env,
neg,
@@ -188,25 +201,20 @@ impl<'ctx> Helpers<'ctx> {
])
.into()
}
pub fn new_thunk(&self, thunk: *const Thunk) -> BasicValueEnum<'ctx> {
self.value_type
.const_named_struct(&[
self.int_type.const_int(ValueTag::Thunk as _, false).into(),
self.ptr_int_type.const_int(thunk as _, false).into(),
])
.into()
}
}
extern "C" fn helper_debug(value: JITValue) {
dbg!(value.tag);
}
extern "C" fn helper_capture_env<'gc>(thunk: JITValue, env: *const VmEnv<'gc>, mc: *const Mutation<'gc>) {
extern "C" fn helper_capture_env<'gc>(
thunk: JITValue,
env: *const VmEnv<'gc>,
mc: *const Mutation<'gc>,
) {
let thunk = unsafe { (thunk.data.ptr as *const Thunk).as_ref().unwrap() };
let env = unsafe { Gc::from_ptr(env) };
thunk.capture(env, unsafe { mc.as_ref() }.unwrap());
thunk.capture_env(env, unsafe { mc.as_ref() }.unwrap());
}
extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue {
@@ -309,13 +317,22 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
}
}
extern "C" fn helper_call(func: JITValue, arg: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue {
extern "C" fn helper_call(
func: JITValue,
arg: JITValue,
vm: *const VM,
mc: *const Mutation,
) -> JITValue {
use ValueTag::*;
match func.tag {
Function => {
let mut func: Value = func.into();
func.call(arg.into(), unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap())
.unwrap();
func.call(
arg.into(),
unsafe { vm.as_ref() }.unwrap(),
unsafe { mc.as_ref() }.unwrap(),
)
.unwrap();
func.into()
}
_ => todo!(),
@@ -329,7 +346,20 @@ extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue {
}
extern "C" fn helper_force(thunk: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue {
let mut val = Value::from(thunk);
val.force(unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap()).unwrap();
val.into()
todo!()
/* let mut val = Value::from(thunk);
val.force(
unsafe { vm.as_ref() }.unwrap(),
unsafe { mc.as_ref() }.unwrap(),
)
.unwrap();
val.into() */
}
extern "C" fn helper_new_thunk(opcodes: *const OpCodes, mc: *const Mutation) -> JITValue {
Value::Thunk(Thunk::new(
unsafe { opcodes.as_ref() }.unwrap(),
unsafe { mc.as_ref() }.unwrap(),
))
.into()
}