feat: JIT (unusable, segfault)
This commit is contained in:
200
src/jit/helpers.rs
Normal file
200
src/jit/helpers.rs
Normal file
@@ -0,0 +1,200 @@
|
||||
use inkwell::AddressSpace;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::module::{Linkage, Module};
|
||||
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::{VM, Env};
|
||||
|
||||
use super::{JITValue, ValueTag, JITFunc};
|
||||
|
||||
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 debug: FunctionValue<'ctx>,
|
||||
pub captured_env: FunctionValue<'ctx>,
|
||||
pub neg: FunctionValue<'ctx>,
|
||||
pub add: FunctionValue<'ctx>,
|
||||
pub call: 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(), ptr_type.into(), value_type.into()],
|
||||
false,
|
||||
);
|
||||
let debug = module.add_function(
|
||||
"debug",
|
||||
context
|
||||
.void_type()
|
||||
.fn_type(&[value_type.into()], false),
|
||||
None
|
||||
);
|
||||
let captured_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 add = module.add_function(
|
||||
"add",
|
||||
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
|
||||
);
|
||||
|
||||
|
||||
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(&neg, helper_neg as _);
|
||||
execution_engine.add_global_mapping(&add, helper_add as _);
|
||||
execution_engine.add_global_mapping(&call, helper_call as _);
|
||||
|
||||
|
||||
Helpers {
|
||||
int_type,
|
||||
float_type,
|
||||
bool_type,
|
||||
ptr_int_type,
|
||||
ptr_type,
|
||||
value_type,
|
||||
func_type,
|
||||
|
||||
debug,
|
||||
captured_env,
|
||||
neg,
|
||||
add,
|
||||
call,
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_debug(value: JITValue) {
|
||||
dbg!(value.tag);
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_capture_env(thunk: JITValue, env: *const Env) {
|
||||
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 {
|
||||
use ValueTag::*;
|
||||
match rhs.tag {
|
||||
Int => JITValue {
|
||||
tag: Int,
|
||||
data: JITValueData {
|
||||
int: -unsafe { rhs.data.int },
|
||||
},
|
||||
},
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_call(vm: *const VM<'_>, env: *const Env<'_>, func_ptr: *const (), arg: JITValue) -> JITValue {
|
||||
let func: JITFunc = unsafe { std::mem::transmute(func_ptr) };
|
||||
func(vm, env, arg)
|
||||
}
|
||||
Reference in New Issue
Block a user