feat(jit): lookup

This commit is contained in:
2025-05-18 21:52:36 +08:00
parent af5a312e1e
commit 4124156d52
4 changed files with 88 additions and 41 deletions

View File

@@ -1,3 +1,5 @@
use std::rc::Rc;
use inkwell::AddressSpace;
use inkwell::context::Context;
use inkwell::execution_engine::ExecutionEngine;
@@ -21,10 +23,12 @@ pub struct Helpers<'ctx> {
pub func_type: FunctionType<'ctx>,
pub debug: FunctionValue<'ctx>,
pub captured_env: FunctionValue<'ctx>,
pub capture_env: FunctionValue<'ctx>,
pub neg: FunctionValue<'ctx>,
pub add: FunctionValue<'ctx>,
pub sub: FunctionValue<'ctx>,
pub call: FunctionValue<'ctx>,
pub lookup: FunctionValue<'ctx>,
}
impl<'ctx> Helpers<'ctx> {
@@ -40,7 +44,7 @@ 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(), value_type.into()],
&[ptr_type.into(), ptr_type.into()],
false,
);
let debug = module.add_function(
@@ -50,7 +54,7 @@ impl<'ctx> Helpers<'ctx> {
.fn_type(&[value_type.into()], false),
None
);
let captured_env = module.add_function(
let capture_env = module.add_function(
"capture_env",
context
.void_type()
@@ -67,19 +71,31 @@ impl<'ctx> Helpers<'ctx> {
value_type.fn_type(&[value_type.into(), value_type.into()], false),
None
);
let sub = module.add_function(
"sub",
value_type.fn_type(&[value_type.into(), value_type.into()], false),
None
);
// Assuming a single argument for now based on the test case
let call = module.add_function(
"call",
value_type.fn_type(&[ptr_type.into(), ptr_type.into(), ptr_type.into(), value_type.into()], false),
None
);
let lookup = module.add_function(
"lookup",
value_type.fn_type(&[ptr_int_type.into(), ptr_type.into()], false),
None
);
execution_engine.add_global_mapping(&debug, helper_debug as _);
execution_engine.add_global_mapping(&captured_env, helper_capture_env as _);
execution_engine.add_global_mapping(&capture_env, helper_capture_env as _);
execution_engine.add_global_mapping(&neg, helper_neg as _);
execution_engine.add_global_mapping(&add, helper_add as _);
execution_engine.add_global_mapping(&sub, helper_sub as _);
execution_engine.add_global_mapping(&call, helper_call as _);
execution_engine.add_global_mapping(&lookup, helper_lookup as _);
Helpers {
@@ -92,10 +108,12 @@ impl<'ctx> Helpers<'ctx> {
func_type,
debug,
captured_env,
capture_env,
neg,
add,
sub,
call,
lookup,
}
}
@@ -194,9 +212,31 @@ extern "C" fn helper_add(lhs: JITValue, rhs: JITValue) -> JITValue {
}
#[unsafe(no_mangle)]
extern "C" fn helper_call<'jit, 'vm>(vm: *const VM<'jit>, env: *const Env<'jit, 'vm>, func_ptr: *const (), arg: JITValue) -> JITValue {
let func: JITFunc = unsafe { std::mem::transmute(func_ptr) };
unsafe {
func(vm, env, arg)
extern "C" fn helper_sub(lhs: JITValue, rhs: JITValue) -> JITValue {
use ValueTag::*;
match (lhs.tag, rhs.tag) {
(Int, Int) => JITValue {
tag: Int,
data: JITValueData {
int: unsafe { lhs.data.int - rhs.data.int },
},
},
_ => todo!("Substruction not implemented for {:?} and {:?}", lhs.tag, rhs.tag),
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_call<'jit, 'vm>(vm: *const VM<'jit>, env: *const Env<'jit, 'vm>, func_ptr: *const (), arg: JITValue) -> JITValue {
let func: JITFunc = unsafe { std::mem::transmute(func_ptr) };
todo!();
unsafe {
func(vm, env)
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_lookup<'jit, 'vm>(sym: usize, env: *const Env<'jit, 'vm>) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val = env.lookup(sym);
val.unwrap().into()
}