feat(jit): fib!
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use inkwell::AddressSpace;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
@@ -8,8 +6,8 @@ use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue};
|
||||
|
||||
use crate::jit::JITValueData;
|
||||
use crate::ty::internal::Thunk;
|
||||
use crate::vm::{Env, VM};
|
||||
use crate::ty::internal::{Thunk, Value};
|
||||
use crate::vm::{LetEnv, VM};
|
||||
|
||||
use super::{JITFunc, JITValue, ValueTag};
|
||||
|
||||
@@ -25,10 +23,14 @@ pub struct Helpers<'ctx> {
|
||||
pub debug: FunctionValue<'ctx>,
|
||||
pub capture_env: FunctionValue<'ctx>,
|
||||
pub neg: FunctionValue<'ctx>,
|
||||
pub not: FunctionValue<'ctx>,
|
||||
pub add: FunctionValue<'ctx>,
|
||||
pub sub: FunctionValue<'ctx>,
|
||||
pub eq: FunctionValue<'ctx>,
|
||||
pub or: FunctionValue<'ctx>,
|
||||
pub call: FunctionValue<'ctx>,
|
||||
pub lookup: FunctionValue<'ctx>,
|
||||
pub force: FunctionValue<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> Helpers<'ctx> {
|
||||
@@ -61,6 +63,11 @@ impl<'ctx> Helpers<'ctx> {
|
||||
value_type.fn_type(&[value_type.into(), ptr_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let not = module.add_function(
|
||||
"not",
|
||||
value_type.fn_type(&[value_type.into(), ptr_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let add = module.add_function(
|
||||
"add",
|
||||
value_type.fn_type(&[value_type.into(), value_type.into()], false),
|
||||
@@ -71,15 +78,23 @@ impl<'ctx> Helpers<'ctx> {
|
||||
value_type.fn_type(&[value_type.into(), value_type.into()], false),
|
||||
None,
|
||||
);
|
||||
// Assuming a single argument for now based on the test case
|
||||
let eq = module.add_function(
|
||||
"eq",
|
||||
value_type.fn_type(&[value_type.into(), value_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let or = module.add_function(
|
||||
"or",
|
||||
value_type.fn_type(&[value_type.into(), value_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let call = module.add_function(
|
||||
"call",
|
||||
value_type.fn_type(
|
||||
&[
|
||||
ptr_type.into(),
|
||||
ptr_type.into(),
|
||||
ptr_type.into(),
|
||||
value_type.into(),
|
||||
value_type.into(),
|
||||
ptr_type.into(),
|
||||
],
|
||||
false,
|
||||
),
|
||||
@@ -90,14 +105,23 @@ impl<'ctx> Helpers<'ctx> {
|
||||
value_type.fn_type(&[ptr_int_type.into(), ptr_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let force = module.add_function(
|
||||
"force",
|
||||
value_type.fn_type(&[value_type.into(), ptr_type.into()], false),
|
||||
None,
|
||||
);
|
||||
|
||||
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 _);
|
||||
execution_engine.add_global_mapping(¬, helper_not as _);
|
||||
execution_engine.add_global_mapping(&add, helper_add as _);
|
||||
execution_engine.add_global_mapping(&sub, helper_sub as _);
|
||||
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(&lookup, helper_lookup as _);
|
||||
execution_engine.add_global_mapping(&force, helper_force as _);
|
||||
|
||||
Helpers {
|
||||
int_type,
|
||||
@@ -111,10 +135,14 @@ impl<'ctx> Helpers<'ctx> {
|
||||
debug,
|
||||
capture_env,
|
||||
neg,
|
||||
not,
|
||||
add,
|
||||
sub,
|
||||
eq,
|
||||
or,
|
||||
call,
|
||||
lookup,
|
||||
force
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,13 +207,13 @@ extern "C" fn helper_debug(value: JITValue) {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_capture_env(thunk: JITValue, env: *const Env) {
|
||||
extern "C" fn helper_capture_env(thunk: JITValue, env: *const LetEnv) {
|
||||
let thunk: &Thunk = unsafe { std::mem::transmute(thunk.data.ptr.as_ref().unwrap()) };
|
||||
thunk.capture(unsafe { env.as_ref().unwrap() }.clone());
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_neg(rhs: JITValue, _env: *const Env) -> JITValue {
|
||||
extern "C" fn helper_neg(rhs: JITValue, _env: *const LetEnv) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match rhs.tag {
|
||||
Int => JITValue {
|
||||
@@ -198,6 +226,20 @@ extern "C" fn helper_neg(rhs: JITValue, _env: *const Env) -> JITValue {
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_not(rhs: JITValue, _env: *const LetEnv) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match rhs.tag {
|
||||
Bool => JITValue {
|
||||
tag: Bool,
|
||||
data: JITValueData {
|
||||
bool: !unsafe { rhs.data.bool },
|
||||
},
|
||||
},
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_add(lhs: JITValue, rhs: JITValue) -> JITValue {
|
||||
use ValueTag::*;
|
||||
@@ -235,20 +277,68 @@ extern "C" fn helper_sub(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) };
|
||||
todo!();
|
||||
unsafe { func(vm, env) }
|
||||
extern "C" fn helper_eq(lhs: JITValue, rhs: JITValue) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match (lhs.tag, rhs.tag) {
|
||||
(Int, Int) => JITValue {
|
||||
tag: Bool,
|
||||
data: JITValueData {
|
||||
bool: unsafe { lhs.data.int == rhs.data.int }
|
||||
},
|
||||
},
|
||||
_ => todo!(
|
||||
"Equation not implemented for {:?} and {:?}",
|
||||
lhs.tag,
|
||||
rhs.tag
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_lookup<'jit, 'vm>(sym: usize, env: *const Env<'jit, 'vm>) -> JITValue {
|
||||
extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match (lhs.tag, rhs.tag) {
|
||||
(Bool, Bool) => JITValue {
|
||||
tag: Bool,
|
||||
data: JITValueData {
|
||||
bool: unsafe { lhs.data.bool || rhs.data.bool },
|
||||
},
|
||||
},
|
||||
_ => todo!(
|
||||
"Substruction not implemented for {:?} and {:?}",
|
||||
lhs.tag,
|
||||
rhs.tag
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_call<'jit, 'vm>(
|
||||
func: JITValue,
|
||||
arg: JITValue,
|
||||
vm: *const VM<'jit>,
|
||||
) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match func.tag {
|
||||
Function => {
|
||||
let func: Value = func.into();
|
||||
func.call(unsafe { vm.as_ref() }.unwrap(), vec![arg.into()]).unwrap().into()
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_lookup<'jit, 'vm>(sym: usize, env: *const LetEnv<'jit, 'vm>) -> JITValue {
|
||||
let env = unsafe { env.as_ref() }.unwrap();
|
||||
let val = env.lookup(sym);
|
||||
dbg!(val.as_ref().unwrap().typename());
|
||||
val.unwrap().into()
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_force<'jit, 'vm>(thunk: JITValue, vm: *const VM<'jit>) -> JITValue {
|
||||
let mut val = Value::from(thunk);
|
||||
val.force(unsafe { vm.as_ref() }.unwrap()).unwrap();
|
||||
val.into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user