feat: bumpalo

This commit is contained in:
2025-05-23 12:09:53 +08:00
parent 53cbb37b00
commit a47a08b051
12 changed files with 130 additions and 127 deletions

View File

@@ -1,7 +1,7 @@
use bumpalo::Bump;
use hashbrown::{HashMap, HashSet};
use inkwell::execution_engine::JitFunction;
use std::cell::{Cell, OnceCell, RefCell};
use std::rc::Rc;
use crate::builtins::env;
use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp};
@@ -30,12 +30,13 @@ pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
symbols: RefCell::new(prog.symbols),
symmap: RefCell::new(prog.symmap),
consts: prog.consts,
bump: Bump::new(),
jit,
};
let env = env(&vm);
let mut seen = HashSet::new();
let value = vm
.eval(prog.top_level.into_iter(), env.into())?
.eval(prog.top_level.into_iter(), vm.bump.alloc(env))?
.to_public(&vm, &mut seen);
Ok(value)
}
@@ -47,6 +48,7 @@ pub struct VM<'jit> {
symbols: RefCell<Vec<EcoString>>,
symmap: RefCell<HashMap<EcoString, usize>>,
consts: Box<[Const]>,
pub bump: Bump,
jit: JITContext<'jit>,
}
@@ -83,7 +85,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
pub fn eval(
&'vm self,
opcodes: impl Iterator<Item = OpCode>,
mut env: Rc<VmEnv<'jit, 'vm>>,
mut env: &'vm VmEnv<'jit, 'vm>,
) -> Result<Value<'jit, 'vm>> {
let mut stack = Stack::<_, STACK_SIZE>::new();
let mut iter = opcodes.into_iter();
@@ -104,7 +106,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
&'vm self,
opcode: OpCode,
stack: &'s mut Stack<Value<'jit, 'vm>, CAP>,
env: &mut Rc<VmEnv<'jit, 'vm>>,
env: &mut &'vm VmEnv<'jit, 'vm>,
) -> Result<usize> {
match opcode {
OpCode::Illegal => panic!("illegal opcode"),
@@ -112,7 +114,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
OpCode::LoadThunk { idx } => {
stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx)).into()))?
}
OpCode::CaptureEnv => stack.tos().as_ref().unwrap_thunk().capture(env.clone()),
OpCode::CaptureEnv => stack.tos().as_ref().unwrap_thunk().capture(*env),
OpCode::ForceValue => {
stack.tos_mut().force(self)?;
}
@@ -131,7 +133,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
OpCode::Func { idx } => {
let func = self.get_func(idx);
stack.push(Value::Func(
Func::new(func, env.clone(), OnceCell::new(), Cell::new(0)).into(),
Func::new(func, env, OnceCell::new(), Cell::new(0)).into(),
))?;
}
OpCode::UnOp { op } => {
@@ -184,7 +186,8 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
stack.push(Value::AttrSet(AttrSet::with_capacity(cap).into()))?;
}
OpCode::FinalizeRec => {
env.enter_let(stack.tos().clone().unwrap_attr_set().into_inner());
let new = self.bump.alloc(HashMap::new_in(&self.bump));
*env = env.enter_let(stack.tos().as_ref().unwrap_attr_set().as_inner().iter().map(|(&k, v)| (k, v.clone())).collect_into(new), &self.bump);
stack.tos_mut().as_mut().unwrap_attr_set().capture(env);
}
OpCode::PushStaticAttr { name } => {
@@ -244,10 +247,16 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
.clone(),
)?;
}
OpCode::EnterLetEnv => env.enter_let(stack.pop().unwrap_attr_set().into_inner()),
OpCode::LeaveLetEnv => env.leave_let(),
OpCode::EnterWithEnv => env.enter_with(stack.pop().unwrap_attr_set().into_inner()),
OpCode::LeaveWithEnv => env.leave_with(),
OpCode::EnterLetEnv => {
let new = self.bump.alloc(HashMap::new_in(&self.bump));
*env = env.enter_let(stack.pop().unwrap_attr_set().into_inner().iter().map(|(&k, v)| (k, v.clone())).collect_into(new), &self.bump);
}
OpCode::LeaveLetEnv => *env = env.leave(),
OpCode::EnterWithEnv => {
let new = self.bump.alloc(HashMap::new_in(&self.bump));
*env = env.enter_with(stack.pop().unwrap_attr_set().into_inner().iter().map(|(&k, v)| (k, v.clone())).collect_into(new), &self.bump);
}
OpCode::LeaveWithEnv => *env = env.leave(),
OpCode::Assert => {
if !stack.pop().unwrap_const().unwrap_bool() {
todo!()