feat: gc-arena

finally...
This commit is contained in:
2025-05-28 21:52:13 +08:00
parent c3ace28af1
commit 99dce2e778
7 changed files with 98 additions and 99 deletions

View File

@@ -8,7 +8,7 @@ use inkwell::execution_engine::{ExecutionEngine, JitFunction};
use inkwell::module::Module;
use inkwell::values::{BasicValueEnum, FunctionValue, PointerValue};
use crate::bytecode::{Func, OpCode, UnOp};
use crate::bytecode::{OpCode, UnOp};
use crate::env::VmEnv;
use crate::error::*;
use crate::stack::Stack;
@@ -28,6 +28,7 @@ const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
#[repr(u64)]
#[derive(Debug, Clone, Copy)]
pub enum ValueTag {
Null,
Int,
Float,
String,
@@ -37,7 +38,6 @@ pub enum ValueTag {
Function,
Thunk,
Path,
Null,
}
#[repr(C)]
@@ -164,6 +164,7 @@ unsafe impl<'gc> Collect<'gc> for JITContext<'gc> {
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, _: &mut T) {}
const NEEDS_TRACE: bool = false;
}
impl<'gc> JITContext<'gc> {
pub fn new(context: &'gc Context) -> Self {
// force linker to link JIT engine
@@ -196,9 +197,12 @@ impl<'gc> JITContext<'gc> {
.unwrap()
}
pub fn compile_function(&self, func: &'gc Func, vm: &'gc VM<'gc>) -> Result<JITFunc<'gc>> {
pub fn compile_seq(
&self,
opcodes: impl Iterator<Item = OpCode> + ExactSizeIterator + DoubleEndedIterator,
vm: &'gc VM<'gc>,
) -> Result<JITFunc<'gc>> {
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);
@@ -206,15 +210,8 @@ impl<'gc> JITContext<'gc> {
let mc = func_.get_nth_param(1).unwrap().into_pointer_value();
let entry = self.context.append_basic_block(func_, "entry");
self.builder.position_at_end(entry);
self.build_expr(
&mut iter,
vm,
env,
mc,
&mut stack,
func_,
func.opcodes.len(),
)?;
let len = opcodes.len();
self.build_expr(&mut opcodes.rev(), vm, env, mc, &mut stack, func_, len)?;
assert_eq!(stack.len(), 1);
let value = stack.pop();
@@ -336,6 +333,25 @@ impl<'gc> JITContext<'gc> {
.unwrap_left()
.into(),
)?,
OpCode::ForceValue => {
let thunk = stack.pop();
let _ = stack.push(
self.builder
.build_direct_call(
self.helpers.force,
&[
thunk.into(),
self.new_ptr(vm as *const _).into(),
mc.into(),
self.new_ptr(self as *const _).into(),
],
"call_force",
)?
.try_as_basic_value()
.left()
.unwrap(),
);
}
OpCode::CaptureEnv => {
let thunk = *stack.tos();
self.builder.build_direct_call(
@@ -442,21 +458,12 @@ impl<'gc> JITContext<'gc> {
)?,
OpCode::Call => {
let arg = stack.pop();
let func = self
.builder
.build_direct_call(
self.helpers.force,
&[stack.pop().into(), self.new_ptr(vm).into()],
"force",
)?
.try_as_basic_value()
.left()
.unwrap();
let func = stack.pop();
let ret = self
.builder
.build_direct_call(
self.helpers.call,
&[func.into(), arg.into(), self.new_ptr(vm).into()],
&[func.into(), arg.into(), self.new_ptr(vm).into(), mc.into()],
"call",
)?
.try_as_basic_value()