feat(jit): fix segfault

This commit is contained in:
2025-05-18 17:07:49 +08:00
parent f98d623c13
commit af5a312e1e
10 changed files with 115 additions and 111 deletions

View File

@@ -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!()