chore: cleanup
This commit is contained in:
373
src/eval/jit/helpers.rs
Normal file
373
src/eval/jit/helpers.rs
Normal file
@@ -0,0 +1,373 @@
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
|
||||
use inkwell::AddressSpace;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue};
|
||||
|
||||
use crate::env::VmEnv;
|
||||
use crate::eval::Engine;
|
||||
use crate::ty::internal::{Thunk, Value};
|
||||
|
||||
use super::{JITContext, JITValue, ValueTag, JITValueData};
|
||||
|
||||
pub struct Helpers<'ctx> {
|
||||
pub int_type: IntType<'ctx>,
|
||||
pub float_type: FloatType<'ctx>,
|
||||
pub bool_type: IntType<'ctx>,
|
||||
pub ptr_int_type: IntType<'ctx>,
|
||||
pub ptr_type: PointerType<'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>,
|
||||
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 arg: FunctionValue<'ctx>,
|
||||
pub lookup_let: FunctionValue<'ctx>,
|
||||
pub lookup: FunctionValue<'ctx>,
|
||||
pub force: FunctionValue<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> Helpers<'ctx> {
|
||||
pub fn new(
|
||||
context: &'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()], false);
|
||||
let func_type = value_type.fn_type(&[ptr_type.into()], false);
|
||||
|
||||
let new_thunk = module.add_function(
|
||||
"new_thunk",
|
||||
value_type.fn_type(&[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
|
||||
.void_type()
|
||||
.fn_type(&[value_type.into(), ptr_type.into()], false),
|
||||
None,
|
||||
);
|
||||
let neg = module.add_function(
|
||||
"neg",
|
||||
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),
|
||||
None,
|
||||
);
|
||||
let sub = module.add_function(
|
||||
"sub",
|
||||
value_type.fn_type(&[value_type.into(), value_type.into()], false),
|
||||
None,
|
||||
);
|
||||
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(
|
||||
&[value_type.into(), value_type.into(), ptr_type.into()],
|
||||
false,
|
||||
),
|
||||
None,
|
||||
);
|
||||
let arg = module.add_function(
|
||||
"arg",
|
||||
value_type.fn_type(&[ptr_int_type.into(), ptr_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,
|
||||
),
|
||||
None,
|
||||
);
|
||||
let lookup = module.add_function(
|
||||
"lookup",
|
||||
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(), ptr_type.into()],
|
||||
false,
|
||||
),
|
||||
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 _);
|
||||
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(&arg, helper_arg as _);
|
||||
execution_engine.add_global_mapping(&lookup_let, helper_lookup_let as _);
|
||||
execution_engine.add_global_mapping(&lookup, helper_lookup as _);
|
||||
execution_engine.add_global_mapping(&force, helper_force as _);
|
||||
|
||||
Helpers {
|
||||
int_type,
|
||||
float_type,
|
||||
bool_type,
|
||||
ptr_int_type,
|
||||
ptr_type,
|
||||
value_type,
|
||||
func_type,
|
||||
|
||||
new_thunk,
|
||||
|
||||
debug,
|
||||
capture_env,
|
||||
neg,
|
||||
not,
|
||||
add,
|
||||
sub,
|
||||
eq,
|
||||
or,
|
||||
call,
|
||||
arg,
|
||||
lookup_let,
|
||||
lookup,
|
||||
force,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_int(&self, int: i64) -> BasicValueEnum<'ctx> {
|
||||
self.value_type
|
||||
.const_named_struct(&[
|
||||
self.int_type.const_int(ValueTag::Int as _, false).into(),
|
||||
self.int_type.const_int(int as _, false).into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn new_float(&self, float: f64) -> BasicValueEnum<'ctx> {
|
||||
self.value_type
|
||||
.const_named_struct(&[
|
||||
self.int_type.const_int(ValueTag::Float as _, false).into(),
|
||||
self.float_type.const_float(float).into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn new_bool(&self, bool: bool) -> BasicValueEnum<'ctx> {
|
||||
self.value_type
|
||||
.const_named_struct(&[
|
||||
self.int_type.const_int(ValueTag::Bool as _, false).into(),
|
||||
self.bool_type.const_int(bool as _, false).into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn new_null(&self) -> BasicValueEnum<'ctx> {
|
||||
self.value_type
|
||||
.const_named_struct(&[
|
||||
self.int_type.const_int(ValueTag::Null as _, false).into(),
|
||||
self.int_type.const_zero().into(),
|
||||
])
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn const_string(&self, string: *const u8) -> BasicValueEnum<'ctx> {
|
||||
self.value_type
|
||||
.const_named_struct(&[
|
||||
self.int_type.const_int(ValueTag::String as _, false).into(),
|
||||
self.ptr_int_type.const_int(string 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>) {
|
||||
let thunk = unsafe { (thunk.data.ptr as *const Thunk).as_ref().unwrap() };
|
||||
let env = unsafe { Rc::from_raw(env) };
|
||||
thunk.capture_env(env.clone());
|
||||
std::mem::forget(env);
|
||||
}
|
||||
|
||||
extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match rhs.tag {
|
||||
Int => JITValue {
|
||||
tag: Int,
|
||||
data: JITValueData {
|
||||
int: -unsafe { rhs.data.int },
|
||||
},
|
||||
},
|
||||
Float => JITValue {
|
||||
tag: Float,
|
||||
data: JITValueData {
|
||||
float: -unsafe { rhs.data.float },
|
||||
},
|
||||
},
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn helper_not(rhs: JITValue, _env: *const VmEnv) -> JITValue {
|
||||
use ValueTag::*;
|
||||
match rhs.tag {
|
||||
Bool => JITValue {
|
||||
tag: Bool,
|
||||
data: JITValueData {
|
||||
bool: !unsafe { rhs.data.bool },
|
||||
},
|
||||
},
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn helper_add(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!(
|
||||
"Addition not implemented for {:?} and {:?}",
|
||||
lhs.tag,
|
||||
rhs.tag
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
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!(
|
||||
"Substraction not implemented for {:?} and {:?}",
|
||||
lhs.tag,
|
||||
rhs.tag
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn helper_call<'gc>(func: JITValue, arg: JITValue, engine: *mut Engine) -> JITValue {
|
||||
todo!()
|
||||
}
|
||||
|
||||
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).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).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().clone().into();
|
||||
val
|
||||
}
|
||||
|
||||
extern "C" fn helper_force<'gc>(
|
||||
thunk: JITValue,
|
||||
vm: NonNull<Engine>,
|
||||
jit: *const JITContext<'gc>,
|
||||
) -> JITValue {
|
||||
if !matches!(thunk.tag, ValueTag::Thunk) {
|
||||
return thunk;
|
||||
}
|
||||
todo!()
|
||||
}
|
||||
|
||||
extern "C" fn helper_new_thunk(opcodes: *const ()) -> JITValue {
|
||||
todo!()
|
||||
}
|
||||
Reference in New Issue
Block a user