feat(jit): fix segfault
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use inkwell::AddressSpace;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::module::{Linkage, Module};
|
||||
use inkwell::module::Module;
|
||||
use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
|
||||
use inkwell::values::{BasicValueEnum, FunctionValue};
|
||||
|
||||
@@ -194,7 +194,9 @@ extern "C" fn helper_add(lhs: JITValue, rhs: JITValue) -> JITValue {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn helper_call(vm: *const VM<'_>, env: *const Env<'_>, func_ptr: *const (), arg: JITValue) -> JITValue {
|
||||
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) };
|
||||
func(vm, env, arg)
|
||||
unsafe {
|
||||
func(vm, env, arg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::rc::Rc;
|
||||
|
||||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::ExecutionEngine;
|
||||
use inkwell::execution_engine::{ExecutionEngine, JitFunction};
|
||||
use inkwell::module::Module;
|
||||
use inkwell::values::{BasicValueEnum, PointerValue};
|
||||
use inkwell::OptimizationLevel;
|
||||
@@ -52,8 +52,8 @@ pub union JITValueData {
|
||||
ptr: *const ()
|
||||
}
|
||||
|
||||
impl<'vm> Into<Value<'vm>> for JITValue {
|
||||
fn into(self) -> Value<'vm> {
|
||||
impl<'jit: 'vm, 'vm> Into<Value<'jit, 'vm>> for JITValue {
|
||||
fn into(self) -> Value<'jit, 'vm> {
|
||||
use ValueTag::*;
|
||||
match self.tag {
|
||||
Int => Value::Const(Const::Int(unsafe { self.data.int })),
|
||||
@@ -63,8 +63,8 @@ impl<'vm> Into<Value<'vm>> for JITValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value<'_>> for JITValue {
|
||||
fn from(value: Value<'_>) -> Self {
|
||||
impl From<Value<'_, '_>> for JITValue {
|
||||
fn from(value: Value<'_, '_>) -> Self {
|
||||
match value {
|
||||
Value::Const(Const::Int(int)) => JITValue { tag: ValueTag::Int, data: JITValueData { int } },
|
||||
_ => todo!()
|
||||
@@ -72,7 +72,7 @@ impl From<Value<'_>> for JITValue {
|
||||
}
|
||||
}
|
||||
|
||||
pub type JITFunc<'vm> = fn(*const VM<'_>, *const Env<'vm>, JITValue) -> JITValue;
|
||||
pub type JITFunc<'jit, 'vm> = unsafe extern "C" fn(*const VM<'jit>, *const Env<'jit, 'vm>, JITValue) -> JITValue;
|
||||
|
||||
pub struct JITContext<'ctx> {
|
||||
context: &'ctx Context,
|
||||
@@ -108,7 +108,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
||||
self.builder.build_int_to_ptr(self.helpers.int_type.const_int(ptr as _, false), self.helpers.ptr_type, "ptrconv").unwrap()
|
||||
}
|
||||
|
||||
pub fn compile_function(&self, func: &Func, vm: &'vm VM<'_>) -> Result<&'vm JITFunc> {
|
||||
pub fn compile_function(&self, func: &Func, vm: &'vm VM<'_>) -> Result<JitFunction<'ctx, JITFunc<'ctx, 'vm>>> {
|
||||
let mut stack = Stack::<_, STACK_SIZE>::new();
|
||||
let mut iter = func.opcodes.iter().copied();
|
||||
let func_ = self.module.add_function("nixjit_function", self.helpers.func_type, None);
|
||||
@@ -125,8 +125,8 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
||||
func_.print_to_stderr();
|
||||
unsafe {
|
||||
let name = func_.get_name().to_str().unwrap();
|
||||
let addr = self.execution_engine.get_function_address(name).unwrap();
|
||||
Ok(std::mem::transmute(addr))
|
||||
let addr = self.execution_engine.get_function(name).unwrap();
|
||||
Ok(addr)
|
||||
}
|
||||
} else {
|
||||
todo!()
|
||||
|
||||
Reference in New Issue
Block a user