From b41fd38bcc0d5e1387e99899827dba814019b0f3 Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Fri, 23 May 2025 19:19:20 +0800 Subject: [PATCH] feat(env): move env out of vm, --- src/builtins/mod.rs | 4 ++-- src/{vm => }/env.rs | 16 +++++++-------- src/jit/helpers.rs | 15 ++++---------- src/jit/mod.rs | 9 +++------ src/lib.rs | 1 + src/ty/internal/attrset.rs | 3 ++- src/ty/internal/func.rs | 6 ++++-- src/ty/internal/mod.rs | 7 ++++--- src/ty/internal/primop.rs | 2 +- src/vm/mod.rs | 40 +++++++++++++++++++++++++++++++------- 10 files changed, 62 insertions(+), 41 deletions(-) rename src/{vm => }/env.rs (95%) diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index c7c21a2..da6b34a 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -2,9 +2,10 @@ use std::rc::Rc; use hashbrown::HashMap; +use crate::env::VmEnv; use crate::ty::common::Const; use crate::ty::internal::{AttrSet, PrimOp, Value}; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; pub fn env<'jit, 'vm>(vm: &'vm VM<'jit>) -> VmEnv<'jit, 'vm> { let primops = [ @@ -50,7 +51,6 @@ pub fn env<'jit, 'vm>(vm: &'vm VM<'jit>) -> VmEnv<'jit, 'vm> { env_map.insert(vm.new_sym("true"), Value::Const(Const::Bool(true))); env_map.insert(vm.new_sym("false"), Value::Const(Const::Bool(false))); - let mut map = HashMap::new(); for primop in primops { let primop = Rc::new(primop); diff --git a/src/vm/env.rs b/src/env.rs similarity index 95% rename from src/vm/env.rs rename to src/env.rs index b8f27fc..2cc7ac1 100644 --- a/src/vm/env.rs +++ b/src/env.rs @@ -1,8 +1,8 @@ use std::fmt::Debug; use std::hash::Hash; -use hashbrown::{DefaultHashBuilder, HashMap}; use bumpalo::Bump; +use hashbrown::{DefaultHashBuilder, HashMap}; use crate::ty::internal::Value; @@ -70,7 +70,7 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> { map: None, last: None, }), - last: None + last: None, } } @@ -85,7 +85,7 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> { &*bump.alloc(Env { let_: self.let_.enter_arg(ident, val, bump), with: self.with, - last: Some(self) + last: Some(self), }) } @@ -93,7 +93,7 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> { &*bump.alloc(Env { let_: self.let_.enter_let(map, bump), with: self.with, - last: Some(self) + last: Some(self), }) } @@ -101,7 +101,7 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> { &*bump.alloc(Env { let_: self.let_, with: self.with.enter(map, bump), - last: Some(self) + last: Some(self), }) } @@ -138,14 +138,14 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> LetEnv<'bump, K, V> { pub fn enter_arg(&'bump self, ident: K, val: V, bump: &'bump Bump) -> &'bump Self { &*bump.alloc(Self { map: LetNode::SingleArg(ident, val), - last: Some(self) + last: Some(self), }) } pub fn enter_let(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { &*bump.alloc(Self { map: LetNode::Let(map), - last: Some(self) + last: Some(self), }) } } @@ -161,7 +161,7 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> With<'bump, K, V> { pub fn enter(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { &*bump.alloc(Self { map: Some(map), - last: Some(self) + last: Some(self), }) } } diff --git a/src/jit/helpers.rs b/src/jit/helpers.rs index 15ac1a7..ad843e7 100644 --- a/src/jit/helpers.rs +++ b/src/jit/helpers.rs @@ -5,9 +5,10 @@ use inkwell::module::Module; use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType}; use inkwell::values::{BasicValueEnum, FunctionValue}; +use crate::env::VmEnv; use crate::jit::JITValueData; use crate::ty::internal::{Thunk, Value}; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; use super::{JITValue, ValueTag}; @@ -91,11 +92,7 @@ impl<'ctx> Helpers<'ctx> { let call = module.add_function( "call", value_type.fn_type( - &[ - value_type.into(), - value_type.into(), - ptr_type.into(), - ], + &[value_type.into(), value_type.into(), ptr_type.into()], false, ), None, @@ -311,11 +308,7 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue { } } -extern "C" fn helper_call<'jit>( - func: JITValue, - arg: JITValue, - vm: *const VM<'jit>, -) -> JITValue { +extern "C" fn helper_call<'jit>(func: JITValue, arg: JITValue, vm: *const VM<'jit>) -> JITValue { use ValueTag::*; match func.tag { Function => { diff --git a/src/jit/mod.rs b/src/jit/mod.rs index a8bf29a..b33c322 100644 --- a/src/jit/mod.rs +++ b/src/jit/mod.rs @@ -8,11 +8,12 @@ use inkwell::module::Module; use inkwell::values::{BasicValueEnum, FunctionValue, PointerValue}; use crate::bytecode::{Func, OpCode, UnOp}; +use crate::env::VmEnv; use crate::error::*; use crate::stack::Stack; use crate::ty::common::Const; use crate::ty::internal::{Thunk, Value}; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; mod helpers; @@ -408,11 +409,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { .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()], "call", )? .try_as_basic_value() diff --git a/src/lib.rs b/src/lib.rs index 2b780a5..90ee9cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ mod builtins; mod bytecode; +mod env; mod stack; mod ty; diff --git a/src/ty/internal/attrset.rs b/src/ty/internal/attrset.rs index 74e4105..8da2873 100644 --- a/src/ty/internal/attrset.rs +++ b/src/ty/internal/attrset.rs @@ -5,8 +5,9 @@ use hashbrown::{HashMap, HashSet}; use derive_more::Constructor; use itertools::Itertools; +use crate::env::VmEnv; use crate::error::Result; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; use super::super::public as p; use super::Value; diff --git a/src/ty/internal/func.rs b/src/ty/internal/func.rs index f97e69f..a7e73f7 100644 --- a/src/ty/internal/func.rs +++ b/src/ty/internal/func.rs @@ -6,11 +6,12 @@ use inkwell::execution_engine::JitFunction; use itertools::Itertools; use crate::bytecode::Func as BFunc; +use crate::env::VmEnv; use crate::error::Result; use crate::ir; use crate::jit::JITFunc; use crate::ty::internal::{Thunk, Value}; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; #[derive(Debug, Clone)] pub enum Param { @@ -63,7 +64,8 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> { alias, } => { let arg = arg.unwrap_attr_set(); - let mut new = HashMap::with_capacity_in(formals.len() + alias.iter().len(), &vm.bump); + let mut new = + HashMap::with_capacity_in(formals.len() + alias.iter().len(), &vm.bump); if !ellipsis && arg .as_inner() diff --git a/src/ty/internal/mod.rs b/src/ty/internal/mod.rs index e4b4597..1b9484d 100644 --- a/src/ty/internal/mod.rs +++ b/src/ty/internal/mod.rs @@ -10,8 +10,9 @@ use super::common::*; use super::public as p; use crate::bytecode::OpCodes; +use crate::env::VmEnv; use crate::error::*; -use crate::vm::{VM, VmEnv}; +use crate::vm::VM; mod attrset; mod func; @@ -231,7 +232,7 @@ impl<'jit, 'vm> Value<'jit, 'vm> { pub fn lt(&mut self, other: Self) { use Const::*; - *self = VmConst(Bool(match (&*self, other) { + *self = VmConst(Bool(match (&*self, other) { (VmConst(Int(a)), VmConst(Int(b))) => *a < b, (VmConst(Int(a)), VmConst(Float(b))) => (*a as f64) < b, (VmConst(Float(a)), VmConst(Int(b))) => *a < b as f64, @@ -241,7 +242,7 @@ impl<'jit, 'vm> Value<'jit, 'vm> { (_, x @ Value::Catchable(_)) => { *self = x; return; - }, + } _ => todo!(), })) } diff --git a/src/ty/internal/primop.rs b/src/ty/internal/primop.rs index 77d3004..f9cab25 100644 --- a/src/ty/internal/primop.rs +++ b/src/ty/internal/primop.rs @@ -69,6 +69,6 @@ impl<'jit: 'vm, 'vm> PartialPrimOp<'jit, 'vm> { } else { let args = std::mem::take(&mut self_mut.args); (self.func)(vm, args) - } + } } } diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 812b2c4..af7d535 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -5,6 +5,7 @@ use std::cell::{Cell, OnceCell, RefCell}; use crate::builtins::env; use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp}; +use crate::env::VmEnv; use crate::error::*; use crate::jit::{JITContext, JITFunc}; use crate::stack::Stack; @@ -14,9 +15,6 @@ use crate::ty::public::{self as p, Symbol}; use derive_more::Constructor; use ecow::EcoString; -pub use env::VmEnv; - -mod env; #[cfg(test)] mod test; @@ -156,7 +154,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> { Sub => { rhs.neg(); lhs.add(rhs); - }, + } Mul => lhs.mul(rhs), Div => lhs.div(rhs)?, And => lhs.and(rhs), @@ -187,7 +185,17 @@ impl<'vm, 'jit: 'vm> VM<'jit> { } OpCode::FinalizeRec => { 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); + *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 } => { @@ -249,12 +257,30 @@ impl<'vm, 'jit: 'vm> VM<'jit> { } 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); + *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); + *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 => {