feat: JIT (WIP)

This commit is contained in:
2025-06-22 17:17:33 +08:00
parent f679ff2ec9
commit e26789f3b7
6 changed files with 373 additions and 133 deletions

View File

@@ -1,4 +1,7 @@
use std::alloc::Layout;
use std::ffi::CStr;
use std::ptr::NonNull;
use std::{slice, str};
use inkwell::AddressSpace;
use inkwell::context::Context;
@@ -33,39 +36,36 @@ pub struct Helpers<'ctx> {
pub eq: FunctionValue<'ctx>,
pub or: FunctionValue<'ctx>,
pub call: FunctionValue<'ctx>,
pub arg: FunctionValue<'ctx>,
pub lookup_let: FunctionValue<'ctx>,
pub lookup_arg: FunctionValue<'ctx>,
pub lookup: FunctionValue<'ctx>,
pub force: FunctionValue<'ctx>,
pub alloc_array: FunctionValue<'ctx>,
}
impl<'ctx> Helpers<'ctx> {
pub fn new(
context: &'ctx Context,
ctx: &'ctx Context,
module: &Module<'ctx>,
execution_engine: &ExecutionEngine<'ctx>,
) -> Self {
let int_type = context.i64_type();
let float_type = context.f64_type();
let bool_type = context.bool_type();
let ptr_int_type = context.ptr_sized_int_type(execution_engine.get_target_data(), None);
let ptr_type = context.ptr_type(AddressSpace::default());
let value_type = context.struct_type(&[int_type.into(), int_type.into(), int_type.into()], false);
let func_type = value_type.fn_type(&[ptr_type.into()], false);
let int_type = ctx.i64_type();
let float_type = ctx.f64_type();
let bool_type = ctx.bool_type();
let ptr_int_type = ctx.ptr_sized_int_type(execution_engine.get_target_data(), None);
let ptr_type = ctx.ptr_type(AddressSpace::default());
let value_type =
ctx.struct_type(&[int_type.into(), 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),
None,
);
let capture_env = module.add_function(
"capture_env",
context
ctx
.void_type()
.fn_type(&[value_type.into(), ptr_type.into()], false),
None,
@@ -106,6 +106,7 @@ impl<'ctx> Helpers<'ctx> {
&[
value_type.into(),
ptr_type.into(),
ptr_int_type.into(),
ptr_type.into(),
ptr_type.into(),
],
@@ -113,22 +114,25 @@ impl<'ctx> Helpers<'ctx> {
),
None,
);
let arg = module.add_function(
"arg",
value_type.fn_type(&[ptr_int_type.into(), ptr_type.into()], false),
let debug = module.add_function(
"debug",
ctx
.void_type()
.fn_type(&[ptr_type.into(), int_type.into()], false),
None,
);
let lookup_let = module.add_function(
"lookup_let",
value_type.fn_type(
&[ptr_int_type.into(), ptr_int_type.into(), ptr_type.into()],
false,
),
let lookup_arg = module.add_function(
"lookup_arg",
// value_type.fn_type(&[ptr_type.into(), int_type.into()], false),
ctx.void_type().fn_type(&[ptr_type.into(), int_type.into(), ptr_type.into()], false),
None,
);
let lookup = module.add_function(
"lookup",
value_type.fn_type(&[ptr_int_type.into(), ptr_type.into()], false),
value_type.fn_type(
&[ptr_int_type.into(), ptr_type.into(), ptr_int_type.into()],
false,
),
None,
);
let force = module.add_function(
@@ -139,6 +143,11 @@ impl<'ctx> Helpers<'ctx> {
),
None,
);
let alloc_array = module.add_function(
"alloc_array",
value_type.fn_type(&[ptr_int_type.into()], false),
None,
);
execution_engine.add_global_mapping(&new_thunk, helper_new_thunk as _);
execution_engine.add_global_mapping(&debug, helper_debug as _);
@@ -150,10 +159,10 @@ impl<'ctx> Helpers<'ctx> {
execution_engine.add_global_mapping(&eq, helper_eq as _);
execution_engine.add_global_mapping(&or, helper_or as _);
execution_engine.add_global_mapping(&call, helper_call as _);
execution_engine.add_global_mapping(&arg, helper_arg as _);
execution_engine.add_global_mapping(&lookup_let, helper_lookup_let as _);
execution_engine.add_global_mapping(&lookup_arg, helper_lookup_arg as _);
execution_engine.add_global_mapping(&lookup, helper_lookup as _);
execution_engine.add_global_mapping(&force, helper_force as _);
execution_engine.add_global_mapping(&alloc_array, helper_alloc_array as _);
Helpers {
int_type,
@@ -175,10 +184,11 @@ impl<'ctx> Helpers<'ctx> {
eq,
or,
call,
arg,
lookup_let,
lookup_arg,
lookup,
force,
alloc_array,
}
}
@@ -186,10 +196,14 @@ impl<'ctx> Helpers<'ctx> {
self.value_type.const_named_struct(&[
self.const_int(tag as i64).into(),
data,
self.int_type.const_zero().into()
self.int_type.const_zero().into(),
])
}
pub fn const_ptr_int(&self, int: usize) -> IntValue<'ctx> {
self.ptr_int_type.const_int(int as _, false)
}
pub fn const_int(&self, int: i64) -> IntValue<'ctx> {
self.int_type.const_int(int as _, false)
}
@@ -219,10 +233,6 @@ impl<'ctx> Helpers<'ctx> {
}
}
extern "C" fn helper_debug(value: JITValue) {
dbg!(value.tag);
}
extern "C" fn helper_capture_env(thunk: JITValue, env: *const Env) {
todo!()
}
@@ -329,28 +339,47 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
extern "C" fn helper_call(
func: JITValue,
args: Box<[JITValue]>,
engine: NonNull<Engine>,
env: NonNull<Env>,
args: *mut JITValue,
len: usize,
mut engine: NonNull<Engine>,
mut env: NonNull<Env>,
) -> JITValue {
let func = Value::from(func);
let mut func = Value::from(func);
// TODO: Error Handling
let args = core::ptr::slice_from_raw_parts_mut(args, len);
let args = unsafe { Box::from_raw(args) };
func.call(
args.into_iter().map(Value::from).collect(),
unsafe { engine.as_mut() },
unsafe { env.as_mut() },
)
.unwrap();
todo!()
}
extern "C" fn helper_arg(idx: usize, env: *const Env) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val: JITValue = env.lookup_arg(idx).clone().into();
extern "C" fn helper_debug(env: NonNull<Env>, level: u64) {
dbg!(env, level);
dbg!(unsafe { env.as_ref() }.lookup_arg(level as usize));
}
extern "C" fn helper_lookup_arg(env_ptr: NonNull<Env>, level: u64, ret: NonNull<JITValue>) {
dbg!(env_ptr, level);
let env_ref = unsafe { env_ptr.as_ref() };
let val: JITValue = env_ref.lookup_arg(level as usize).into();
unsafe { ret.write(val) }
}
extern "C" fn helper_lookup(env: NonNull<Env>, ptr: *const u8, len: usize) -> JITValue {
let env = unsafe { env.as_ref() };
// TODO: Error Handling
let val: JITValue = env
.lookup_with(unsafe { str::from_utf8_unchecked(slice::from_raw_parts(ptr, len)) })
.unwrap()
.clone()
.into();
val
}
extern "C" fn helper_lookup_let(level: usize, idx: usize, env: *const Env) -> JITValue {
todo!()
}
extern "C" fn helper_lookup(sym: usize, env: *const Env) -> JITValue {
todo!()
}
extern "C" fn helper_force(
thunk: JITValue,
vm: NonNull<Engine>,
@@ -365,3 +394,7 @@ extern "C" fn helper_force(
extern "C" fn helper_new_thunk(opcodes: *const ()) -> JITValue {
todo!()
}
unsafe extern "C" fn helper_alloc_array(len: usize) -> *mut u8 {
unsafe { std::alloc::alloc(Layout::array::<JITValue>(len).unwrap()) }
}